forked from cjbassi/gotop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
460 lines (409 loc) · 10.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"sort"
"strconv"
"syscall"
"time"
docopt "github.com/docopt/docopt.go"
ui "github.com/gizak/termui/v3"
"github.com/cjbassi/gotop/colorschemes"
"github.com/cjbassi/gotop/src/logging"
"github.com/cjbassi/gotop/src/utils"
w "github.com/cjbassi/gotop/src/widgets"
)
const (
appName = "gotop"
version = "3.0.0"
graphHorizontalScaleDelta = 3
)
var (
configDir = utils.GetConfigDir(appName)
logDir = utils.GetLogDir(appName)
logPath = filepath.Join(logDir, "errors.log")
stderrLogger = log.New(os.Stderr, "", 0)
graphHorizontalScale = 7
helpVisible = false
colorscheme = colorschemes.Default
updateInterval = time.Second
minimalMode = false
averageLoad = false
percpuLoad = false
tempScale = w.Celcius
battery = false
statusbar = false
cpu *w.CpuWidget
batt *w.BatteryWidget
mem *w.MemWidget
proc *w.ProcWidget
net *w.NetWidget
disk *w.DiskWidget
temp *w.TempWidget
help *w.HelpMenu
grid *ui.Grid
bar *w.StatusBar
)
func parseArgs() error {
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 Print version and exit.
-p, --percpu Show each CPU in the CPU widget.
-a, --averagecpu Show average CPU in the CPU widget.
-f, --fahrenheit Show temperatures in fahrenheit.
-s, --statusbar Show a statusbar with the time.
-b, --battery Show battery level widget ('minimal' turns off).
Colorschemes:
default
default-dark (for white background)
solarized
monokai
vice
`
args, err := docopt.ParseArgs(usage, os.Args[1:], version)
if err != nil {
return err
}
if val, _ := args["--color"]; val != nil {
if err := handleColorscheme(val.(string)); err != nil {
return err
}
}
averageLoad, _ = args["--averagecpu"].(bool)
percpuLoad, _ = args["--percpu"].(bool)
battery, _ = args["--battery"].(bool)
minimalMode, _ = args["--minimal"].(bool)
statusbar, _ = args["--statusbar"].(bool)
rateStr, _ := args["--rate"].(string)
rate, err := strconv.ParseFloat(rateStr, 64)
if err != nil {
return fmt.Errorf("invalid rate parameter")
}
if rate < 1 {
updateInterval = time.Second * time.Duration(1/rate)
} else {
updateInterval = time.Second / time.Duration(rate)
}
fahrenheit, _ := args["--fahrenheit"].(bool)
if fahrenheit {
tempScale = w.Fahrenheit
}
return nil
}
func handleColorscheme(cs string) error {
switch cs {
case "default":
colorscheme = colorschemes.Default
case "solarized":
colorscheme = colorschemes.Solarized
case "monokai":
colorscheme = colorschemes.Monokai
case "vice":
colorscheme = colorschemes.Vice
case "default-dark":
colorscheme = colorschemes.DefaultDark
default:
custom, err := getCustomColorscheme(cs)
if err != nil {
return err
}
colorscheme = custom
}
return nil
}
// getCustomColorscheme tries to read a custom json colorscheme from <configDir>/<name>.json
func getCustomColorscheme(name string) (colorschemes.Colorscheme, error) {
var colorscheme colorschemes.Colorscheme
filePath := filepath.Join(configDir, name+".json")
dat, err := ioutil.ReadFile(filePath)
if err != nil {
return colorscheme, fmt.Errorf("failed to read colorscheme file: %v", err)
}
err = json.Unmarshal(dat, &colorscheme)
if err != nil {
return colorscheme, fmt.Errorf("failed to parse colorscheme file: %v", err)
}
return colorscheme, nil
}
func setupGrid() {
grid = ui.NewGrid()
if minimalMode {
grid.Set(
ui.NewRow(1.0/2, cpu),
ui.NewRow(1.0/2,
ui.NewCol(1.0/2, mem),
ui.NewCol(1.0/2, proc),
),
)
} else {
var cpuRow ui.GridItem
if battery {
cpuRow = ui.NewRow(1.0/3,
ui.NewCol(2.0/3, cpu),
ui.NewCol(1.0/3, batt),
)
} else {
cpuRow = ui.NewRow(1.0/3, cpu)
}
grid.Set(
cpuRow,
ui.NewRow(1.0/3,
ui.NewCol(1.0/3,
ui.NewRow(1.0/2, disk),
ui.NewRow(1.0/2, temp),
),
ui.NewCol(2.0/3, mem),
),
ui.NewRow(1.0/3,
ui.NewCol(1.0/2, net),
ui.NewCol(1.0/2, proc),
),
)
}
}
func setDefaultTermuiColors() {
ui.Theme.Default = ui.NewStyle(ui.Color(colorscheme.Fg), ui.Color(colorscheme.Bg))
ui.Theme.Block.Title = ui.NewStyle(ui.Color(colorscheme.BorderLabel), ui.Color(colorscheme.Bg))
ui.Theme.Block.Border = ui.NewStyle(ui.Color(colorscheme.BorderLine), ui.Color(colorscheme.Bg))
}
func setWidgetColors() {
mem.LineColors["Main"] = ui.Color(colorscheme.MainMem)
mem.LineColors["Swap"] = ui.Color(colorscheme.SwapMem)
proc.CursorColor = ui.Color(colorscheme.ProcCursor)
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.LineColors[v] = ui.Color(c)
i++
}
if !minimalMode {
if battery {
var battKeys []string
for key := range batt.Data {
battKeys = append(battKeys, key)
}
sort.Strings(battKeys)
i = 0 // Re-using variable from CPU
for _, v := range battKeys {
if i >= len(colorscheme.BattLines) {
// assuming colorscheme for battery lines is not empty
i = 0
}
c := colorscheme.BattLines[i]
batt.LineColors[v] = ui.Color(c)
i++
}
}
temp.TempLowColor = ui.Color(colorscheme.TempLow)
temp.TempHighColor = ui.Color(colorscheme.TempHigh)
net.Lines[0].LineColor = ui.Color(colorscheme.Sparkline)
net.Lines[0].TitleColor = ui.Color(colorscheme.BorderLabel)
net.Lines[1].LineColor = ui.Color(colorscheme.Sparkline)
net.Lines[1].TitleColor = ui.Color(colorscheme.BorderLabel)
}
}
func initWidgets() {
cpu = w.NewCpuWidget(updateInterval, graphHorizontalScale, averageLoad, percpuLoad)
mem = w.NewMemWidget(updateInterval, graphHorizontalScale)
proc = w.NewProcWidget()
help = w.NewHelpMenu()
if !minimalMode {
if battery {
batt = w.NewBatteryWidget(graphHorizontalScale)
}
net = w.NewNetWidget()
disk = w.NewDiskWidget()
temp = w.NewTempWidget(tempScale)
}
if statusbar {
bar = w.NewStatusBar()
}
}
func eventLoop() {
drawTicker := time.NewTicker(updateInterval).C
// handles kill signal sent to gotop
sigTerm := make(chan os.Signal, 2)
signal.Notify(sigTerm, os.Interrupt, syscall.SIGTERM)
uiEvents := ui.PollEvents()
previousKey := ""
for {
select {
case <-sigTerm:
return
case <-drawTicker:
if !helpVisible {
ui.Render(grid)
if statusbar {
ui.Render(bar)
}
}
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "?":
helpVisible = !helpVisible
case "<Resize>":
payload := e.Payload.(ui.Resize)
termWidth, termHeight := payload.Width, payload.Height
if statusbar {
grid.SetRect(0, 0, termWidth, termHeight-1)
bar.SetRect(0, termHeight-1, termWidth, termHeight)
} else {
grid.SetRect(0, 0, payload.Width, payload.Height)
}
help.Resize(payload.Width, payload.Height)
ui.Clear()
}
if helpVisible {
switch e.ID {
case "?":
ui.Clear()
ui.Render(help)
case "<Escape>":
helpVisible = false
ui.Render(grid)
case "<Resize>":
ui.Render(help)
}
} else {
switch e.ID {
case "?":
ui.Render(grid)
case "h":
graphHorizontalScale += graphHorizontalScaleDelta
cpu.HorizontalScale = graphHorizontalScale
mem.HorizontalScale = graphHorizontalScale
ui.Render(cpu, mem)
case "l":
if graphHorizontalScale > graphHorizontalScaleDelta {
graphHorizontalScale -= graphHorizontalScaleDelta
cpu.HorizontalScale = graphHorizontalScale
mem.HorizontalScale = graphHorizontalScale
ui.Render(cpu, mem)
}
case "<Resize>":
ui.Render(grid)
if statusbar {
ui.Render(bar)
}
case "<MouseLeft>":
payload := e.Payload.(ui.Mouse)
proc.HandleClick(payload.X, payload.Y)
ui.Render(proc)
case "k", "<Up>", "<MouseWheelUp>":
proc.ScrollUp()
ui.Render(proc)
case "j", "<Down>", "<MouseWheelDown>":
proc.ScrollDown()
ui.Render(proc)
case "<Home>":
proc.ScrollTop()
ui.Render(proc)
case "g":
if previousKey == "g" {
proc.ScrollTop()
ui.Render(proc)
}
case "G", "<End>":
proc.ScrollBottom()
ui.Render(proc)
case "<C-d>":
proc.ScrollHalfPageDown()
ui.Render(proc)
case "<C-u>":
proc.ScrollHalfPageUp()
ui.Render(proc)
case "<C-f>":
proc.ScrollPageDown()
ui.Render(proc)
case "<C-b>":
proc.ScrollPageUp()
ui.Render(proc)
case "d":
if previousKey == "d" {
proc.KillProc()
}
case "<Tab>":
proc.ToggleShowingGroupedProcs()
ui.Render(proc)
case "m", "c", "p":
proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID))
ui.Render(proc)
}
if previousKey == e.ID {
previousKey = ""
} else {
previousKey = e.ID
}
}
}
}
}
func setupLogfile() (*os.File, error) {
// create the log directory
if err := os.MkdirAll(logDir, 0755); err != nil {
return nil, fmt.Errorf("failed to make the log directory: %v", err)
}
// open the log file
logfile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0660)
if err != nil {
return nil, fmt.Errorf("failed to open log file: %v", err)
}
// log time, filename, and line number
log.SetFlags(log.Ltime | log.Lshortfile)
// log to file
log.SetOutput(logfile)
return logfile, nil
}
func main() {
if err := parseArgs(); err != nil {
stderrLogger.Fatalf("failed to parse cli args: %v", err)
}
logfile, err := setupLogfile()
if err != nil {
stderrLogger.Fatalf("failed to setup log file: %v", err)
}
defer logfile.Close()
if err := ui.Init(); err != nil {
stderrLogger.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
logging.StderrToLogfile(logfile)
setDefaultTermuiColors() // done before initializing widgets to allow inheriting colors
initWidgets()
setWidgetColors()
setupGrid()
termWidth, termHeight := ui.TerminalDimensions()
if statusbar {
grid.SetRect(0, 0, termWidth, termHeight-1)
} else {
grid.SetRect(0, 0, termWidth, termHeight)
}
help.Resize(termWidth, termHeight)
ui.Render(grid)
if statusbar {
bar.SetRect(0, termHeight-1, termWidth, termHeight)
ui.Render(bar)
}
eventLoop()
}