gotop/main.go

465 lines
9.9 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-04-13 23:54:20 +08:00
"sync"
2018-02-19 15:25:02 +08:00
"syscall"
"time"
appdir "github.com/ProtonMail/go-appdir"
"github.com/cjbassi/gotop/colorschemes"
2018-12-13 10:18:43 +08:00
"github.com/cjbassi/gotop/src/logging"
2018-05-11 12:23:20 +08:00
w "github.com/cjbassi/gotop/src/widgets"
2018-12-25 05:09:30 +08:00
docopt "github.com/docopt/docopt.go"
2019-01-01 08:55:50 +08:00
ui "github.com/gizak/termui"
2018-02-19 15:25:02 +08:00
)
2018-12-14 14:42:46 +08:00
var version = "1.7.1"
2018-02-19 15:25:02 +08:00
var (
colorscheme = colorschemes.Default
2018-03-10 08:29:05 +08:00
minimal = false
interval = time.Second
zoom = 7
zoomInterval = 3
helpVisible = false
averageLoad = false
battery = false
percpuLoad = false
2018-12-02 13:19:10 +08:00
fahrenheit = false
2018-12-23 11:24:12 +08:00
configDir = appdir.New("gotop").UserConfig()
logPath = filepath.Join(configDir, "errors.log")
stderrLogger = log.New(os.Stderr, "", 0)
2019-01-01 18:15:31 +08:00
statusbar = false
2019-01-01 08:55:50 +08:00
termWidth int
termHeight int
2018-08-01 05:24:44 +08:00
2018-02-21 18:24:36 +08:00
cpu *w.CPU
batt *w.Batt
2018-02-21 18:24:36 +08:00
mem *w.Mem
proc *w.Proc
net *w.Net
disk *w.Disk
temp *w.Temp
help *w.HelpMenu
2019-01-01 08:55:50 +08:00
grid *ui.Grid
2018-02-19 15:25:02 +08:00
)
2018-12-11 13:21:40 +08:00
func cliArguments() 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-01-05 01:51:31 +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
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
minimal, _ = 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 {
interval = time.Second * time.Duration(1/rate)
} else {
interval = time.Second / time.Duration(rate)
}
fahrenheit, _ = args["--fahrenheit"].(bool)
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
case "default-dark":
colorscheme = colorschemes.DefaultDark
2018-02-21 18:24:36 +08:00
default:
2018-12-11 13:21:40 +08:00
if colorscheme, err := getCustomColorscheme(cs); err != nil {
colorscheme = colorscheme
return err
}
}
2018-12-11 13:21:40 +08:00
return nil
}
// 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 {
2018-12-11 13:21:40 +08:00
return colorscheme, fmt.Errorf("colorscheme file not found")
2018-02-19 15:25:02 +08:00
}
err = json.Unmarshal(dat, &colorscheme)
if err != nil {
2018-12-11 13:21:40 +08:00
return colorscheme, fmt.Errorf("could not parse colorscheme file")
}
2018-12-11 13:21:40 +08:00
return colorscheme, nil
2018-02-19 15:25:02 +08:00
}
func setupGrid() {
2019-01-01 08:55:50 +08:00
grid = ui.NewGrid()
grid.SetRect(0, 0, termWidth, termHeight)
2018-02-19 15:25:02 +08:00
2019-01-01 18:15:31 +08:00
var barRow interface{}
2018-03-09 15:51:03 +08:00
if minimal {
2019-01-01 18:15:31 +08:00
rowHeight := 1.0 / 2
if statusbar {
rowHeight = 50.0 / 101
barRow = ui.NewRow(1.0/101, w.NewStatusBar())
}
2019-01-01 08:55:50 +08:00
grid.Set(
2019-01-01 18:15:31 +08:00
ui.NewRow(rowHeight, cpu),
ui.NewRow(rowHeight,
2019-01-01 08:55:50 +08:00
ui.NewCol(1.0/2, mem),
ui.NewCol(1.0/2, proc),
),
2019-01-01 18:15:31 +08:00
barRow,
2019-01-01 08:55:50 +08:00
)
2018-03-09 15:51:03 +08:00
} else {
2019-01-01 18:15:31 +08:00
rowHeight := 1.0 / 3
if statusbar {
rowHeight = 50.0 / 151
barRow = ui.NewRow(1.0/151, w.NewStatusBar())
}
var cpuRow ui.GridItem
if battery {
cpuRow = ui.NewRow(rowHeight,
ui.NewCol(2.0/3, cpu),
ui.NewCol(1.0/3, batt),
)
} else {
cpuRow = ui.NewRow(rowHeight, cpu)
}
2019-01-01 08:55:50 +08:00
grid.Set(
cpuRow,
2019-01-01 18:15:31 +08:00
ui.NewRow(rowHeight,
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-01-01 18:15:31 +08:00
ui.NewRow(rowHeight,
2019-01-01 08:55:50 +08:00
ui.NewCol(1.0/2, net),
ui.NewCol(1.0/2, proc),
),
2019-01-01 18:15:31 +08:00
barRow,
2019-01-01 08:55:50 +08:00
)
2018-03-09 15:51:03 +08:00
}
2018-02-19 15:25:02 +08:00
}
2018-02-21 18:24:36 +08:00
func termuiColors() {
2019-01-01 08:55:50 +08:00
ui.Theme.Default = ui.AttrPair{ui.Attribute(colorscheme.Fg), ui.Attribute(colorscheme.Bg)}
ui.Theme.Block.Title = ui.AttrPair{ui.Attribute(colorscheme.BorderLabel), ui.Attribute(colorscheme.Bg)}
ui.Theme.Block.Border = ui.AttrPair{ui.Attribute(colorscheme.BorderLine), ui.Attribute(colorscheme.Bg)}
2018-02-21 18:24:36 +08:00
}
func widgetColors() {
2019-01-01 08:55:50 +08:00
mem.LineColor["Main"] = ui.Attribute(colorscheme.MainMem)
mem.LineColor["Swap"] = ui.Attribute(colorscheme.SwapMem)
proc.CursorColor = ui.Attribute(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-01-01 08:55:50 +08:00
cpu.LineColor[v] = ui.Attribute(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 {
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.LineColor[v] = ui.Attribute(c)
i++
}
}
2019-01-01 08:55:50 +08:00
temp.TempLow = ui.Attribute(colorscheme.TempLow)
temp.TempHigh = ui.Attribute(colorscheme.TempHigh)
net.Lines[0].LineColor = ui.Attribute(colorscheme.Sparkline)
net.Lines[0].TitleColor = ui.Attribute(colorscheme.BorderLabel)
net.Lines[1].LineColor = ui.Attribute(colorscheme.Sparkline)
net.Lines[1].TitleColor = ui.Attribute(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() {
var wg sync.WaitGroup
2018-02-24 13:15:38 +08:00
wg.Add(1)
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
}()
wg.Add(1)
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
}()
wg.Add(1)
2018-04-13 10:43:17 +08:00
go func() {
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 {
if battery {
wg.Add(1)
go func() {
batt = w.NewBatt(time.Minute, zoom)
wg.Done()
}()
}
wg.Add(1)
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
}()
wg.Add(1)
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
}()
wg.Add(1)
2018-04-13 10:43:17 +08:00
go func() {
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()
}
func eventLoop() {
drawTicker := time.NewTicker(interval).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 {
2019-01-01 08:55:50 +08:00
ui.Render(grid)
}
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "?":
helpVisible = !helpVisible
case "<Resize>":
payload := e.Payload.(ui.Resize)
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
2019-01-01 08:55:50 +08:00
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":
zoom += zoomInterval
cpu.Zoom = zoom
mem.Zoom = zoom
ui.Render(cpu, mem)
case "l":
if zoom > zoomInterval {
zoom -= zoomInterval
cpu.Zoom = zoom
mem.Zoom = zoom
ui.Render(cpu, mem)
}
case "<Resize>":
2019-01-01 08:55:50 +08:00
ui.Render(grid)
case "<MouseLeft>":
payload := e.Payload.(ui.Mouse)
proc.Click(payload.X, payload.Y)
ui.Render(proc)
case "k", "<Up>", "<MouseWheelUp>":
proc.Up()
ui.Render(proc)
case "j", "<Down>", "<MouseWheelDown>":
proc.Down()
ui.Render(proc)
case "g", "<Home>":
if previousKey == "g" {
proc.Top()
ui.Render(proc)
}
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()
}
case "<Tab>":
proc.Tab()
ui.Render(proc)
case "m", "c", "p":
proc.ChangeSort(e)
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
2018-12-13 10:18:43 +08:00
func setupLogging() (*os.File, error) {
// make the config directory
2018-12-11 13:21:40 +08:00
if err := os.MkdirAll(configDir, 0755); err != nil {
return nil, fmt.Errorf("failed to make the configuration directory: %v", err)
}
// open the log file
2018-12-14 14:04:51 +08:00
lf, 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
log.SetOutput(lf)
2018-12-11 13:21:40 +08:00
return lf, nil
2018-12-10 13:19:09 +08:00
}
func main() {
2018-12-13 10:18:43 +08:00
lf, err := setupLogging()
2018-12-11 13:21:40 +08:00
if err != nil {
stderrLogger.Fatalf("failed to setup logging: %v", err)
}
2018-12-10 13:23:05 +08:00
defer lf.Close()
2018-12-11 13:21:40 +08:00
if err := cliArguments(); err != nil {
stderrLogger.Fatalf("failed to parse cli args: %v", err)
}
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
2018-12-13 10:18:43 +08:00
logging.StderrToLogfile(lf)
2018-12-11 13:21:40 +08:00
2019-01-01 08:55:50 +08:00
termWidth, termHeight = ui.TerminalSize()
termuiColors() // need to do this before initializing widgets so that they can inherit the colors
initWidgets()
widgetColors()
help = w.NewHelpMenu()
help.Resize(termWidth, termHeight)
setupGrid()
2019-01-01 08:55:50 +08:00
ui.Render(grid)
2018-12-11 13:21:40 +08:00
eventLoop()
2018-02-19 15:25:02 +08:00
}