add expanded net, mem widgets

This commit is contained in:
Bradley Cicenas 2017-01-07 20:37:11 +00:00
parent 99aac17030
commit 98a8cecfa1
5 changed files with 138 additions and 39 deletions

View File

@ -1,24 +1,22 @@
package widgets
import (
"fmt"
ui "github.com/gizak/termui"
)
type Expanded struct {
Info *ui.Table
Net *ui.Par
Cpu *ExpandedCpu
Memory *ui.Gauge
Info *ui.Table
Net *ExpandedNet
Cpu *ExpandedCpu
Mem *ExpandedMem
}
func NewExpanded(id, name string) *Expanded {
return &Expanded{
Info: NewInfo(id, name),
Net: ui.NewPar("-"),
Cpu: NewExpandedCpu(),
Memory: mkGauge(),
Info: NewInfo(id, name),
Net: NewExpandedNet(),
Cpu: NewExpandedCpu(),
Mem: NewExpandedMem(),
}
}
@ -36,9 +34,9 @@ func NewInfo(id, name string) *ui.Table {
}
func (w *Expanded) Render() {
ui.Render(w.Info, w.Cpu)
ui.Render(w.Info, w.Cpu, w.Mem, w.Net)
ui.Handle("/timer/1s", func(ui.Event) {
ui.Render(w.Info, w.Cpu)
ui.Render(w.Info, w.Cpu, w.Mem, w.Net)
})
ui.Handle("/sys/kbd/", func(ui.Event) {
ui.StopLoop()
@ -49,7 +47,7 @@ func (w *Expanded) Render() {
func (w *Expanded) Row() *ui.Row {
return ui.NewRow(
ui.NewCol(2, 0, w.Cpu),
ui.NewCol(2, 0, w.Memory),
ui.NewCol(2, 0, w.Mem),
ui.NewCol(2, 0, w.Net),
)
}
@ -65,16 +63,9 @@ func (w *Expanded) SetCPU(val int) {
}
func (w *Expanded) SetNet(rx int64, tx int64) {
w.Net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
w.Net.Update(rx, tx)
}
func (w *Expanded) SetMem(val int64, limit int64, percent int) {
w.Memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
if percent < 5 {
percent = 5
w.Memory.BarColor = ui.ColorBlack
} else {
w.Memory.BarColor = ui.ColorGreen
}
w.Memory.Percent = percent
w.Mem.Update(int(val), int(limit))
}

View File

@ -5,26 +5,24 @@ import (
)
type ExpandedCpu struct {
*ui.BarChart
hist HistData
*ui.LineChart
hist FloatHistData
}
func NewExpandedCpu() *ExpandedCpu {
cpu := &ExpandedCpu{ui.NewBarChart(), NewHistData(12)}
cpu.BorderLabel = "CPU Util"
cpu := &ExpandedCpu{ui.NewLineChart(), NewFloatHistData(60)}
cpu.BorderLabel = "CPU"
cpu.Height = 10
cpu.Width = 50
cpu.BarColor = ui.ColorGreen
cpu.BarWidth = 3
cpu.BarGap = 1
cpu.X = 0
cpu.Y = 4
cpu.Data = cpu.hist.data
cpu.DataLabels = cpu.hist.labels
cpu.AxesColor = ui.ColorDefault
cpu.LineColor = ui.ColorGreen
return cpu
}
func (w *ExpandedCpu) Update(val int) {
w.hist.Append(val)
w.Data = w.hist.data
w.hist.Append(float64(val))
}

41
widgets/expanded_mem.go Normal file
View File

@ -0,0 +1,41 @@
package widgets
import (
ui "github.com/gizak/termui"
)
type ExpandedMem struct {
*ui.MBarChart
valHist IntHistData
limitHist IntHistData
}
func NewExpandedMem() *ExpandedMem {
mem := &ExpandedMem{
ui.NewMBarChart(),
NewIntHistData(8),
NewIntHistData(8),
}
mem.BorderLabel = "MEM"
mem.Height = 10
mem.Width = 50
mem.BarWidth = 5
mem.BarGap = 1
mem.X = 51
mem.Y = 4
mem.TextColor = ui.ColorDefault
mem.Data[0] = mem.valHist.data
mem.Data[0] = mem.valHist.data
mem.Data[1] = mem.limitHist.data
mem.BarColor[0] = ui.ColorGreen
mem.BarColor[1] = ui.ColorBlack
mem.DataLabels = mem.valHist.labels
//mem.ShowScale = true
return mem
}
func (w *ExpandedMem) Update(val int, limit int) {
w.valHist.Append(val)
w.limitHist.Append(limit - val)
//w.Data[0] = w.hist.data
}

45
widgets/expanded_net.go Normal file
View File

@ -0,0 +1,45 @@
package widgets
import (
"fmt"
ui "github.com/gizak/termui"
)
type ExpandedNet struct {
*ui.Sparklines
rxHist IntHistData
txHist IntHistData
}
func NewExpandedNet() *ExpandedNet {
net := &ExpandedNet{ui.NewSparklines(), NewIntHistData(60), NewIntHistData(60)}
rx := ui.NewSparkline()
rx.Title = "RX"
rx.Height = 3
rx.Data = net.rxHist.data
rx.TitleColor = ui.ColorDefault
rx.LineColor = ui.ColorGreen
tx := ui.NewSparkline()
tx.Title = "TX"
tx.Height = 3
tx.Data = net.txHist.data
tx.TitleColor = ui.ColorDefault
tx.LineColor = ui.ColorGreen
net.Lines = []ui.Sparkline{rx, tx}
net.Height = 12
net.Width = 50
net.X = 0
net.Y = 15
return net
}
func (w *ExpandedNet) Update(rx int64, tx int64) {
w.rxHist.Append(int(rx))
w.txHist.Append(int(tx))
w.Lines[0].Title = fmt.Sprintf("RX (%s)", byteFormat(rx))
w.Lines[1].Title = fmt.Sprintf("TX (%s)", byteFormat(tx))
}

View File

@ -1,21 +1,45 @@
package widgets
type HistData struct {
data []int
labels []string
maxSize int
maxLen int
labels []string
}
func NewHistData(max int) HistData {
return HistData{
data: make([]int, max),
labels: make([]string, max),
maxSize: max,
maxLen: max,
labels: make([]string, max),
}
}
func (h HistData) Append(val int) {
if len(h.data) >= h.maxSize {
type IntHistData struct {
HistData
data []int
}
func NewIntHistData(max int) IntHistData {
return IntHistData{NewHistData(max), make([]int, max)}
}
func (h IntHistData) Append(val int) {
if len(h.data) >= h.maxLen {
h.data = append(h.data[:0], h.data[1:]...)
}
h.data = append(h.data, val)
}
type FloatHistData struct {
HistData
data []float64
}
func NewFloatHistData(max int) FloatHistData {
return FloatHistData{NewHistData(max), make([]float64, max)}
}
func (h FloatHistData) Append(val float64) {
if len(h.data) >= h.maxLen {
h.data = append(h.data[:0], h.data[1:]...)
}
h.data = append(h.data, val)