gotop/main.go

313 lines
5.9 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package main
import (
2018-02-21 18:24:36 +08:00
"fmt"
2018-02-19 15:25:02 +08:00
"os"
"os/signal"
2018-08-17 08:05:21 +08:00
"sort"
2018-03-09 16:27:46 +08:00
"strconv"
2018-04-13 23:54:20 +08:00
"sync"
2018-02-19 15:25:02 +08:00
"syscall"
"time"
2018-05-11 12:23:20 +08:00
"github.com/cjbassi/gotop/src/colorschemes"
w "github.com/cjbassi/gotop/src/widgets"
2018-03-30 06:48:43 +08:00
ui "github.com/cjbassi/termui"
2018-02-19 15:25:02 +08:00
"github.com/docopt/docopt-go"
)
2018-08-24 11:01:58 +08:00
var version = "1.4.2"
2018-02-19 15:25:02 +08:00
var (
2018-03-04 09:05:52 +08:00
termResized = make(chan bool, 1)
2018-02-19 15:25:02 +08:00
helpToggled = make(chan bool, 1)
2018-03-04 09:05:52 +08:00
helpVisible = false
2018-02-19 15:25:02 +08:00
2018-04-14 00:45:24 +08:00
wg sync.WaitGroup
2018-02-24 13:15:38 +08:00
// used to render the proc widget whenever a key is pressed for it
2018-02-19 15:25:02 +08:00
keyPressed = make(chan bool, 1)
2018-03-10 08:29:05 +08:00
// used to render cpu and mem when zoom has changed
zoomed = make(chan bool, 1)
2018-02-19 15:25:02 +08:00
2018-02-22 09:49:47 +08:00
colorscheme = colorschemes.Default
2018-02-19 15:25:02 +08:00
2018-03-10 08:29:05 +08:00
minimal = false
2018-04-13 10:43:17 +08:00
widgetCount = 6
2018-03-10 08:29:05 +08:00
interval = time.Second
zoom = 7
zoomInterval = 3
2018-03-09 15:51:03 +08:00
2018-08-01 05:40:51 +08:00
averageLoad = false
2018-08-17 07:03:29 +08:00
percpuLoad = false
2018-08-01 05:24:44 +08:00
2018-02-21 18:24:36 +08:00
cpu *w.CPU
mem *w.Mem
proc *w.Proc
net *w.Net
disk *w.Disk
temp *w.Temp
help *w.HelpMenu
2018-02-19 15:25:02 +08:00
)
2018-02-24 13:15:38 +08:00
func cliArguments() {
2018-02-19 15:25:02 +08:00
usage := `
Usage: gotop [options]
Options:
2018-03-09 16:37:10 +08:00
-c, --color=NAME Set a colorscheme.
2018-03-09 15:51:03 +08:00
-h, --help Show this screen.
-m, --minimal Only show CPU, Mem and Process widgets.
2018-03-09 16:27:46 +08:00
-r, --rate=RATE Number of times per second to update CPU and Mem widgets [default: 1].
2018-02-21 18:24:36 +08:00
-v, --version Show version.
2018-08-17 07:03:29 +08:00
-p, --percpu Show each CPU in the CPU widget.
-a, --averagecpu Show average CPU in the CPU widget.
2018-02-19 15:25:02 +08:00
Colorschemes:
default
2018-04-10 13:00:27 +08:00
default-dark (for white background)
2018-02-21 18:24:36 +08:00
solarized
2018-02-22 11:54:25 +08:00
monokai
2018-02-19 15:25:02 +08:00
`
args, _ := docopt.ParseArgs(usage, os.Args[1:], version)
2018-02-21 18:24:36 +08:00
if val, _ := args["--color"]; val != nil {
handleColorscheme(val.(string))
}
2018-03-09 15:51:03 +08:00
minimal, _ = args["--minimal"].(bool)
2018-04-13 10:43:17 +08:00
if minimal {
widgetCount = 3
}
2018-03-09 16:27:46 +08:00
rateStr, _ := args["--rate"].(string)
2018-03-09 16:34:26 +08:00
rate, _ := strconv.ParseFloat(rateStr, 64)
if rate < 1 {
interval = time.Second * time.Duration(1/rate)
} else {
interval = time.Second / time.Duration(rate)
}
2018-08-17 07:03:29 +08:00
averageLoad, _ = args["--averagecpu"].(bool)
percpuLoad, _ = args["--percpu"].(bool)
2018-02-21 18:24:36 +08:00
}
func handleColorscheme(cs string) {
switch cs {
case "default":
2018-02-22 09:49:47 +08:00
colorscheme = colorschemes.Default
2018-04-10 13:00:27 +08:00
case "solarized":
colorscheme = colorschemes.Solarized
case "monokai":
colorscheme = colorschemes.Monokai
case "default-dark":
colorscheme = colorschemes.DefaultDark
2018-02-21 18:24:36 +08:00
default:
fmt.Fprintf(os.Stderr, "error: colorscheme not recognized\n")
os.Exit(1)
2018-02-19 15:25:02 +08:00
}
}
func setupGrid() {
ui.Body.Cols = 12
ui.Body.Rows = 12
2018-03-09 15:51:03 +08:00
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)
2018-02-19 15:25:02 +08:00
2018-03-09 15:51:03 +08:00
ui.Body.Set(0, 4, 4, 6, disk)
ui.Body.Set(0, 6, 4, 8, temp)
ui.Body.Set(4, 4, 12, 8, mem)
2018-02-19 15:25:02 +08:00
2018-03-09 15:51:03 +08:00
ui.Body.Set(0, 8, 6, 12, net)
ui.Body.Set(6, 8, 12, 12, proc)
}
2018-02-19 15:25:02 +08:00
}
func keyBinds() {
// quits
ui.On("q", "<C-c>", func(e ui.Event) {
2018-02-19 15:25:02 +08:00
ui.StopLoop()
})
// toggles help menu
ui.On("?", func(e ui.Event) {
helpToggled <- true
2018-03-04 09:05:52 +08:00
helpVisible = !helpVisible
2018-02-19 15:25:02 +08:00
})
// hides help menu
ui.On("<escape>", func(e ui.Event) {
2018-03-04 09:05:52 +08:00
if helpVisible {
2018-02-19 15:25:02 +08:00
helpToggled <- true
2018-03-04 09:05:52 +08:00
helpVisible = false
2018-02-19 15:25:02 +08:00
}
})
2018-03-10 08:29:05 +08:00
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
}
})
2018-02-19 15:25:02 +08:00
}
2018-02-21 18:24:36 +08:00
func termuiColors() {
2018-02-22 03:15:53 +08:00
ui.Theme.Fg = ui.Color(colorscheme.Fg)
2018-02-21 18:24:36 +08:00
ui.Theme.Bg = ui.Color(colorscheme.Bg)
2018-02-22 03:15:53 +08:00
ui.Theme.LabelFg = ui.Color(colorscheme.BorderLabel)
2018-02-21 18:24:36 +08:00
ui.Theme.LabelBg = ui.Color(colorscheme.Bg)
2018-02-22 03:15:53 +08:00
ui.Theme.BorderFg = ui.Color(colorscheme.BorderLine)
ui.Theme.BorderBg = ui.Color(colorscheme.Bg)
2018-02-21 18:24:36 +08:00
ui.Theme.TableCursor = ui.Color(colorscheme.ProcCursor)
ui.Theme.Sparkline = ui.Color(colorscheme.Sparkline)
ui.Theme.GaugeColor = ui.Color(colorscheme.DiskBar)
2018-02-21 18:24:36 +08:00
}
func widgetColors() {
mem.LineColor["Main"] = ui.Color(colorscheme.MainMem)
mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem)
2018-08-17 08:05:21 +08:00
var keys []string
for key := range cpu.Data {
keys = append(keys, key)
}
sort.Strings(keys)
2018-08-01 05:24:44 +08:00
i := 0
2018-08-17 08:05:21 +08:00
for _, v := range keys {
2018-08-01 05:24:44 +08:00
if i >= len(colorscheme.CPULines) {
// assuming colorscheme for CPU lines is not empty
i = 0
}
2018-08-01 05:24:44 +08:00
c := colorscheme.CPULines[i]
2018-08-17 08:05:21 +08:00
cpu.LineColor[v] = ui.Color(c)
2018-08-01 05:24:44 +08:00
i++
2018-02-21 18:24:36 +08:00
}
2018-03-09 15:51:03 +08:00
if !minimal {
temp.TempLow = ui.Color(colorscheme.TempLow)
temp.TempHigh = ui.Color(colorscheme.TempHigh)
}
2018-02-19 15:25:02 +08:00
}
2018-04-14 00:45:24 +08:00
// load widgets asynchronously but wait till they are all finished
2018-04-13 23:54:20 +08:00
func initWidgets() {
wg.Add(widgetCount)
2018-02-24 13:15:38 +08:00
2018-04-13 10:43:17 +08:00
go func() {
2018-08-01 05:24:44 +08:00
cpu = w.NewCPU(interval, zoom, averageLoad, percpuLoad)
2018-04-29 10:30:09 +08:00
wg.Done()
2018-04-13 10:43:17 +08:00
}()
go func() {
mem = w.NewMem(interval, zoom)
2018-04-29 10:30:09 +08:00
wg.Done()
2018-04-13 10:43:17 +08:00
}()
go func() {
proc = w.NewProc(keyPressed)
2018-04-29 10:30:09 +08:00
wg.Done()
2018-04-13 10:43:17 +08:00
}()
2018-03-09 15:51:03 +08:00
if !minimal {
2018-04-13 10:43:17 +08:00
go func() {
net = w.NewNet()
2018-04-29 10:30:09 +08:00
wg.Done()
2018-04-13 10:43:17 +08:00
}()
go func() {
disk = w.NewDisk()
2018-04-29 10:30:09 +08:00
wg.Done()
2018-04-13 10:43:17 +08:00
}()
go func() {
temp = w.NewTemp()
2018-04-29 10:30:09 +08:00
wg.Done()
2018-04-13 10:43:17 +08:00
}()
}
2018-04-13 23:54:20 +08:00
wg.Wait()
}
func main() {
cliArguments()
keyBinds()
// need to do this before initializing widgets so that they can inherit the colors
termuiColors()
initWidgets()
2018-02-21 18:24:36 +08:00
widgetColors()
2018-04-13 10:48:50 +08:00
help = w.NewHelpMenu()
2018-02-24 13:15:38 +08:00
// inits termui
2018-02-19 15:25:02 +08:00
err := ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
setupGrid()
ui.On("<resize>", func(e ui.Event) {
2018-02-19 15:25:02 +08:00
ui.Body.Width, ui.Body.Height = e.Width, e.Height
ui.Body.Resize()
2018-02-21 19:41:40 +08:00
2018-03-04 09:05:52 +08:00
termResized <- true
2018-02-19 15:25:02 +08:00
})
2018-03-04 09:05:52 +08:00
// all rendering done here
2018-02-19 15:25:02 +08:00
go func() {
ui.Render(ui.Body)
2018-03-09 16:27:46 +08:00
drawTick := time.NewTicker(interval)
2018-02-19 15:25:02 +08:00
for {
if helpVisible {
select {
case <-helpToggled:
2018-02-19 15:25:02 +08:00
ui.Render(ui.Body)
case <-termResized:
2018-02-19 15:25:02 +08:00
ui.Clear()
ui.Render(help)
}
} else {
select {
case <-helpToggled:
ui.Clear()
ui.Render(help)
case <-termResized:
ui.Clear()
2018-03-10 08:29:05 +08:00
ui.Render(ui.Body)
case <-keyPressed:
ui.Render(proc)
case <-zoomed:
ui.Render(cpu, mem)
case <-drawTick.C:
2018-02-19 15:25:02 +08:00
ui.Render(ui.Body)
}
}
}
}()
2018-04-15 00:01:15 +08:00
// handles kill signal sent to gotop
2018-02-19 15:25:02 +08:00
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
ui.StopLoop()
}()
ui.Loop()
}