refactor all container widgets into subpackage

This commit is contained in:
Bradley Cicenas
2017-02-26 22:04:24 +00:00
parent 4aaf26b63d
commit 70f2648952
14 changed files with 348 additions and 317 deletions

28
cwidgets/expanded/cpu.go Normal file
View File

@@ -0,0 +1,28 @@
package expanded
import (
ui "github.com/gizak/termui"
)
type ExpandedCpu struct {
*ui.LineChart
hist FloatHist
}
func NewExpandedCpu() *ExpandedCpu {
cpu := &ExpandedCpu{ui.NewLineChart(), NewFloatHist(60)}
cpu.BorderLabel = "CPU"
cpu.Height = 10
cpu.Width = 50
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(float64(val))
}

74
cwidgets/expanded/hist.go Normal file
View File

@@ -0,0 +1,74 @@
package expanded
type IntHist struct {
data []int
labels []string
}
func NewIntHist(max int) IntHist {
return IntHist{
data: make([]int, max),
labels: make([]string, max),
}
}
func (h IntHist) Append(val int) {
if len(h.data) == cap(h.data) {
h.data = append(h.data[:0], h.data[1:]...)
}
h.data = append(h.data, val)
}
type FloatHist struct {
data []float64
labels []string
}
func NewFloatHist(max int) FloatHist {
return FloatHist{
data: make([]float64, max),
labels: make([]string, max),
}
}
func (h FloatHist) Append(val float64) {
if len(h.data) == cap(h.data) {
h.data = append(h.data[:0], h.data[1:]...)
}
h.data = append(h.data, val)
}
type DiffHist struct {
data []int // data point derivatives
srcData []int // principal input data
labels []string
}
func NewDiffHist(max int) DiffHist {
return DiffHist{
data: make([]int, max),
srcData: make([]int, max),
labels: make([]string, max),
}
}
// return most recent value
func (h DiffHist) Last() int {
return h.data[len(h.data)-1]
}
func (h DiffHist) Append(val int) {
if len(h.data) == cap(h.data) {
h.data = append(h.data[:0], h.data[1:]...)
}
if len(h.srcData) == cap(h.srcData) {
h.srcData = append(h.srcData[:0], h.srcData[1:]...)
}
diff := val - h.srcData[len(h.srcData)-1]
if diff != val { // skip adding to data if this is the initial update
h.data = append(h.data, diff)
}
h.srcData = append(h.srcData, val)
}

68
cwidgets/expanded/main.go Normal file
View File

@@ -0,0 +1,68 @@
package expanded
import (
ui "github.com/gizak/termui"
)
type Expanded struct {
Info *ui.Table
Net *ExpandedNet
Cpu *ExpandedCpu
Mem *ExpandedMem
}
func NewExpanded(id, name string) *Expanded {
return &Expanded{
Info: NewInfo(id, name),
Net: NewExpandedNet(),
Cpu: NewExpandedCpu(),
Mem: NewExpandedMem(),
}
}
func NewInfo(id, name string) *ui.Table {
p := ui.NewTable()
p.Rows = [][]string{
[]string{"name", name},
[]string{"id", id},
}
p.Height = 4
p.Width = 50
p.FgColor = ui.ColorWhite
p.Seperator = false
return p
}
func (w *Expanded) Buffer() ui.Buffer {
return ui.NewBuffer()
}
func (w *Expanded) Reset() {}
func (w *Expanded) SetY(_ int) {}
func (w *Expanded) SetWidth(_ int) {}
func (w *Expanded) Highlight() {}
func (w *Expanded) UnHighlight() {}
func (w *Expanded) SetStatus(val string) {}
func (w *Expanded) Render(_, _ int) {
ui.Render(w.Info, w.Cpu, w.Mem, w.Net)
ui.Handle("/timer/1s", func(ui.Event) {
ui.Render(w.Info, w.Cpu, w.Mem, w.Net)
})
ui.Handle("/sys/kbd/", func(ui.Event) {
ui.StopLoop()
})
ui.Loop()
}
func (w *Expanded) SetCPU(val int) {
w.Cpu.Update(val)
}
func (w *Expanded) SetNet(rx int64, tx int64) {
w.Net.Update(rx, tx)
}
func (w *Expanded) SetMem(val int64, limit int64, percent int) {
w.Mem.Update(int(val), int(limit))
}

41
cwidgets/expanded/mem.go Normal file
View File

@@ -0,0 +1,41 @@
package expanded
import (
"github.com/bcicen/ctop/cwidgets"
ui "github.com/gizak/termui"
)
type ExpandedMem struct {
*ui.BarChart
hist IntHist
}
func NewExpandedMem() *ExpandedMem {
mem := &ExpandedMem{
ui.NewBarChart(),
NewIntHist(8),
}
mem.BorderLabel = "MEM"
mem.Height = 10
mem.Width = 50
mem.BarWidth = 5
mem.BarGap = 1
mem.X = 0
mem.Y = 14
mem.TextColor = ui.ColorDefault
mem.Data = mem.hist.data
mem.BarColor = ui.ColorGreen
mem.DataLabels = mem.hist.labels
mem.NumFmt = cwidgets.ByteFormatInt
return mem
}
func (w *ExpandedMem) Update(val int, limit int) {
// implement our own scaling for mem graph
if val*4 < limit {
w.SetMax(val * 4)
} else {
w.SetMax(limit)
}
w.hist.Append(val)
}

53
cwidgets/expanded/net.go Normal file
View File

@@ -0,0 +1,53 @@
package expanded
import (
"fmt"
"strings"
"github.com/bcicen/ctop/cwidgets"
ui "github.com/gizak/termui"
)
type ExpandedNet struct {
*ui.Sparklines
rxHist DiffHist
txHist DiffHist
}
func NewExpandedNet() *ExpandedNet {
net := &ExpandedNet{ui.NewSparklines(), NewDiffHist(50), NewDiffHist(50)}
net.BorderLabel = "NET"
net.Height = 6
net.Width = 50
net.X = 0
net.Y = 24
rx := ui.NewSparkline()
rx.Title = "RX"
rx.Height = 1
rx.Data = net.rxHist.data
rx.TitleColor = ui.ColorDefault
rx.LineColor = ui.ColorGreen
tx := ui.NewSparkline()
tx.Title = "TX"
tx.Height = 1
tx.Data = net.txHist.data
tx.TitleColor = ui.ColorDefault
tx.LineColor = ui.ColorYellow
net.Lines = []ui.Sparkline{rx, tx}
return net
}
func (w *ExpandedNet) Update(rx int64, tx int64) {
var rate string
w.rxHist.Append(int(rx))
rate = strings.ToLower(cwidgets.ByteFormatInt(w.rxHist.Last()))
w.Lines[0].Title = fmt.Sprintf("RX [%s/s]", rate)
w.txHist.Append(int(tx))
rate = strings.ToLower(cwidgets.ByteFormatInt(w.txHist.Last()))
w.Lines[1].Title = fmt.Sprintf("TX [%s/s]", rate)
}