2018-02-19 15:25:02 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-19 04:42:49 +08:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2018-12-05 13:44:25 +08:00
|
|
|
"log"
|
2018-02-19 15:25:02 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-12-05 13:44:25 +08:00
|
|
|
"path/filepath"
|
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-09-19 04:42:49 +08:00
|
|
|
"github.com/cjbassi/gotop/colorschemes"
|
2018-05-11 12:23:20 +08:00
|
|
|
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-12-02 13:19:10 +08:00
|
|
|
var version = "1.6.0"
|
2018-02-19 15:25:02 +08:00
|
|
|
|
|
|
|
var (
|
2018-11-30 10:17:13 +08:00
|
|
|
colorscheme = colorschemes.Default
|
2018-03-10 08:29:05 +08:00
|
|
|
minimal = false
|
|
|
|
interval = time.Second
|
|
|
|
zoom = 7
|
|
|
|
zoomInterval = 3
|
2018-11-30 10:17:13 +08:00
|
|
|
helpVisible = false
|
|
|
|
averageLoad = false
|
|
|
|
percpuLoad = false
|
|
|
|
widgetCount = 6
|
2018-12-02 13:19:10 +08:00
|
|
|
fahrenheit = false
|
2018-12-05 07:22:41 +08:00
|
|
|
configDir = getConfigDir()
|
2018-12-05 13:44:25 +08:00
|
|
|
logPath = filepath.Join(configDir, "errors.log")
|
|
|
|
stderrLogger = log.New(os.Stderr, "", 0)
|
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-11-14 01:29:09 +08:00
|
|
|
-f, --fahrenheit Show temperatures in fahrenheit.
|
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
|
|
|
`
|
|
|
|
|
2018-12-05 13:44:25 +08:00
|
|
|
args, err := docopt.ParseArgs(usage, os.Args[1:], version)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-02-21 18:24:36 +08:00
|
|
|
|
|
|
|
if val, _ := args["--color"]; val != nil {
|
|
|
|
handleColorscheme(val.(string))
|
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
averageLoad, _ = args["--averagecpu"].(bool)
|
|
|
|
percpuLoad, _ = args["--percpu"].(bool)
|
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-12-05 13:44:25 +08:00
|
|
|
rate, err := strconv.ParseFloat(rateStr, 64)
|
|
|
|
if err != nil {
|
|
|
|
stderrLogger.Fatalf("error: invalid rate parameter")
|
|
|
|
}
|
2018-03-09 16:34:26 +08:00
|
|
|
if rate < 1 {
|
|
|
|
interval = time.Second * time.Duration(1/rate)
|
|
|
|
} else {
|
|
|
|
interval = time.Second / time.Duration(rate)
|
|
|
|
}
|
2018-11-14 01:29:09 +08:00
|
|
|
fahrenheit, _ = args["--fahrenheit"].(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:
|
2018-09-19 04:42:49 +08:00
|
|
|
colorscheme = getCustomColorscheme(cs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 07:22:41 +08:00
|
|
|
func getConfigDir() string {
|
|
|
|
globalConfigDir := os.Getenv("XDG_CONFIG_HOME")
|
|
|
|
if globalConfigDir == "" {
|
2018-12-05 13:44:25 +08:00
|
|
|
globalConfigDir = filepath.Join(os.ExpandEnv("$HOME"), ".config")
|
2018-09-19 04:42:49 +08:00
|
|
|
}
|
2018-12-05 13:44:25 +08:00
|
|
|
return filepath.Join(globalConfigDir, "gotop")
|
2018-12-05 07:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getCustomColorscheme tries to read a custom json colorscheme from {configDir}/{name}.json
|
|
|
|
func getCustomColorscheme(name string) colorschemes.Colorscheme {
|
2018-12-05 13:44:25 +08:00
|
|
|
filePath := filepath.Join(configDir, name+".json")
|
2018-12-05 07:22:41 +08:00
|
|
|
dat, err := ioutil.ReadFile(filePath)
|
2018-09-19 04:42:49 +08:00
|
|
|
if err != nil {
|
2018-12-05 13:44:25 +08:00
|
|
|
stderrLogger.Fatalf("error: colorscheme not recognized")
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2018-09-19 04:42:49 +08:00
|
|
|
var colorscheme colorschemes.Colorscheme
|
|
|
|
err = json.Unmarshal(dat, &colorscheme)
|
|
|
|
if err != nil {
|
2018-12-05 13:44:25 +08:00
|
|
|
stderrLogger.Fatalf("error: could not parse colorscheme")
|
2018-09-19 04:42:49 +08:00
|
|
|
}
|
|
|
|
return colorscheme
|
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
|
|
|
}
|
|
|
|
|
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)
|
2018-02-25 13:35:57 +08:00
|
|
|
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-03-28 05:00:32 +08:00
|
|
|
}
|
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-02-25 13:35:57 +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-13 23:54:20 +08:00
|
|
|
func initWidgets() {
|
2018-11-30 10:17:13 +08:00
|
|
|
var wg sync.WaitGroup
|
2018-04-13 23:54:20 +08:00
|
|
|
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() {
|
2018-11-30 10:17:13 +08:00
|
|
|
proc = w.NewProc()
|
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() {
|
2018-11-14 01:29:09 +08:00
|
|
|
temp = w.NewTemp(fahrenheit)
|
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()
|
|
|
|
}
|
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
func eventLoop() {
|
|
|
|
drawTicker := time.NewTicker(interval).C
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
// handles kill signal sent to gotop
|
|
|
|
sigTerm := make(chan os.Signal, 2)
|
|
|
|
signal.Notify(sigTerm, os.Interrupt, syscall.SIGTERM)
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
uiEvents := ui.PollEvents()
|
2018-02-21 19:41:40 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
previousKey := ""
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sigTerm:
|
|
|
|
return
|
|
|
|
case <-drawTicker:
|
|
|
|
if !helpVisible {
|
|
|
|
ui.Render(ui.Body)
|
|
|
|
}
|
|
|
|
case e := <-uiEvents:
|
|
|
|
switch e.ID {
|
|
|
|
case "q", "<C-c>":
|
|
|
|
return
|
|
|
|
case "?":
|
|
|
|
helpVisible = !helpVisible
|
|
|
|
if helpVisible {
|
2018-02-19 15:25:02 +08:00
|
|
|
ui.Clear()
|
|
|
|
ui.Render(help)
|
2018-11-30 10:17:13 +08:00
|
|
|
} else {
|
|
|
|
ui.Render(ui.Body)
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
case "h":
|
|
|
|
if !helpVisible {
|
|
|
|
zoom += zoomInterval
|
|
|
|
cpu.Zoom = zoom
|
|
|
|
mem.Zoom = zoom
|
|
|
|
ui.Render(cpu, mem)
|
|
|
|
}
|
|
|
|
case "l":
|
|
|
|
if !helpVisible {
|
|
|
|
if zoom > zoomInterval {
|
|
|
|
zoom -= zoomInterval
|
|
|
|
cpu.Zoom = zoom
|
|
|
|
mem.Zoom = zoom
|
|
|
|
ui.Render(cpu, mem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "<Escape>":
|
|
|
|
if helpVisible {
|
|
|
|
helpVisible = false
|
|
|
|
ui.Render(ui.Body)
|
|
|
|
}
|
|
|
|
case "<Resize>":
|
|
|
|
payload := e.Payload.(ui.Resize)
|
|
|
|
ui.Body.Width, ui.Body.Height = payload.Width, payload.Height
|
|
|
|
ui.Body.Resize()
|
|
|
|
ui.Clear()
|
|
|
|
if helpVisible {
|
2018-04-13 06:53:12 +08:00
|
|
|
ui.Render(help)
|
2018-11-30 10:17:13 +08:00
|
|
|
} else {
|
2018-03-10 08:29:05 +08:00
|
|
|
ui.Render(ui.Body)
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case "<MouseLeft>":
|
|
|
|
payload := e.Payload.(ui.Mouse)
|
|
|
|
proc.Click(payload.X, payload.Y)
|
|
|
|
ui.Render(proc)
|
|
|
|
case "<MouseWheelUp>", "<Up>", "k":
|
|
|
|
proc.Up()
|
|
|
|
ui.Render(proc)
|
|
|
|
case "<MouseWheelDown>", "<Down>", "j":
|
|
|
|
proc.Down()
|
|
|
|
ui.Render(proc)
|
|
|
|
case "g", "<Home>":
|
|
|
|
if previousKey == "g" {
|
|
|
|
proc.Top()
|
2018-04-13 06:53:12 +08:00
|
|
|
ui.Render(proc)
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
|
|
|
case "G", "<End>":
|
|
|
|
proc.Bottom()
|
|
|
|
ui.Render(proc)
|
|
|
|
case "<C-d>":
|
|
|
|
proc.HalfPageDown()
|
|
|
|
ui.Render(proc)
|
|
|
|
case "<C-u>":
|
|
|
|
proc.HalfPageUp()
|
|
|
|
ui.Render(proc)
|
|
|
|
case "<C-f>":
|
|
|
|
proc.PageDown()
|
|
|
|
ui.Render(proc)
|
|
|
|
case "<C-b>":
|
|
|
|
proc.PageUp()
|
|
|
|
ui.Render(proc)
|
|
|
|
case "d":
|
|
|
|
if previousKey == "d" {
|
|
|
|
proc.Kill()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
case "<Tab>":
|
|
|
|
proc.Tab()
|
|
|
|
ui.Render(proc)
|
|
|
|
case "m", "c", "p":
|
|
|
|
proc.ChangeSort(e)
|
|
|
|
ui.Render(proc)
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2018-12-05 13:07:14 +08:00
|
|
|
|
|
|
|
if previousKey == e.ID {
|
|
|
|
previousKey = ""
|
|
|
|
} else {
|
|
|
|
previousKey = e.ID
|
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
func main() {
|
2018-12-05 13:44:25 +08:00
|
|
|
// make the config directory
|
|
|
|
err := os.MkdirAll(configDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
stderrLogger.Fatalf("failed to make the configuration directory: %v", err)
|
|
|
|
}
|
|
|
|
// open the log file
|
|
|
|
lf, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
|
|
|
|
if err != nil {
|
|
|
|
stderrLogger.Fatalf("failed to open log file: %v", err)
|
|
|
|
}
|
|
|
|
defer lf.Close()
|
|
|
|
|
|
|
|
// log time, filename, and line number
|
|
|
|
log.SetFlags(log.Ltime | log.Lshortfile)
|
|
|
|
// log to file
|
|
|
|
log.SetOutput(lf)
|
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
cliArguments()
|
|
|
|
termuiColors() // need to do this before initializing widgets so that they can inherit the colors
|
|
|
|
initWidgets()
|
|
|
|
widgetColors()
|
|
|
|
help = w.NewHelpMenu()
|
2018-02-19 15:25:02 +08:00
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
// inits termui
|
2018-12-05 13:44:25 +08:00
|
|
|
err = ui.Init()
|
2018-11-30 10:17:13 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer ui.Close()
|
|
|
|
|
|
|
|
setupGrid()
|
|
|
|
ui.Render(ui.Body)
|
|
|
|
eventLoop()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|