-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (72 loc) · 1.4 KB
/
main.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
86
87
88
package main
import (
"time"
tui "github.com/gizak/termui/v3"
"github.com/sunghun7511/gotop/widgets"
)
var (
cpuWidget widgets.Widget
memoryWidget widgets.Widget
processWidget widgets.Widget
endChan chan struct{}
)
func initWidgets() {
cpuWidget = widgets.NewCpuWidget()
memoryWidget = widgets.NewMemoryWidget()
processWidget = widgets.NewProcessWidget()
}
func render() {
grid := tui.NewGrid()
termWidth, termHeight := tui.TerminalDimensions()
grid.SetRect(0, 0, termWidth, termHeight)
grid.Set(
tui.NewRow(1.0/2, cpuWidget.GetUI()),
tui.NewRow(1.0/2,
tui.NewCol(1.0/2, memoryWidget.GetUI()),
tui.NewCol(1.0/2, processWidget.GetUI()),
),
)
tui.Render(grid)
}
func handleSignal(e tui.Event) {
if e.ID == "q" || e.ID == "<C-c>" {
endChan <- struct{}{}
return
}
cpuWidget.HandleSignal(e)
memoryWidget.HandleSignal(e)
processWidget.HandleSignal(e)
}
func updateWidgets() {
cpuWidget.Update()
memoryWidget.Update()
processWidget.Update()
}
func handleEvents() {
go func() {
uiEvents := tui.PollEvents()
for {
e := <-uiEvents
handleSignal(e)
render()
}
}()
go func() {
updateStatTicker := time.NewTicker(1 * time.Second)
for {
<-updateStatTicker.C
updateWidgets()
render()
}
}()
}
func main() {
if err := tui.Init(); err != nil {
panic(err)
}
defer tui.Close()
initWidgets()
handleEvents()
endChan = make(chan struct{})
<-endChan
}