mirror of
https://github.com/cjbassi/gotop.git
synced 2025-05-03 22:17:15 +08:00
Removed network logic from termui sparkline
This commit is contained in:
parent
c483016c16
commit
3a695dede9
@ -1,16 +1,12 @@
|
|||||||
package termui
|
package termui
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/cjbassi/gotop/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
var SPARKS = [8]rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
|
var SPARKS = [8]rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
|
||||||
|
|
||||||
// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers.
|
// Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers.
|
||||||
type Sparkline struct {
|
type Sparkline struct {
|
||||||
Data []int
|
Data []int
|
||||||
Title string
|
Title1 string
|
||||||
|
Title2 string
|
||||||
TitleColor Attribute
|
TitleColor Attribute
|
||||||
Total int
|
Total int
|
||||||
LineColor Attribute
|
LineColor Attribute
|
||||||
@ -52,47 +48,16 @@ func (sl *Sparklines) Buffer() *Buffer {
|
|||||||
// for each line
|
// for each line
|
||||||
for i, line := range sl.Lines {
|
for i, line := range sl.Lines {
|
||||||
|
|
||||||
// Total and current
|
title1Y := 2 + (sl.Y/lc)*i
|
||||||
y := 2 + (sl.Y/lc)*i
|
title2Y := (2 + (sl.Y/lc)*i) + 1
|
||||||
total := ""
|
|
||||||
title := ""
|
|
||||||
current := ""
|
|
||||||
|
|
||||||
cur := line.Data[len(line.Data)-1]
|
title1 := MaxString(line.Title1, sl.X)
|
||||||
curMag := "B"
|
title2 := MaxString(line.Title2, sl.X)
|
||||||
if cur >= 1000000 {
|
buf.SetString(1, title1Y, title1, line.TitleColor|AttrBold, sl.Bg)
|
||||||
cur = int(utils.BytesToMB(cur))
|
buf.SetString(1, title2Y, title2, line.TitleColor|AttrBold, sl.Bg)
|
||||||
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)
|
|
||||||
|
|
||||||
// sparkline
|
// sparkline
|
||||||
y = (sl.Y / lc) * (i + 1)
|
sparkY := (sl.Y / lc) * (i + 1)
|
||||||
// finds max used for relative heights
|
// finds max used for relative heights
|
||||||
max := 1
|
max := 1
|
||||||
for i := len(line.Data) - 1; i >= 0 && sl.X-((len(line.Data)-1)-i) >= 1; i-- {
|
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) {
|
if (sl.X - x) < len(line.Data) {
|
||||||
char = SPARKS[int((float64(line.Data[(len(line.Data)-1)-(sl.X-x)])/float64(max))*7)]
|
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
|
package widgets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
ui "github.com/cjbassi/gotop/termui"
|
ui "github.com/cjbassi/gotop/termui"
|
||||||
|
"github.com/cjbassi/gotop/utils"
|
||||||
ps "github.com/shirou/gopsutil/net"
|
ps "github.com/shirou/gopsutil/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Net struct {
|
type Net struct {
|
||||||
*ui.Sparklines
|
*ui.Sparklines
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
|
recvTotal int
|
||||||
|
sentTotal int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNet() *Net {
|
func NewNet() *Net {
|
||||||
recv := ui.NewSparkline()
|
recv := ui.NewSparkline()
|
||||||
recv.Title = "Receiving"
|
|
||||||
recv.Data = []int{0}
|
recv.Data = []int{0}
|
||||||
recv.Total = 0
|
|
||||||
|
|
||||||
sent := ui.NewSparkline()
|
sent := ui.NewSparkline()
|
||||||
sent.Title = "Transfering"
|
|
||||||
sent.Data = []int{0}
|
sent.Data = []int{0}
|
||||||
sent.Total = 0
|
|
||||||
|
|
||||||
spark := ui.NewSparklines(recv, sent)
|
spark := ui.NewSparklines(recv, sent)
|
||||||
n := &Net{spark, time.Second}
|
n := &Net{spark, time.Second, 0, 0}
|
||||||
n.Label = "Network Usage"
|
n.Label = "Network Usage"
|
||||||
|
|
||||||
go n.update()
|
go n.update()
|
||||||
@ -43,14 +43,49 @@ func (n *Net) update() {
|
|||||||
recv := int(interfaces[0].BytesRecv)
|
recv := int(interfaces[0].BytesRecv)
|
||||||
sent := int(interfaces[0].BytesSent)
|
sent := int(interfaces[0].BytesSent)
|
||||||
|
|
||||||
if n.Lines[0].Total != 0 { // if this isn't the first update
|
if n.recvTotal != 0 { // if this isn't the first update
|
||||||
curRecv := recv - n.Lines[0].Total
|
curRecv := recv - n.recvTotal
|
||||||
curSent := sent - n.Lines[1].Total
|
curSent := sent - n.sentTotal
|
||||||
|
|
||||||
n.Lines[0].Data = append(n.Lines[0].Data, curRecv)
|
n.Lines[0].Data = append(n.Lines[0].Data, curRecv)
|
||||||
n.Lines[1].Data = append(n.Lines[1].Data, curSent)
|
n.Lines[1].Data = append(n.Lines[1].Data, curSent)
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Lines[0].Total = recv
|
n.recvTotal = recv
|
||||||
n.Lines[1].Total = sent
|
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