-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (64 loc) · 1.88 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
package main
import (
"log"
"time"
"github.com/ashishkhuraishy/memstat/pkg"
ui "github.com/gizak/termui/v3"
)
func main() {
Run()
}
// The run function is responsible for initialising and rendering
// the ui on the screen and updating it on each second.
func Run() {
// Initialising the ui package
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialise terminal : %v", err)
}
defer ui.Close()
// Initialising a new controller which stores the data
// for rendering and resizing our ui
controller := pkg.NewController()
// `events` will be a channel which contains data for
// each time a UI event occurs (like mouse click, keyboard
// typed, resized). Here we will listean to the values from
// the channel to quit and redraw the ui each time user
// resizes the terminal
events := ui.PollEvents()
// `tick` is also a channel which will trigger each second
// so that we can use that to update our on screen data and
// display new data every second
tick := time.Tick(time.Second)
// Starting an infinte loop which will listean for
// events and time tiker
for {
select {
// This will listean for every events that occur
// related to the UI
case event := <-events:
switch event.Type {
case ui.KeyboardEvent:
// If the user clicks <CTRL+c> then the
// loop will break and the program will
// be executed
if event.ID == "<C-c>" {
return
}
// When the user resizes the terminal, we are
// triggering the controller to redraw the UI,
// so that it'll be responsive
case ui.ResizeEvent:
controller.Resize()
}
// Here the ticker will trigger every second and
// we are getting the current status from the
// system , then using that data to render a new
// UI state.
case <-tick:
// Loading the status
stat := pkg.LoadStats()
// rendering the new state with new data
controller.Render(stat)
}
}
}