-
Notifications
You must be signed in to change notification settings - Fork 0
/
batmon.go
76 lines (62 loc) · 1.26 KB
/
batmon.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 (
"time"
"github.com/batmon/domain"
"github.com/batmon/ui"
termbox "github.com/nsf/termbox-go"
)
const ver = "1.0"
const timeInterval = 1
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
m := NewModel()
m.Redraw()
eventQueue := make(chan termbox.Event)
go func() {
for {
eventQueue <- termbox.PollEvent()
}
}()
ticker := time.NewTicker(time.Duration(timeInterval) * time.Second)
loop:
for {
select {
case ev := <-eventQueue:
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyCtrlQ:
break loop
}
}
case <-ticker.C:
m.dataPanel.SetData(domain.ReadBatteryStatus())
m.Redraw()
}
}
}
func NewModel() Model {
vw, vh := termbox.Size()
m := Model{
vw: vw,
vh: vh,
statusBar: ui.NewStatusBar(vw, " BatMon "+ver, termbox.ColorBlack, termbox.ColorCyan),
dataPanel: ui.NewDataPanel(2, 2, domain.ReadBatteryStatus(), termbox.ColorDefault, termbox.ColorDefault),
}
return m
}
type Model struct {
vw int
vh int
statusBar ui.StatusBar
dataPanel ui.DataPanel
}
func (m *Model) Redraw() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
m.statusBar.Redraw()
m.dataPanel.Redraw()
termbox.Flush()
}