Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Added graph zooming; closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbassi committed Mar 10, 2018
1 parent d3bf834 commit 75220b3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
31 changes: 27 additions & 4 deletions gotop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions termui/linegraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type LineGraph struct {
*Block
Data map[string][]float64
LineColor map[string]Color
Zoom int

DefaultLineColor Color
}
Expand All @@ -22,6 +23,7 @@ func NewLineGraph() *LineGraph {
Block: NewBlock(),
Data: make(map[string][]float64),
LineColor: make(map[string]Color),
Zoom: 5,

DefaultLineColor: Theme.LineGraph,
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion widgets/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ 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(),
count: count,
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}
Expand Down
6 changes: 4 additions & 2 deletions widgets/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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}
Expand Down
3 changes: 2 additions & 1 deletion widgets/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down

0 comments on commit 75220b3

Please sign in to comment.