update expanded net labels

This commit is contained in:
Bradley Cicenas
2017-01-08 16:13:25 +00:00
parent 3b0373b34f
commit 72066aba6e
2 changed files with 17 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package widgets
import (
"fmt"
"strings"
ui "github.com/gizak/termui"
)
@ -14,6 +15,11 @@ type ExpandedNet struct {
func NewExpandedNet() *ExpandedNet {
net := &ExpandedNet{ui.NewSparklines(), NewDiffHistData(30), NewDiffHistData(30)}
net.BorderLabel = "NET"
net.Height = 8
net.Width = 35
net.X = 0
net.Y = 15
rx := ui.NewSparkline()
rx.Title = "RX"
@ -30,17 +36,17 @@ func NewExpandedNet() *ExpandedNet {
tx.LineColor = ui.ColorYellow
net.Lines = []ui.Sparkline{rx, tx}
net.Height = 8
net.Width = 35
net.X = 0
net.Y = 15
return net
}
func (w *ExpandedNet) Update(rx int64, tx int64) {
var rate string
w.rxHist.Append(int(rx))
w.Lines[0].Title = fmt.Sprintf("RX [%s/s]", byteFormat(int64(w.rxHist.Last())))
rate = strings.ToLower(byteFormatInt(w.rxHist.Last()))
w.Lines[0].Title = fmt.Sprintf("RX [%s/s]", rate)
w.txHist.Append(int(tx))
w.Lines[1].Title = fmt.Sprintf("TX [%s/s]", byteFormat(int64(w.txHist.Last())))
rate = strings.ToLower(byteFormatInt(w.txHist.Last()))
w.Lines[1].Title = fmt.Sprintf("TX [%s/s]", rate)
}

View File

@ -13,6 +13,11 @@ const (
gb = mb * 1024
)
// convenience method
func byteFormatInt(n int) string {
return byteFormat(int64(n))
}
func byteFormat(n int64) string {
if n < kb {
return fmt.Sprintf("%sB", strconv.FormatInt(n, 10))