-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
308 lines (271 loc) · 7.21 KB
/
ui.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"strconv"
ui "github.com/gizak/termui"
)
// createHostList display system informations about the host
func createHostList() *ui.List {
host, _ := getHostStat()
l := ui.NewList()
l.BorderLabel = "Host "
l.Items = []string{
"[Hostname ](fg-cyan)" + host.hostname,
"[Domain ](fg-cyan)" + host.domainname,
"[OS ](fg-cyan)" + host.os,
"[OS version ](fg-cyan)" + host.osRelease,
"[Platform ](fg-cyan)" + host.platform,
"[Platform version ](fg-cyan)" + host.platformVersion,
"[Architecture ](fg-cyan)" + host.arch,
"[Uptime ](fg-cyan)",
}
l.Width = 39
l.Height = 10
l.X = 0
l.Y = 0
return l
}
// updateHostList/ update the host informations
func updateHostList(g *ui.List) {
uptime, _ := getUptime()
g.Items[7] = "[Uptime ](fg-cyan)" + uptime
}
// createCPUList display informations about the CPUs
func createCPUList() *ui.List {
cpu, _ := getCPUStat()
l := ui.NewList()
l.BorderLabel = "CPU "
l.Items = []string{
"[CPUs ](fg-cyan)" + cpu.count,
"[Vendor ](fg-cyan)" + cpu.vendorID,
"[Model ](fg-cyan)" + cpu.modelName, //TODO use refreshing rate to display roll long text ?
"[Frequency ](fg-cyan)" + cpu.mhz + " Mhz",
"[Temperature ](fg-cyan)", //TODO CPU temperature
}
l.Width = 39
l.Height = 7
l.X = 0
l.Y = 10
return l
}
// updateCPUList update the CPU informations
func updateCPUList(g *ui.List) {
//uptime := getCPUtemp() //TODO CPU temperature
g.Items[4] = "[Temperature ](fg-cyan)" + "--"
}
// createBIOSList display bios and motherboard informations
func createBIOSList() *ui.List {
bios, _ := getBIOSStat()
l := ui.NewList()
l.BorderLabel = "BIOS "
l.Items = []string{
"[Motherboard ](fg-cyan)" + bios.boardName,
"[Vendor ](fg-cyan)" + bios.boardVendor,
"[BIOS ](fg-cyan)" + bios.biosVendor,
"[Version ](fg-cyan)" + bios.biosVersion + " " + bios.biosDate,
}
l.Width = 39
l.Height = 6
l.X = 0
l.Y = 17
return l
}
// createNetList display information about the global network activity (all interfaces)
func createNetList() *ui.List { //TODO move over ramGauge
l := ui.NewList()
l.BorderLabel = "Network "
l.Items = []string{
"[Up ](fg-cyan)",
"[Down ](fg-cyan)",
}
l.Width = 19
l.Height = 4
l.X = 40
l.Y = 0
return l
}
// NetworkOld keep track of network traffic up & down
var NetworkOld NetStat //TODO get rid of this globals
// updateNetList update network informations
func updateNetList(g *ui.List) {
net, _ := getNetStat()
up := net.up - NetworkOld.up
down := net.down - NetworkOld.down
NetworkOld = net
g.Items[0] = "[Up ](fg-cyan)" + strconv.FormatFloat(up, 'f', 1, 64) + " KiB"
g.Items[1] = "[Down ](fg-cyan)" + strconv.FormatFloat(down, 'f', 1, 64) + " KiB"
}
// createProcList display information about the processes
func createProcList() *ui.List { //TODO move over ramGauge
l := ui.NewList()
l.BorderLabel = "Processes "
l.Items = []string{
"[Tasks ](fg-cyan)",
"[Running ](fg-cyan)",
}
l.Width = 19
l.Height = 4
l.X = 60
l.Y = 0
return l
}
// updateProcList update processes informations
func updateProcList(g *ui.List) {
procs, _ := getProcStat()
g.Items[0] = "[Tasks ](fg-cyan)" + procs.total
g.Items[1] = "[Running ](fg-cyan)" + procs.running
}
// createRamGauge display physical RAM usage informations
func createRAMGauge() *ui.Gauge {
g := ui.NewGauge()
g.BorderLabel = "RAM usage "
g.BarColor = ui.ColorBlue
g.Width = 39
g.Height = 3
g.X = 40
g.Y = 4
return g
}
// updateRAMGauge update the percentage and label of the RAM gauge
func updateRAMGauge(g *ui.Gauge) {
ram, _ := getRAMStat()
g.Percent = ram.usedPercent
g.Label = "{{percent}}% - " + ram.used + "/" + ram.total + " GiB"
}
// createSwapGauge display physical swap usage informations
func createSwapGauge() *ui.Gauge {
g := ui.NewGauge()
g.BorderLabel = "Swap usage "
g.BarColor = ui.ColorBlue
g.Width = 39
g.Height = 3
g.X = 40
g.Y = 7
return g
}
// updateSwapGauge update the percentage and label of the Swap gauge
func updateSwapGauge(g *ui.Gauge) {
swap, _ := getSwapStat()
g.Percent = swap.usedPercent
g.Label = "{{percent}}% - " + swap.used + "/" + swap.total + " GiB"
}
// createCPUGauge display information about the CPU usage
func createCPUGauge() *ui.Gauge {
g := ui.NewGauge()
g.BorderLabel = "CPU usage "
g.BarColor = ui.ColorBlue
g.Width = 39
g.Height = 3
g.X = 40
g.Y = 10
return g
}
// updateRAMGauge update the percentage of the CPU gauge
func updateCPUGauge(g *ui.Gauge) {
g.Percent, _ = getCPUPercent()
}
// createDiskGauges display informations about the physical disks
// up to 3 disks. return an arrau of termui.Gauge
func createDiskGauge() []*ui.Gauge {
disk, _ := getDiskStat()
g := make([]*ui.Gauge, 3) // display 3 disks max
for i := range disk {
if i >= 3 { // display 3 disk max
break
}
g[i] = ui.NewGauge()
g[i].BarColor = ui.ColorBlue
g[i].Width = 39
g[i].Height = 3
g[i].X = 40
g[i].Y = 14 + (i * 3)
}
return g
}
// updateRAMGauge update the percentages of the disk gauges
func updateDiskGauge(g []*ui.Gauge) { //BUG tyu crash when mounting an external disk
disk, _ := getDiskStat()
for i := range disk {
if i >= 3 { // display 3 disk max
break
}
g[i].BorderLabel = disk[i].device + " disk usage "
g[i].Percent = disk[i].usedPercent
g[i].Label = "{{percent}}% - " + disk[i].used + "/" + disk[i].total + " GiB"
}
}
// createQuitPar display a quit help text
func createQuitPar() *ui.Par {
p := ui.NewPar("[ Type 'q' to exit ](fg-white,bg-blue)")
p.Height = 1
p.Width = 20
p.X = 0
p.Y = 23
p.Border = false
return p
}
// init the termui dashboard
func dashboard() {
// init the dashboard
if err := ui.Init(); err != nil {
panic(err) //TODO do not panic but manage the error
}
defer ui.Close()
// inits the dashboard widgets
hostList := createHostList()
cpuList := createCPUList()
biosList := createBIOSList()
netList := createNetList()
procList := createProcList()
ramGauge := createRAMGauge()
swapGauge := createSwapGauge()
cpuGauge := createCPUGauge()
diskGauge := createDiskGauge()
quitPar := createQuitPar()
// render the dashboard with 26x80 fixed size
draw := func(t int) {
// updates the dashboard widgets
updateHostList(hostList)
updateCPUList(cpuList)
updateNetList(netList)
updateProcList(procList)
updateRAMGauge(ramGauge)
updateSwapGauge(swapGauge)
updateCPUGauge(cpuGauge)
updateDiskGauge(diskGauge)
// register the dashboard widgets (except the disks)
ui.Render(
hostList,
cpuList,
biosList,
netList,
procList,
ramGauge,
swapGauge,
cpuGauge,
quitPar,
)
// register the dashboard disk gauges widgets
disk, _ := getDiskStat() // TODO try to avoid this check
if len(disk) >= 1 { // one disk or more
ui.Render(diskGauge[0])
}
if len(disk) >= 2 { // two disks or more
ui.Render(diskGauge[1])
}
if len(disk) == 3 { // three disks
ui.Render(diskGauge[2])
}
}
// setup the events handlers
ui.Handle("/sys/kbd/q", func(ui.Event) { // quit on `q` keystroke
ui.StopLoop()
})
ui.Handle("/sys/kbd/C-c", func(ui.Event) { // quit on `CTRL+c` keystroke
ui.StopLoop()
})
ui.Handle("/timer/1s", func(e ui.Event) { // refresh every second
t := e.Data.(ui.EvtTimer)
draw(int(t.Count))
})
ui.Loop()
}