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