forked from cjbassi/gotop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
335 lines (286 loc) · 6.49 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"sort"
"strconv"
"sync"
"syscall"
"time"
"github.com/cjbassi/gotop/colorschemes"
w "github.com/cjbassi/gotop/src/widgets"
ui "github.com/cjbassi/termui"
"github.com/docopt/docopt-go"
)
var version = "1.5.0"
var (
termResized = make(chan bool, 1)
helpToggled = make(chan bool, 1)
helpVisible = false
wg sync.WaitGroup
// used to render the proc widget whenever a key is pressed for it
keyPressed = make(chan bool, 1)
// used to render cpu and mem when zoom has changed
zoomed = make(chan bool, 1)
colorscheme = colorschemes.Default
minimal = false
widgetCount = 6
interval = time.Second
zoom = 7
zoomInterval = 3
averageLoad = false
percpuLoad = false
cpu *w.CPU
mem *w.Mem
proc *w.Proc
net *w.Net
disk *w.Disk
temp *w.Temp
help *w.HelpMenu
)
func cliArguments() {
usage := `
Usage: gotop [options]
Options:
-c, --color=NAME Set a colorscheme.
-h, --help Show this screen.
-m, --minimal Only show CPU, Mem and Process widgets.
-r, --rate=RATE Number of times per second to update CPU and Mem widgets [default: 1].
-v, --version Show version.
-p, --percpu Show each CPU in the CPU widget.
-a, --averagecpu Show average CPU in the CPU widget.
Colorschemes:
default
default-dark (for white background)
solarized
monokai
`
args, _ := docopt.ParseArgs(usage, os.Args[1:], version)
if val, _ := args["--color"]; val != nil {
handleColorscheme(val.(string))
}
minimal, _ = args["--minimal"].(bool)
if minimal {
widgetCount = 3
}
rateStr, _ := args["--rate"].(string)
rate, _ := strconv.ParseFloat(rateStr, 64)
if rate < 1 {
interval = time.Second * time.Duration(1/rate)
} else {
interval = time.Second / time.Duration(rate)
}
averageLoad, _ = args["--averagecpu"].(bool)
percpuLoad, _ = args["--percpu"].(bool)
}
func handleColorscheme(cs string) {
switch cs {
case "default":
colorscheme = colorschemes.Default
case "solarized":
colorscheme = colorschemes.Solarized
case "monokai":
colorscheme = colorschemes.Monokai
case "default-dark":
colorscheme = colorschemes.DefaultDark
default:
colorscheme = getCustomColorscheme(cs)
}
}
// getCustomColorscheme tries to read a custom json colorscheme from
// {$XDG_CONFIG_HOME,~/.config}/gotop/{name}.json
func getCustomColorscheme(name string) colorschemes.Colorscheme {
xdg := os.Getenv("XDG_CONFIG_HOME")
if xdg == "" {
xdg = os.ExpandEnv("$HOME") + "/.config"
}
file := xdg + "/gotop/" + name + ".json"
dat, err := ioutil.ReadFile(file)
if err != nil {
fmt.Fprintf(os.Stderr, "error: colorscheme not recognized\n")
os.Exit(1)
}
var colorscheme colorschemes.Colorscheme
err = json.Unmarshal(dat, &colorscheme)
if err != nil {
fmt.Fprintf(os.Stderr, "error: could not parse colorscheme\n")
os.Exit(1)
}
return colorscheme
}
func setupGrid() {
ui.Body.Cols = 12
ui.Body.Rows = 12
if minimal {
ui.Body.Set(0, 0, 12, 6, cpu)
ui.Body.Set(0, 6, 6, 12, mem)
ui.Body.Set(6, 6, 12, 12, proc)
} else {
ui.Body.Set(0, 0, 12, 4, cpu)
ui.Body.Set(0, 4, 4, 6, disk)
ui.Body.Set(0, 6, 4, 8, temp)
ui.Body.Set(4, 4, 12, 8, mem)
ui.Body.Set(0, 8, 6, 12, net)
ui.Body.Set(6, 8, 12, 12, proc)
}
}
func keyBinds() {
// quits
ui.On("q", "<C-c>", func(e ui.Event) {
ui.StopLoop()
})
// toggles help menu
ui.On("?", func(e ui.Event) {
helpToggled <- true
helpVisible = !helpVisible
})
// hides help menu
ui.On("<escape>", func(e ui.Event) {
if helpVisible {
helpToggled <- true
helpVisible = false
}
})
ui.On("h", func(e ui.Event) {
zoom += zoomInterval
cpu.Zoom = zoom
mem.Zoom = zoom
zoomed <- true
})
ui.On("l", func(e ui.Event) {
if zoom > zoomInterval {
zoom -= zoomInterval
cpu.Zoom = zoom
mem.Zoom = zoom
zoomed <- true
}
})
}
func termuiColors() {
ui.Theme.Fg = ui.Color(colorscheme.Fg)
ui.Theme.Bg = ui.Color(colorscheme.Bg)
ui.Theme.LabelFg = ui.Color(colorscheme.BorderLabel)
ui.Theme.LabelBg = ui.Color(colorscheme.Bg)
ui.Theme.BorderFg = ui.Color(colorscheme.BorderLine)
ui.Theme.BorderBg = ui.Color(colorscheme.Bg)
ui.Theme.TableCursor = ui.Color(colorscheme.ProcCursor)
ui.Theme.Sparkline = ui.Color(colorscheme.Sparkline)
ui.Theme.GaugeColor = ui.Color(colorscheme.DiskBar)
}
func widgetColors() {
mem.LineColor["Main"] = ui.Color(colorscheme.MainMem)
mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem)
var keys []string
for key := range cpu.Data {
keys = append(keys, key)
}
sort.Strings(keys)
i := 0
for _, v := range keys {
if i >= len(colorscheme.CPULines) {
// assuming colorscheme for CPU lines is not empty
i = 0
}
c := colorscheme.CPULines[i]
cpu.LineColor[v] = ui.Color(c)
i++
}
if !minimal {
temp.TempLow = ui.Color(colorscheme.TempLow)
temp.TempHigh = ui.Color(colorscheme.TempHigh)
}
}
// load widgets asynchronously but wait till they are all finished
func initWidgets() {
wg.Add(widgetCount)
go func() {
cpu = w.NewCPU(interval, zoom, averageLoad, percpuLoad)
wg.Done()
}()
go func() {
mem = w.NewMem(interval, zoom)
wg.Done()
}()
go func() {
proc = w.NewProc(keyPressed)
wg.Done()
}()
if !minimal {
go func() {
net = w.NewNet()
wg.Done()
}()
go func() {
disk = w.NewDisk()
wg.Done()
}()
go func() {
temp = w.NewTemp()
wg.Done()
}()
}
wg.Wait()
}
func main() {
cliArguments()
keyBinds()
// need to do this before initializing widgets so that they can inherit the colors
termuiColors()
initWidgets()
widgetColors()
help = w.NewHelpMenu()
// inits termui
err := ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
setupGrid()
ui.On("<resize>", func(e ui.Event) {
ui.Body.Width, ui.Body.Height = e.Width, e.Height
ui.Body.Resize()
termResized <- true
})
// all rendering done here
go func() {
ui.Render(ui.Body)
drawTick := time.NewTicker(interval)
for {
if helpVisible {
select {
case <-helpToggled:
ui.Render(ui.Body)
case <-termResized:
ui.Clear()
ui.Render(help)
}
} else {
select {
case <-helpToggled:
ui.Clear()
ui.Render(help)
case <-termResized:
ui.Clear()
ui.Render(ui.Body)
case <-keyPressed:
ui.Render(proc)
case <-zoomed:
ui.Render(cpu, mem)
case <-drawTick.C:
ui.Render(ui.Body)
}
}
}
}()
// handles kill signal sent to gotop
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
ui.StopLoop()
}()
ui.Loop()
}