Removed network logic from termui sparkline
This commit is contained in:
parent
c483016c16
commit
3a695dede9
@ -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 := ""
|
||||
title1Y := 2 + (sl.Y/lc)*i
|
||||
title2Y := (2 + (sl.Y/lc)*i) + 1
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,30 +1,30 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
ui "github.com/cjbassi/gotop/termui"
|
||||
"github.com/cjbassi/gotop/utils"
|
||||
ps "github.com/shirou/gopsutil/net"
|
||||
)
|
||||
|
||||
type Net struct {
|
||||
*ui.Sparklines
|
||||
interval time.Duration
|
||||
interval time.Duration
|
||||
recvTotal int
|
||||
sentTotal int
|
||||
}
|
||||
|
||||
func NewNet() *Net {
|
||||
recv := ui.NewSparkline()
|
||||
recv.Title = "Receiving"
|
||||
recv.Data = []int{0}
|
||||
recv.Total = 0
|
||||
|
||||
sent := ui.NewSparkline()
|
||||
sent.Title = "Transfering"
|
||||
sent.Data = []int{0}
|
||||
sent.Total = 0
|
||||
|
||||
spark := ui.NewSparklines(recv, sent)
|
||||
n := &Net{spark, time.Second}
|
||||
n := &Net{spark, time.Second, 0, 0}
|
||||
n.Label = "Network Usage"
|
||||
|
||||
go n.update()
|
||||
@ -43,14 +43,49 @@ func (n *Net) update() {
|
||||
recv := int(interfaces[0].BytesRecv)
|
||||
sent := int(interfaces[0].BytesSent)
|
||||
|
||||
if n.Lines[0].Total != 0 { // if this isn't the first update
|
||||
curRecv := recv - n.Lines[0].Total
|
||||
curSent := sent - n.Lines[1].Total
|
||||
if n.recvTotal != 0 { // if this isn't the first update
|
||||
curRecv := recv - n.recvTotal
|
||||
curSent := sent - n.sentTotal
|
||||
|
||||
n.Lines[0].Data = append(n.Lines[0].Data, curRecv)
|
||||
n.Lines[1].Data = append(n.Lines[1].Data, curSent)
|
||||
}
|
||||
|
||||
n.Lines[0].Total = recv
|
||||
n.Lines[1].Total = sent
|
||||
n.recvTotal = recv
|
||||
n.sentTotal = sent
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
var method string
|
||||
var total int
|
||||
cur := n.Lines[i].Data[len(n.Lines[i].Data)-1]
|
||||
totalUnit := "B"
|
||||
curUnit := "B"
|
||||
|
||||
if i == 0 {
|
||||
total = n.recvTotal
|
||||
method = "Rx"
|
||||
} else {
|
||||
total = n.sentTotal
|
||||
method = "Tx"
|
||||
}
|
||||
|
||||
if cur >= 1000000 {
|
||||
cur = int(utils.BytesToMB(cur))
|
||||
curUnit = "MB"
|
||||
} else if cur >= 1000 {
|
||||
cur = int(utils.BytesToKB(cur))
|
||||
curUnit = "kB"
|
||||
}
|
||||
|
||||
if total >= 1000000000 {
|
||||
total = int(utils.BytesToGB(total))
|
||||
totalUnit = "GB"
|
||||
} else if total >= 1000000 {
|
||||
total = int(utils.BytesToMB(total))
|
||||
totalUnit = "MB"
|
||||
}
|
||||
|
||||
n.Lines[i].Title1 = fmt.Sprintf(" Total %s: %3d %s", method, total, totalUnit)
|
||||
n.Lines[i].Title2 = fmt.Sprintf(" %s/s: %7d %2s/s", method, cur, curUnit)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user