-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (63 loc) · 1.16 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
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"system/src/cli"
"system/src/sys"
"time"
ui "github.com/gizak/termui/v3"
)
var c sys.CPU
var p sys.Proc
var m sys.Memory
var d sys.Disk
var s sys.Swap
var n sys.Network
var grid ui.Grid
var flg cli.Flags
var gp cli.Graph
func main() {
flags := flg.ManArgs()
if flg.Contains(flags, "help") {
return
}
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
width, height := ui.TerminalDimensions()
grid := ui.NewGrid()
grid.SetRect(0, 0, width, height)
grid = gp.Define(flags, grid)
ui.Render(grid)
ctx, cancel := context.WithCancel(context.Background())
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case sig := <-sig:
log.Printf("Received signal: %v", sig)
cancel()
case <-ctx.Done():
}
}()
uiEvents := ui.PollEvents()
for {
select {
case e := <-uiEvents:
if e.Type == ui.KeyboardEvent && e.ID == "q" {
ui.Close()
os.Exit(0)
return
}
case <-ctx.Done():
time.Sleep(100 * time.Millisecond)
ui.Close()
os.Exit(0)
return
}
}
}