2018-02-19 15:25:02 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-19 04:42:49 +08:00
|
|
|
"encoding/json"
|
2018-12-11 13:21:40 +08:00
|
|
|
"fmt"
|
2018-09-19 04:42:49 +08:00
|
|
|
"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-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
|
|
|
|
2018-09-19 04:42:49 +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)
|
2019-01-13 08:40:43 +08:00
|
|
|
logPath = filepath.Join(logDir, "errors.log")
|
2019-01-13 08:31:37 +08:00
|
|
|
|
2018-12-05 13:44:25 +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
|
2019-06-08 06:03:00 +08:00
|
|
|
netInterface = w.NET_INTERFACE_ALL
|
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.
|
2018-11-14 01:29:09 +08:00
|
|
|
-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).
|
2019-05-15 04:26:22 +08:00
|
|
|
-i, --interface=NAME Select network interface [default: all].
|
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
|
|
|
`
|
|
|
|
|
2018-12-05 13:44:25 +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-12-05 13:44:25 +08:00
|
|
|
}
|
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
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
averageLoad, _ = args["--averagecpu"].(bool)
|
|
|
|
percpuLoad, _ = args["--percpu"].(bool)
|
2018-12-27 12:06:55 +08:00
|
|
|
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)
|
2018-12-05 13:44:25 +08:00
|
|
|
rate, err := strconv.ParseFloat(rateStr, 64)
|
|
|
|
if err != nil {
|
2018-12-11 13:21:40 +08:00
|
|
|
return fmt.Errorf("invalid rate parameter")
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
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
|
|
|
|
}
|
2019-05-15 04:26:22 +08:00
|
|
|
netInterface, _ = args["--interface"].(string)
|
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:
|
2019-01-15 11:39:37 +08:00
|
|
|
custom, err := getCustomColorscheme(cs)
|
|
|
|
if err != nil {
|
2018-12-11 13:21:40 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-01-15 11:39:37 +08:00
|
|
|
colorscheme = custom
|
2018-09-19 04:42:49 +08:00
|
|
|
}
|
2018-12-11 13:21:40 +08:00
|
|
|
return nil
|
2018-09-19 04:42:49 +08:00
|
|
|
}
|
|
|
|
|
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
|
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 {
|
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
|
|
|
}
|
2018-09-19 04:42:49 +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-09-19 04:42:49 +08:00
|
|
|
}
|
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 {
|
2019-01-02 01:04:31 +08:00
|
|
|
var cpuRow ui.GridItem
|
2018-12-27 12:06:55 +08:00
|
|
|
if battery {
|
2019-02-02 15:22:27 +08:00
|
|
|
cpuRow = ui.NewRow(1.0/3,
|
2019-01-02 01:04:31 +08:00
|
|
|
ui.NewCol(2.0/3, cpu),
|
|
|
|
ui.NewCol(1.0/3, batt),
|
|
|
|
)
|
2018-12-27 12:06:55 +08:00
|
|
|
} else {
|
2019-02-02 15:22:27 +08:00
|
|
|
cpuRow = ui.NewRow(1.0/3, cpu)
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
2019-01-01 08:55:50 +08:00
|
|
|
grid.Set(
|
2019-01-02 01:04:31 +08:00
|
|
|
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() {
|
2019-01-24 13:23:35 +08:00
|
|
|
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
|
|
|
|
2019-01-24 13:23:35 +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-03-28 05:00:32 +08:00
|
|
|
}
|
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
|
|
|
}
|
2018-02-25 13:35:57 +08:00
|
|
|
|
2019-01-13 08:31:37 +08:00
|
|
|
if !minimalMode {
|
2019-01-02 01:04:31 +08:00
|
|
|
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)
|
2019-01-02 01:04:31 +08:00
|
|
|
i++
|
2018-12-27 12:06:55 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 01:04:31 +08:00
|
|
|
|
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
|
|
|
|
2019-01-24 13:23:35 +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() {
|
2019-03-08 15:43:10 +08:00
|
|
|
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 {
|
2019-01-02 01:04:31 +08:00
|
|
|
if battery {
|
2019-03-08 15:43:10 +08:00
|
|
|
batt = w.NewBatteryWidget(graphHorizontalScale)
|
2019-01-02 01:04:31 +08:00
|
|
|
}
|
2019-05-15 00:09:17 +08:00
|
|
|
net = w.NewNetWidget(netInterface)
|
2019-03-08 15:43:10 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-30 10:17:13 +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
|
|
|
|
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 {
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(grid)
|
2019-03-17 22:54:56 +08:00
|
|
|
if statusbar {
|
|
|
|
ui.Render(bar)
|
|
|
|
}
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
|
|
|
case e := <-uiEvents:
|
|
|
|
switch e.ID {
|
|
|
|
case "q", "<C-c>":
|
|
|
|
return
|
|
|
|
case "?":
|
|
|
|
helpVisible = !helpVisible
|
2019-01-08 12:10:09 +08:00
|
|
|
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)
|
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
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)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<Escape>":
|
|
|
|
helpVisible = false
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(grid)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<Resize>":
|
|
|
|
ui.Render(help)
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
} else {
|
|
|
|
switch e.ID {
|
|
|
|
case "?":
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(grid)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "h":
|
2019-01-13 08:31:37 +08:00
|
|
|
graphHorizontalScale += graphHorizontalScaleDelta
|
|
|
|
cpu.HorizontalScale = graphHorizontalScale
|
|
|
|
mem.HorizontalScale = graphHorizontalScale
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(cpu, mem)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "l":
|
2019-01-13 08:31:37 +08:00
|
|
|
if graphHorizontalScale > graphHorizontalScaleDelta {
|
|
|
|
graphHorizontalScale -= graphHorizontalScaleDelta
|
|
|
|
cpu.HorizontalScale = graphHorizontalScale
|
|
|
|
mem.HorizontalScale = graphHorizontalScale
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(cpu, mem)
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<Resize>":
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(grid)
|
2019-02-06 18:11:41 +08:00
|
|
|
if statusbar {
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(bar)
|
2019-02-06 18:11:41 +08:00
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<MouseLeft>":
|
|
|
|
payload := e.Payload.(ui.Mouse)
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.HandleClick(payload.X, payload.Y)
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "k", "<Up>", "<MouseWheelUp>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollUp()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "j", "<Down>", "<MouseWheelDown>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollDown()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-02-02 15:22:27 +08:00
|
|
|
case "<Home>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollTop()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-02-02 15:06:57 +08:00
|
|
|
case "g":
|
2019-01-08 12:10:09 +08:00
|
|
|
if previousKey == "g" {
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollTop()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
}
|
|
|
|
case "G", "<End>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollBottom()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<C-d>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollHalfPageDown()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<C-u>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollHalfPageUp()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<C-f>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollPageDown()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "<C-b>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ScrollPageUp()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "d":
|
|
|
|
if previousKey == "d" {
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.KillProc()
|
2019-01-08 12:10:09 +08:00
|
|
|
}
|
|
|
|
case "<Tab>":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ToggleShowingGroupedProcs()
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2019-01-08 12:10:09 +08:00
|
|
|
case "m", "c", "p":
|
2019-03-01 08:29:52 +08:00
|
|
|
proc.ChangeProcSortMethod(w.ProcSortMethod(e.ID))
|
2019-03-08 15:43:10 +08:00
|
|
|
ui.Render(proc)
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
2019-01-08 12:10:09 +08:00
|
|
|
|
|
|
|
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-11-30 10:17:13 +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
|
2019-01-13 08:40:43 +08:00
|
|
|
if err := os.MkdirAll(logDir, 0755); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to make the log directory: %v", err)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
|
|
|
// 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)
|
2018-12-05 13:44:25 +08:00
|
|
|
if err != nil {
|
2018-12-11 13:21:40 +08:00
|
|
|
return nil, fmt.Errorf("failed to open log file: %v", err)
|
2018-12-05 13:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2018-12-05 13:44:25 +08:00
|
|
|
|
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)
|
2018-11-30 10:17:13 +08:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
|
2019-01-24 13:23:35 +08:00
|
|
|
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
|
|
|
|
2018-11-30 10:17:13 +08:00
|
|
|
eventLoop()
|
2018-02-19 15:25:02 +08:00
|
|
|
}
|