Added graph zooming; closes #3

This commit is contained in:
Caleb Bassi 2018-03-09 16:29:05 -08:00
parent d3bf834aa3
commit 75220b3db8
6 changed files with 50 additions and 11 deletions

View File

@ -37,7 +37,7 @@ go get github.com/cjbassi/gotop
### Keybinds
* Quit: `q` or `<C-c>`
* Navigation:
* Process Navigation:
* `<up>`/`<down>` and `j`/`k`: up and down
* `<C-d>` and `<C-u>`: up and down half a page
* `<C-f>` and `<C-b>`: up and down a full page
@ -48,6 +48,7 @@ go get github.com/cjbassi/gotop
* `p`: PID
* `<tab>`: 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

View File

@ -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
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)

View File

@ -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

View File

@ -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}

View File

@ -9,7 +9,7 @@ import (
const KEYBINDS = `
Quit: q or <C-c>
Navigation
Process Navigation
- <up>/<down> and j/k: up and down
- <C-d> and <C-u>: up and down half a page
- <C-f> and <C-b>: up and down a full page
@ -22,6 +22,8 @@ Process Sorting
<tab>: 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}

View File

@ -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}