github.com/wtfutil/wtf@v0.43.0/modules/power/widget.go (about) 1 package power 2 3 import ( 4 "fmt" 5 "runtime" 6 7 "github.com/rivo/tview" 8 "github.com/wtfutil/wtf/utils" 9 "github.com/wtfutil/wtf/view" 10 ) 11 12 const ( 13 msgNoBattery = " no battery found" 14 productNameTrimLen = 14 15 ) 16 17 type Widget struct { 18 view.TextWidget 19 20 Battery *Battery 21 ManagedDevices *ManagedDevices 22 23 settings *Settings 24 } 25 26 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget { 27 widget := Widget{ 28 TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common), 29 30 Battery: NewBattery(), 31 ManagedDevices: NewManagedDevices(), 32 33 settings: settings, 34 } 35 36 widget.View.SetWrap(true) 37 38 return &widget 39 } 40 41 /* -------------------- Exported Functions -------------------- */ 42 43 func (widget *Widget) Refresh() { 44 widget.Battery.Refresh() 45 46 // Handle the reading of connected battery-driven devices 47 switch runtime.GOOS { 48 case "darwin": 49 widget.ManagedDevices.Refresh() 50 case "linux": 51 case "windows": 52 default: 53 } 54 55 widget.Redraw(widget.content) 56 } 57 58 /* -------------------- Unexported Functions -------------------- */ 59 60 func (widget *Widget) content() (string, string, bool) { 61 content := fmt.Sprintf(" %14s: %s\n", "Source", powerSource()) 62 63 if widget.Battery.String() != msgNoBattery { 64 content += widget.Battery.String() 65 content += "\n" 66 } 67 68 content += "\n" 69 70 for _, manDev := range widget.ManagedDevices.Devices { 71 if manDev.HasBattery() { 72 percent := utils.ColorizePercent(float64(manDev.BatteryPercent())) 73 74 prodName := manDev.Product() 75 76 if len(prodName) > productNameTrimLen { 77 prodName = prodName[:productNameTrimLen] 78 } 79 80 content += fmt.Sprintf(" %s: %s\n", prodName, percent) 81 } 82 } 83 84 return widget.CommonSettings().Title, content, true 85 }