xmtop/main.go

461 lines
10 KiB
Go
Raw Normal View History

2018-02-19 15:25:02 +08:00
package main
import (
"encoding/json"
2018-12-11 13:21:40 +08:00
"fmt"
"io/ioutil"
"log"
2018-02-19 15:25:02 +08:00
"os"
"os/signal"
"path/filepath"
2018-08-17 08:05:21 +08:00
"sort"
2018-03-09 16:27:46 +08:00
"strconv"
2018-02-19 15:25:02 +08:00
"syscall"
"time"
2019-02-07 09:18:44 +08:00
docopt "github.com/docopt/docopt.go"
2019-03-08 15:15:41 +08:00
ui "github.com/gizak/termui/v3"
2019-02-07 09:18:44 +08:00
"github.com/cjbassi/gotop/colorschemes"
2018-12-13 10:18:43 +08:00
"github.com/cjbassi/gotop/src/logging"
2019-03-09 05:55:50 +08:00
"github.com/cjbassi/gotop/src/utils"
2018-05-11 12:23:20 +08:00
w "github.com/cjbassi/gotop/src/widgets"
2018-02-19 15:25:02 +08:00
)
2019-01-13 08:31:37 +08:00
const (
2019-02-16 18:11:05 +08:00
appName = "gotop"
2019-02-23 03:53:36 +08:00
version = "3.0.0"
2019-01-13 08:31:37 +08:00
graphHorizontalScaleDelta = 3
)
2018-02-19 15:25:02 +08:00
var (
2019-03-09 05:55:50 +08:00
configDir = utils.GetConfigDir(appName)
logDir = utils.GetLogDir(appName)
logPath = filepath.Join(logDir, "errors.log")
2019-01-13 08:31:37 +08:00
stderrLogger = log.New(os.Stderr, "", 0)
2019-01-13 08:31:37 +08:00
graphHorizontalScale = 7
helpVisible = false
colorscheme = colorschemes.Default
updateInterval = time.Second
minimalMode = false
averageLoad = false
percpuLoad = false
2019-03-01 08:29:52 +08:00
tempScale = w.Celcius
2019-01-13 08:31:37 +08:00
battery = false
statusbar = false
2018-08-01 05:24:44 +08:00
2019-03-01 08:29:52 +08:00
cpu *w.CpuWidget
batt *w.BatteryWidget
mem *w.MemWidget
proc *w.ProcWidget
net *w.NetWidget
disk *w.DiskWidget
temp *w.TempWidget
2018-02-21 18:24:36 +08:00
help *w.HelpMenu
2019-01-01 08:55:50 +08:00
grid *ui.Grid
2019-02-02 15:22:27 +08:00
bar *w.StatusBar
2018-02-19 15:25:02 +08:00
)
2019-02-07 09:18:44 +08:00
func parseArgs() error {
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].
2019-02-16 17:56:35 +08:00
-V, --version Print version and exit.
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.
-f, --fahrenheit Show temperatures in fahrenheit.
2019-01-05 01:51:31 +08:00
-s, --statusbar Show a statusbar with the time.
-b, --battery Show battery level widget ('minimal' turns off).
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
2019-02-21 15:51:49 +08:00
vice
2018-02-19 15:25:02 +08:00
`
args, err := docopt.ParseArgs(usage, os.Args[1:], version)
if err != nil {
2018-12-11 13:21:40 +08:00
return err
}
2018-02-21 18:24:36 +08:00
if val, _ := args["--color"]; val != nil {
2018-12-11 13:21:40 +08:00
if err := handleColorscheme(val.(string)); err != nil {
return err
}
2018-02-21 18:24:36 +08:00
}
averageLoad, _ = args["--averagecpu"].(bool)
percpuLoad, _ = args["--percpu"].(bool)
battery, _ = args["--battery"].(bool)
2018-03-09 15:51:03 +08:00
2019-01-13 08:31:37 +08:00
minimalMode, _ = args["--minimal"].(bool)
2018-03-09 16:27:46 +08:00
2019-01-05 01:51:31 +08:00
statusbar, _ = args["--statusbar"].(bool)
2018-03-09 16:27:46 +08:00
rateStr, _ := args["--rate"].(string)
rate, err := strconv.ParseFloat(rateStr, 64)
if err != nil {
2018-12-11 13:21:40 +08:00
return fmt.Errorf("invalid rate parameter")
}
2018-03-09 16:34:26 +08:00
if rate < 1 {
2019-01-13 08:31:37 +08:00
updateInterval = time.Second * time.Duration(1/rate)
2018-03-09 16:34:26 +08:00
} else {
2019-01-13 08:31:37 +08:00
updateInterval = time.Second / time.Duration(rate)
2018-03-09 16:34:26 +08:00
}
2019-03-01 08:29:52 +08:00
fahrenheit, _ := args["--fahrenheit"].(bool)
if fahrenheit {
tempScale = w.Fahrenheit
}
2018-12-11 13:21:40 +08:00
return nil
2018-02-21 18:24:36 +08:00
}
2018-12-11 13:21:40 +08:00
func handleColorscheme(cs string) error {
2018-02-21 18:24:36 +08:00
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
2019-02-21 15:51:49 +08:00
case "vice":
colorscheme = colorschemes.Vice
2018-04-10 13:00:27 +08:00
case "default-dark":
colorscheme = colorschemes.DefaultDark
2018-02-21 18:24:36 +08:00
default:
custom, err := getCustomColorscheme(cs)
if err != nil {
2018-12-11 13:21:40 +08:00
return err
}
colorscheme = custom
}
2018-12-11 13:21:40 +08:00
return nil
}
2019-02-07 09:18:44 +08:00
// getCustomColorscheme tries to read a custom json colorscheme from <configDir>/<name>.json
2018-12-11 13:21:40 +08:00
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 {
2019-01-13 08:44:12 +08:00
return colorscheme, fmt.Errorf("failed to read colorscheme file: %v", err)
2018-02-19 15:25:02 +08:00
}
err = json.Unmarshal(dat, &colorscheme)
if err != nil {
2019-01-13 08:44:12 +08:00
return colorscheme, fmt.Errorf("failed to parse colorscheme file: %v", err)
}
2018-12-11 13:21:40 +08:00
return colorscheme, nil
2018-02-19 15:25:02 +08:00
}
2019-01-20 11:37:31 +08:00
func setupGrid() {
2019-01-01 08:55:50 +08:00
grid = ui.NewGrid()
2018-02-19 15:25:02 +08:00
2019-01-13 08:31:37 +08:00
if minimalMode {
2019-01-01 08:55:50 +08:00
grid.Set(
2019-02-02 15:22:27 +08:00
ui.NewRow(1.0/2, cpu),
ui.NewRow(1.0/2,
2019-01-01 08:55:50 +08:00
ui.NewCol(1.0/2, mem),
ui.NewCol(1.0/2, proc),
),
)
2018-03-09 15:51:03 +08:00
} else {
var cpuRow ui.GridItem
if battery {
2019-02-02 15:22:27 +08:00
cpuRow = ui.NewRow(1.0/3,
ui.NewCol(2.0/3, cpu),
ui.NewCol(1.0/3, batt),
)
} else {
2019-02-02 15:22:27 +08:00
cpuRow = ui.NewRow(1.0/3, cpu)
}
2019-01-01 08:55:50 +08:00
grid.Set(
cpuRow,
2019-02-02 15:22:27 +08:00
ui.NewRow(1.0/3,
2019-01-01 08:55:50 +08:00
ui.NewCol(1.0/3,
ui.NewRow(1.0/2, disk),
ui.NewRow(1.0/2, temp),
),
ui.NewCol(2.0/3, mem),
),
2019-02-02 15:22:27 +08:00
ui.NewRow(1.0/3,
2019-01-01 08:55:50 +08:00
ui.NewCol(1.0/2, net),
ui.NewCol(1.0/2, proc),
),
)
2018-03-09 15:51:03 +08:00
}
2018-02-19 15:25:02 +08:00
}
2019-02-07 09:18:44 +08:00
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))
2018-02-21 18:24:36 +08:00
}
2019-02-07 09:18:44 +08:00
func setWidgetColors() {
2019-03-01 08:29:52 +08:00
mem.LineColors["Main"] = ui.Color(colorscheme.MainMem)
mem.LineColors["Swap"] = ui.Color(colorscheme.SwapMem)
2019-01-01 08:55:50 +08:00
proc.CursorColor = ui.Color(colorscheme.ProcCursor)
2018-02-21 18:24:36 +08:00
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]
2019-03-01 08:29:52 +08:00
cpu.LineColors[v] = ui.Color(c)
2018-08-01 05:24:44 +08:00
i++
2018-02-21 18:24:36 +08:00
}
2019-01-13 08:31:37 +08:00
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]
2019-03-01 08:29:52 +08:00
batt.LineColors[v] = ui.Color(c)
i++
}
}
2019-03-01 08:29:52 +08:00
temp.TempLowColor = ui.Color(colorscheme.TempLow)
temp.TempHighColor = ui.Color(colorscheme.TempHigh)
2019-01-01 08:55:50 +08:00
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)
2018-03-09 15:51:03 +08:00
}
2018-02-19 15:25:02 +08:00
}
2018-04-13 23:54:20 +08:00
func initWidgets() {
cpu = w.NewCpuWidget(updateInterval, graphHorizontalScale, averageLoad, percpuLoad)
mem = w.NewMemWidget(updateInterval, graphHorizontalScale)
proc = w.NewProcWidget()
2019-01-20 11:37:31 +08:00
help = w.NewHelpMenu()
2019-01-13 08:31:37 +08:00
if !minimalMode {
if battery {
batt = w.NewBatteryWidget(graphHorizontalScale)
}
net = w.NewNetWidget()
disk = w.NewDiskWidget()
temp = w.NewTempWidget(tempScale)
2018-04-13 10:43:17 +08:00
}
2019-02-02 15:22:27 +08:00
if statusbar {
bar = w.NewStatusBar()
}
2018-04-13 23:54:20 +08:00
}
func eventLoop() {
2019-01-13 08:31:37 +08:00
drawTicker := time.NewTicker(updateInterval).C
2018-02-19 15:25:02 +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
uiEvents := ui.PollEvents()
2018-02-21 19:41:40 +08:00
previousKey := ""
2018-02-19 15:25:02 +08:00
for {
select {
case <-sigTerm:
return
case <-drawTicker:
if !helpVisible {
ui.Render(grid)
2019-03-17 22:54:56 +08:00
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)
2019-02-07 09:18:44 +08:00
termWidth, termHeight := payload.Width, payload.Height
2019-02-02 15:22:27 +08:00
if statusbar {
2019-02-07 09:18:44 +08:00
grid.SetRect(0, 0, termWidth, termHeight-1)
bar.SetRect(0, termHeight-1, termWidth, termHeight)
2019-02-02 15:22:27 +08:00
} else {
grid.SetRect(0, 0, payload.Width, payload.Height)
}
help.Resize(payload.Width, payload.Height)
ui.Clear()
}
if helpVisible {
switch e.ID {
case "?":
2018-02-19 15:25:02 +08:00
ui.Clear()
ui.Render(help)
case "<Escape>":
helpVisible = false
ui.Render(grid)
case "<Resize>":
ui.Render(help)
2018-02-19 15:25:02 +08:00
}
} else {
switch e.ID {
case "?":
ui.Render(grid)
case "h":
2019-01-13 08:31:37 +08:00
graphHorizontalScale += graphHorizontalScaleDelta
cpu.HorizontalScale = graphHorizontalScale
mem.HorizontalScale = graphHorizontalScale
ui.Render(cpu, mem)
case "l":
2019-01-13 08:31:37 +08:00
if graphHorizontalScale > graphHorizontalScaleDelta {
graphHorizontalScale -= graphHorizontalScaleDelta
cpu.HorizontalScale = graphHorizontalScale
mem.HorizontalScale = graphHorizontalScale
ui.Render(cpu, mem)
}
case "<Resize>":
ui.Render(grid)
2019-02-06 18:11:41 +08:00
if statusbar {
ui.Render(bar)
2019-02-06 18:11:41 +08:00
}
case "<MouseLeft>":
payload := e.Payload.(ui.Mouse)
2019-03-01 08:29:52 +08:00
proc.HandleClick(payload.X, payload.Y)
ui.Render(proc)
case "k", "<Up>", "<MouseWheelUp>":
2019-03-01 08:29:52 +08:00
proc.ScrollUp()
ui.Render(proc)
case "j", "<Down>", "<MouseWheelDown>":
2019-03-01 08:29:52 +08:00
proc.ScrollDown()
ui.Render(proc)
2019-02-02 15:22:27 +08:00
case "<Home>":
2019-03-01 08:29:52 +08:00
proc.ScrollTop()
ui.Render(proc)
2019-02-02 15:06:57 +08:00
case "g":
if previousKey == "g" {
2019-03-01 08:29:52 +08:00
proc.ScrollTop()
ui.Render(proc)
}
case "G", "<End>":
2019-03-01 08:29:52 +08:00
proc.ScrollBottom()
ui.Render(proc)
case "<C-d>":
2019-03-01 08:29:52 +08:00
proc.ScrollHalfPageDown()
ui.Render(proc)
case "<C-u>":
2019-03-01 08:29:52 +08:00
proc.ScrollHalfPageUp()
ui.Render(proc)
case "<C-f>":
2019-03-01 08:29:52 +08:00
proc.ScrollPageDown()
ui.Render(proc)
case "<C-b>":
2019-03-01 08:29:52 +08:00
proc.ScrollPageUp()
ui.Render(proc)
case "d":
if previousKey == "d" {
2019-03-01 08:29:52 +08:00
proc.KillProc()
}
case "<Tab>":
2019-03-01 08:29:52 +08:00
proc.ToggleShowingGroupedProcs()
ui.Render(proc)
case "m", "c", "p":
2019-03-01 08:29:52 +08:00
proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID))
ui.Render(proc)
}
if previousKey == e.ID {
previousKey = ""
} else {
previousKey = e.ID
2018-02-19 15:25:02 +08:00
}
}
2018-12-05 13:07:14 +08:00
2018-02-19 15:25:02 +08:00
}
}
}
2018-02-19 15:25:02 +08:00
2019-02-07 09:18:44 +08:00
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
2019-02-07 09:18:44 +08:00
logfile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0660)
if err != nil {
2018-12-11 13:21:40 +08:00
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
2019-02-07 09:18:44 +08:00
log.SetOutput(logfile)
2019-02-07 09:18:44 +08:00
return logfile, nil
2018-12-10 13:19:09 +08:00
}
func main() {
2019-02-07 09:18:44 +08:00
if err := parseArgs(); err != nil {
2018-12-11 13:21:40 +08:00
stderrLogger.Fatalf("failed to parse cli args: %v", err)
}
2019-02-07 09:18:44 +08:00
logfile, err := setupLogfile()
2019-01-20 11:37:31 +08:00
if err != nil {
stderrLogger.Fatalf("failed to setup log file: %v", err)
}
2019-02-07 09:18:44 +08:00
defer logfile.Close()
2019-01-20 11:37:31 +08:00
2018-12-10 13:19:09 +08:00
if err := ui.Init(); err != nil {
2018-12-11 13:21:40 +08:00
stderrLogger.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
2018-12-11 13:21:40 +08:00
2019-02-07 09:18:44 +08:00
logging.StderrToLogfile(logfile)
2018-12-11 13:21:40 +08:00
2019-02-07 09:18:44 +08:00
setDefaultTermuiColors() // done before initializing widgets to allow inheriting colors
2019-01-01 08:55:50 +08:00
initWidgets()
2019-02-07 09:18:44 +08:00
setWidgetColors()
2019-01-13 08:44:12 +08:00
2019-01-20 11:37:31 +08:00
setupGrid()
termWidth, termHeight := ui.TerminalDimensions()
2019-02-02 15:22:27 +08:00
if statusbar {
grid.SetRect(0, 0, termWidth, termHeight-1)
} else {
grid.SetRect(0, 0, termWidth, termHeight)
}
2019-01-01 08:55:50 +08:00
help.Resize(termWidth, termHeight)
ui.Render(grid)
2019-02-02 15:22:27 +08:00
if statusbar {
bar.SetRect(0, termHeight-1, termWidth, termHeight)
ui.Render(bar)
}
2018-12-11 13:21:40 +08:00
eventLoop()
2018-02-19 15:25:02 +08:00
}