summaryrefslogtreecommitdiffstats
path: root/termui
diff options
context:
space:
mode:
authorCaleb Bassi <calebjbassi@gmail.com>2018-02-19 15:02:01 -0800
committerCaleb Bassi <calebjbassi@gmail.com>2018-02-19 15:02:01 -0800
commit3a695dede9f4dd38e2084268caf4422eb8c11f76 (patch)
treeaf3bac9610c857444b3f932d4814f345c5464cc2 /termui
parentc483016c16febc60b4d7f4b389c42b7ac093e9ab (diff)
Removed network logic from termui sparkline
Diffstat (limited to 'termui')
-rw-r--r--termui/sparkline.go55
1 files changed, 10 insertions, 45 deletions
diff --git a/termui/sparkline.go b/termui/sparkline.go
index 2778505..76d047c 100644
--- a/termui/sparkline.go
+++ b/termui/sparkline.go
@@ -1,16 +1,12 @@
package termui
-import (
- "fmt"
- "github.com/cjbassi/gotop/utils"
-)
-
var SPARKS = [8]rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers.
type Sparkline struct {
Data []int
- Title string
+ Title1 string
+ Title2 string
TitleColor Attribute
Total int
LineColor Attribute
@@ -52,47 +48,16 @@ func (sl *Sparklines) Buffer() *Buffer {
// for each line
for i, line := range sl.Lines {
- // Total and current
- y := 2 + (sl.Y/lc)*i
- total := ""
- title := ""
- current := ""
-
- cur := line.Data[len(line.Data)-1]
- curMag := "B"
- if cur >= 1000000 {
- cur = int(utils.BytesToMB(cur))
- curMag = "MB"
- } else if cur >= 1000 {
- cur = int(utils.BytesToKB(cur))
- curMag = "kB"
- }
-
- t := line.Total
- tMag := "B"
- if t >= 1000000000 {
- t = int(utils.BytesToGB(t))
- tMag = "GB"
- } else if t >= 1000000 {
- t = int(utils.BytesToMB(t))
- tMag = "MB"
- }
-
- if i == 0 {
- total = fmt.Sprintf(" Total Rx: %3d %s", t, tMag)
- current = fmt.Sprintf(" Rx/s: %7d %2s/s", cur, curMag)
- } else {
- total = fmt.Sprintf(" Total Tx: %3d %s", t, tMag)
- current = fmt.Sprintf(" Tx/s: %7d %2s/s", cur, curMag)
- }
+ title1Y := 2 + (sl.Y/lc)*i
+ title2Y := (2 + (sl.Y/lc)*i) + 1
- total = MaxString(total, sl.X)
- title = MaxString(current, sl.X)
- buf.SetString(1, y, total, line.TitleColor|AttrBold, sl.Bg)
- buf.SetString(1, y+1, title, line.TitleColor|AttrBold, sl.Bg)
+ title1 := MaxString(line.Title1, sl.X)
+ title2 := MaxString(line.Title2, sl.X)
+ buf.SetString(1, title1Y, title1, line.TitleColor|AttrBold, sl.Bg)
+ buf.SetString(1, title2Y, title2, line.TitleColor|AttrBold, sl.Bg)
// sparkline
- y = (sl.Y / lc) * (i + 1)
+ sparkY := (sl.Y / lc) * (i + 1)
// finds max used for relative heights
max := 1
for i := len(line.Data) - 1; i >= 0 && sl.X-((len(line.Data)-1)-i) >= 1; i-- {
@@ -106,7 +71,7 @@ func (sl *Sparklines) Buffer() *Buffer {
if (sl.X - x) < len(line.Data) {
char = SPARKS[int((float64(line.Data[(len(line.Data)-1)-(sl.X-x)])/float64(max))*7)]
}
- buf.SetCell(x, y, Cell{char, line.LineColor, sl.Bg})
+ buf.SetCell(x, sparkY, Cell{char, line.LineColor, sl.Bg})
}
}