gotop/gotop.go

217 lines
3.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"
"syscall"
"time"
2018-02-21 18:24:36 +08:00
"github.com/cjbassi/gotop/colorschemes"
2018-02-19 15:25:02 +08:00
ui "github.com/cjbassi/gotop/termui"
w "github.com/cjbassi/gotop/widgets"
"github.com/docopt/docopt-go"
)
const VERSION = "1.0.1"
2018-02-19 15:25:02 +08:00
var (
resized = make(chan bool, 1)
helpToggled = make(chan bool, 1)
helpStatus = false
procLoaded = make(chan bool, 1)
keyPressed = make(chan bool, 1)
2018-02-22 09:49:47 +08:00
colorscheme = colorschemes.Default
2018-02-19 15:25:02 +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
)
// Sets up docopt which is a command line argument parser
2018-02-21 18:24:36 +08:00
func arguments() {
2018-02-19 15:25:02 +08:00
usage := `
Usage: gotop [options]
Options:
2018-02-21 18:24:36 +08:00
-c, --color <name> Set a colorscheme.
-h, --help Show this screen.
-v, --version Show version.
2018-02-19 15:25:02 +08:00
Colorschemes:
default
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))
}
}
func handleColorscheme(cs string) {
switch cs {
2018-02-22 11:54:25 +08:00
case "monokai":
colorscheme = colorschemes.Monokai
2018-02-21 18:24:36 +08:00
case "solarized":
2018-02-22 09:49:47 +08:00
colorscheme = colorschemes.Solarized
2018-02-21 18:24:36 +08:00
case "default":
2018-02-22 09:49:47 +08:00
colorscheme = colorschemes.Default
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
ui.Body.Set(0, 0, 12, 4, cpu)
ui.Body.Set(0, 4, 4, 6, disk)
ui.Body.Set(0, 6, 4, 8, temp)
ui.Body.Set(4, 4, 12, 8, mem)
ui.Body.Set(0, 8, 6, 12, net)
ui.Body.Set(6, 8, 12, 12, proc)
}
func keyBinds() {
// quits
ui.On("q", "C-c", func(e ui.Event) {
ui.StopLoop()
})
// toggles help menu
ui.On("?", func(e ui.Event) {
helpToggled <- true
helpStatus = !helpStatus
})
// hides help menu
ui.On("<escape>", func(e ui.Event) {
if helpStatus {
helpToggled <- true
helpStatus = false
}
})
}
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.BarColor = ui.Color(colorscheme.DiskBar)
ui.Theme.TempLow = ui.Color(colorscheme.TempLow)
ui.Theme.TempHigh = ui.Color(colorscheme.TempHigh)
}
func widgetColors() {
mem.LineColor["Main"] = ui.Color(colorscheme.MainMem)
mem.LineColor["Swap"] = ui.Color(colorscheme.SwapMem)
LineColor := make(map[string]ui.Color)
for i := 0; i < len(cpu.Data); i++ {
LineColor[fmt.Sprintf("CPU%d", i+1)] = ui.Color(colorscheme.CPULines[i])
}
cpu.LineColor = LineColor
2018-02-19 15:25:02 +08:00
}
func main() {
2018-02-21 18:24:36 +08:00
arguments()
termuiColors()
2018-02-19 15:25:02 +08:00
keyBinds()
2018-02-21 18:24:36 +08:00
cpu = w.NewCPU()
mem = w.NewMem()
proc = w.NewProc(procLoaded, keyPressed)
net = w.NewNet()
disk = w.NewDisk()
temp = w.NewTemp()
widgetColors()
2018-02-19 15:25:02 +08:00
<-procLoaded
err := ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
setupGrid()
2018-02-21 19:41:40 +08:00
help = w.NewHelpMenu()
2018-02-19 15:25:02 +08:00
ui.On("resize", func(e ui.Event) {
ui.Body.Width, ui.Body.Height = e.Width, e.Height
ui.Body.Resize()
2018-02-21 19:41:40 +08:00
help.XOffset = (ui.Body.Width - help.X) / 2
help.YOffset = (ui.Body.Height - help.Y) / 2
2018-02-19 15:25:02 +08:00
resized <- true
})
// All rendering done here
go func() {
ui.Render(ui.Body)
drawTick := time.NewTicker(time.Second)
for {
select {
case <-helpToggled:
if helpStatus {
ui.Clear()
ui.Render(help)
} else {
ui.Render(ui.Body)
}
case <-resized:
if !helpStatus {
ui.Clear()
ui.Render(ui.Body)
} else if helpStatus {
ui.Clear()
ui.Render(help)
}
case <-keyPressed:
if !helpStatus {
ui.Render(proc)
}
case <-drawTick.C:
if !helpStatus {
ui.Render(ui.Body)
}
}
}
}()
// handles kill signal
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
ui.StopLoop()
}()
ui.Loop()
}