-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
76 lines (68 loc) · 1.89 KB
/
status.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
package main
import (
"fmt"
"strings"
"time"
"github.com/distatus/battery"
"github.com/rivo/tview"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
)
func setObserver(t *tview.TextView, routine <-chan string) {
for update := range routine {
t.SetText(update)
}
}
func writerRoutine(status chan<- string, delay time.Duration, update func() string) {
for {
status <- update()
time.Sleep(delay)
}
}
func timeRoutine(status chan<- string) {
writerRoutine(status, 500*time.Millisecond, func() string {
curTime := time.Now()
weekday := fmt.Sprint(curTime.Weekday())[:3]
_, month, day := curTime.Date()
clock := strings.Split(curTime.Format(time.Layout), " ")[1]
return fmt.Sprintf(" [:black]%s %d %s %s", weekday, day, month, clock)
})
}
func bytesToGB(bytes uint64) float64 {
return float64(bytes) / (1 << 30)
}
func machineRoutine(status chan<- string) {
writerRoutine(status, 2*time.Second, func() string {
cpuP, _ := cpu.Percent(time.Second, false)
v, _ := mem.VirtualMemory()
b, _ := battery.Get(0)
bgColor := "black"
if v.UsedPercent > 70.0 || cpuP[0] > 55.0 {
bgColor = "red"
}
return fmt.Sprintf(
"[:%s]cpu: %.2f%% ram: %.2f%% battery: %.2f%%",
bgColor,
cpuP[0],
v.UsedPercent,
b.Current/b.Full*100,
)
})
}
func getStatusUI(app *tview.Application) *tview.Flex {
createTV := func(align int) *tview.TextView {
return tview.NewTextView().SetDynamicColors(true).SetWrap(false).SetTextAlign(align).SetChangedFunc(func() {
app.Draw()
})
}
timeStat := createTV(0)
machineStat := createTV(2)
status := tview.NewFlex().AddItem(timeStat, 0, 1, false).AddItem(machineStat, 0, 2, false)
timeStatusChan := make(chan string)
machineStatusChan := make(chan string)
go timeRoutine(timeStatusChan)
go machineRoutine(machineStatusChan)
go setObserver(timeStat, timeStatusChan)
go setObserver(machineStat, machineStatusChan)
return status
}