From 75220b3db8e4ed759eec75b75aada8fa63f49283 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Fri, 9 Mar 2018 16:29:05 -0800 Subject: [PATCH] Added graph zooming; closes #3 --- README.md | 3 ++- gotop.go | 31 +++++++++++++++++++++++++++---- termui/linegraph.go | 15 +++++++++++++-- widgets/cpu.go | 3 ++- widgets/help.go | 6 ++++-- widgets/mem.go | 3 ++- 6 files changed, 50 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index eb49ff2..5d8e4eb 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ go get github.com/cjbassi/gotop ### Keybinds * Quit: `q` or `` -* Navigation: +* Process Navigation: * ``/`` and `j`/`k`: up and down * `` and ``: up and down half a page * `` and ``: up and down a full page @@ -48,6 +48,7 @@ go get github.com/cjbassi/gotop * `p`: PID * ``: toggle process grouping * `dd`: kill the selected process or process group +* `h` and `l`: zoom in and out of CPU and Mem graphs * `?`: toggles keybind help menu diff --git a/gotop.go b/gotop.go index a5a708a..e25841a 100644 --- a/gotop.go +++ b/gotop.go @@ -26,11 +26,15 @@ var ( procLoaded = make(chan bool, 1) // used to render the proc widget whenever a key is pressed for it keyPressed = make(chan bool, 1) + // used to render cpu and mem when zoom has changed + zoomed = make(chan bool, 1) colorscheme = colorschemes.Default - minimal = false - interval = time.Second + minimal = false + interval = time.Second + zoom = 7 + zoomInterval = 3 cpu *w.CPU mem *w.Mem @@ -128,6 +132,21 @@ func keyBinds() { helpVisible = false } }) + + ui.On("h", func(e ui.Event) { + zoom += zoomInterval + cpu.Zoom = zoom + mem.Zoom = zoom + zoomed <- true + }) + ui.On("l", func(e ui.Event) { + if zoom > zoomInterval { + zoom -= zoomInterval + cpu.Zoom = zoom + mem.Zoom = zoom + zoomed <- true + } + }) } func termuiColors() { @@ -167,8 +186,8 @@ func main() { // need to do this before initializing widgets so that they can inherit the colors termuiColors() - cpu = w.NewCPU(interval) - mem = w.NewMem(interval) + cpu = w.NewCPU(interval, zoom) + mem = w.NewMem(interval, zoom) proc = w.NewProc(procLoaded, keyPressed) if !minimal { net = w.NewNet() @@ -227,6 +246,10 @@ func main() { if !helpVisible { ui.Render(proc) } + case <-zoomed: + if !helpVisible { + ui.Render(ui.Body) + } case <-drawTick.C: if !helpVisible { ui.Render(ui.Body) diff --git a/termui/linegraph.go b/termui/linegraph.go index 4b01691..01d8414 100644 --- a/termui/linegraph.go +++ b/termui/linegraph.go @@ -12,6 +12,7 @@ type LineGraph struct { *Block Data map[string][]float64 LineColor map[string]Color + Zoom int DefaultLineColor Color } @@ -22,6 +23,7 @@ func NewLineGraph() *LineGraph { Block: NewBlock(), Data: make(map[string][]float64), LineColor: make(map[string]Color), + Zoom: 5, DefaultLineColor: Theme.LineGraph, } @@ -61,9 +63,18 @@ func (lc *LineGraph) Buffer() *Buffer { lastY, lastX := -1, -1 // assign colors to `colors` and lines/points to the canvas for i := len(seriesData) - 1; i >= 0; i-- { - x := ((lc.X + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * 5) + x := ((lc.X + 1) * 2) - 1 - (((len(seriesData) - 1) - i) * lc.Zoom) y := ((lc.Y + 1) * 4) - 1 - int((float64((lc.Y)*4)-1)*(seriesData[i]/100)) - if x < 0 { // stop rendering at the left-most wall + if x < 0 { + // render the line to the last point up to the wall + if x > 0-lc.Zoom { + for _, p := range drawille.Line(lastX, lastY, x, y) { + if p.X > 0 { + c.Set(p.X, p.Y) + colors[p.X/2][p.Y/4] = seriesLineColor + } + } + } break } if lastY == -1 { // if this is the first point diff --git a/widgets/cpu.go b/widgets/cpu.go index 82c097c..6065279 100644 --- a/widgets/cpu.go +++ b/widgets/cpu.go @@ -14,7 +14,7 @@ type CPU struct { interval time.Duration } -func NewCPU(interval time.Duration) *CPU { +func NewCPU(interval time.Duration, zoom int) *CPU { count, _ := psCPU.Counts(false) c := &CPU{ LineGraph: ui.NewLineGraph(), @@ -22,6 +22,7 @@ func NewCPU(interval time.Duration) *CPU { interval: interval, } c.Label = "CPU Usage" + c.Zoom = zoom for i := 0; i < c.count; i++ { key := "CPU" + strconv.Itoa(i+1) c.Data[key] = []float64{0} diff --git a/widgets/help.go b/widgets/help.go index ff5c716..faa64b9 100644 --- a/widgets/help.go +++ b/widgets/help.go @@ -9,7 +9,7 @@ import ( const KEYBINDS = ` Quit: q or -Navigation +Process Navigation - / and j/k: up and down - and : up and down half a page - and : up and down a full page @@ -22,6 +22,8 @@ Process Sorting : toggle process grouping dd: kill the selected process or process group + +h and l: zoom in and out of CPU and Mem graphs ` type HelpMenu struct { @@ -31,7 +33,7 @@ type HelpMenu struct { func NewHelpMenu() *HelpMenu { block := ui.NewBlock() block.X = 48 // width - 1 - block.Y = 15 // height - 1 + block.Y = 17 // height - 1 block.XOffset = (ui.Body.Width - block.X) / 2 // X coordinate block.YOffset = (ui.Body.Height - block.Y) / 2 // Y coordinate return &HelpMenu{block} diff --git a/widgets/mem.go b/widgets/mem.go index 70bf9ae..073d9e6 100644 --- a/widgets/mem.go +++ b/widgets/mem.go @@ -12,12 +12,13 @@ type Mem struct { interval time.Duration } -func NewMem(interval time.Duration) *Mem { +func NewMem(interval time.Duration, zoom int) *Mem { m := &Mem{ LineGraph: ui.NewLineGraph(), interval: interval, } m.Label = "Memory Usage" + m.Zoom = zoom m.Data["Main"] = []float64{0} m.Data["Swap"] = []float64{0}