convert expanded mem widget to mbarchart, add innerlabel

This commit is contained in:
Bradley Cicenas 2017-03-06 23:58:04 +00:00
parent d94aed1531
commit 8e995a37c6
4 changed files with 97 additions and 44 deletions

View File

@ -4,25 +4,31 @@ import (
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
type ExpandedCpu struct { type Cpu struct {
*ui.LineChart *ui.LineChart
hist FloatHist hist FloatHist
} }
func NewExpandedCpu() *ExpandedCpu { func NewCpu() *Cpu {
cpu := &ExpandedCpu{ui.NewLineChart(), NewFloatHist(60)} cpu := &Cpu{ui.NewLineChart(), NewFloatHist(55)}
cpu.Mode = "dot"
cpu.BorderLabel = "CPU" cpu.BorderLabel = "CPU"
cpu.Height = 10 cpu.Height = 12
cpu.Width = colWidth[0] cpu.Width = colWidth[0]
cpu.X = 0 cpu.X = 0
cpu.Y = 6
cpu.Data = cpu.hist.Data
cpu.DataLabels = cpu.hist.Labels cpu.DataLabels = cpu.hist.Labels
cpu.AxesColor = ui.ColorDefault cpu.AxesColor = ui.ColorDefault
cpu.LineColor = ui.ColorGreen cpu.LineColor = ui.ColorGreen
// hack to force the default minY scale to 0
tmpData := []float64{20}
cpu.Data = tmpData
_ = cpu.Buffer()
cpu.Data = cpu.hist.Data
return cpu return cpu
} }
func (w *ExpandedCpu) Update(val int) { func (w *Cpu) Update(val int) {
w.hist.Append(float64(val)) w.hist.Append(float64(val))
} }

View File

@ -9,14 +9,14 @@ import (
var ( var (
log = logging.Init() log = logging.Init()
sizeError = termSizeError() sizeError = termSizeError()
colWidth = [2]int{60, 0} // left,right column width colWidth = [2]int{65, 0} // left,right column width
) )
type Expanded struct { type Expanded struct {
Info *Info Info *Info
Net *ExpandedNet Net *Net
Cpu *ExpandedCpu Cpu *Cpu
Mem *ExpandedMem Mem *Mem
Width int Width int
} }
@ -26,9 +26,9 @@ func NewExpanded(id string) *Expanded {
} }
return &Expanded{ return &Expanded{
Info: NewInfo(id), Info: NewInfo(id),
Net: NewExpandedNet(), Net: NewNet(),
Cpu: NewExpandedCpu(), Cpu: NewCpu(),
Mem: NewExpandedMem(), Mem: NewMem(),
Width: ui.TermWidth(), Width: ui.TermWidth(),
} }
} }
@ -56,6 +56,7 @@ func (e *Expanded) Align() {
if e.Width > colWidth[0] { if e.Width > colWidth[0] {
colWidth[1] = e.Width - (colWidth[0] + 1) colWidth[1] = e.Width - (colWidth[0] + 1)
} }
e.Mem.Align()
log.Debugf("align: width=%v left-col=%v right-col=%v", e.Width, colWidth[0], colWidth[1]) log.Debugf("align: width=%v left-col=%v right-col=%v", e.Width, colWidth[0], colWidth[1])
} }

View File

@ -1,41 +1,87 @@
package expanded package expanded
import ( import (
"fmt"
"github.com/bcicen/ctop/cwidgets" "github.com/bcicen/ctop/cwidgets"
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
type ExpandedMem struct { type Mem struct {
*ui.BarChart *ui.Block
hist *IntHist Chart *ui.MBarChart
InnerLabel *ui.Par
valHist *IntHist
limitHist *IntHist
} }
func NewExpandedMem() *ExpandedMem { func NewMem() *Mem {
mem := &ExpandedMem{ mem := &Mem{
ui.NewBarChart(), Block: ui.NewBlock(),
NewIntHist(10), Chart: newMemChart(),
InnerLabel: newMemLabel(),
valHist: NewIntHist(9),
limitHist: NewIntHist(9),
} }
mem.BorderLabel = "MEM" mem.Height = 13
mem.Height = 10
mem.Width = colWidth[0] mem.Width = colWidth[0]
mem.BarWidth = 5 mem.BorderLabel = "MEM"
mem.BarGap = 1
mem.X = 0 mem.Chart.Data[0] = mem.valHist.Data
mem.Y = 14 mem.Chart.Data[1] = mem.limitHist.Data
mem.TextColor = ui.ColorDefault mem.Chart.DataLabels = mem.valHist.Labels
mem.Data = mem.hist.Data
mem.BarColor = ui.ColorGreen
mem.DataLabels = mem.hist.Labels
mem.NumFmt = cwidgets.ByteFormatInt
return mem return mem
} }
func (w *ExpandedMem) Update(val int, limit int) { func (w *Mem) Align() {
// implement our own scaling for mem graph y := w.Y + 1
if val*4 < limit { w.InnerLabel.SetY(y)
w.SetMax(val * 4) w.Chart.SetY(y + w.InnerLabel.Height)
} else {
w.SetMax(limit) w.Chart.Height = w.Height - w.InnerLabel.Height - 2
} w.Chart.SetWidth(w.Width - 2)
w.hist.Append(val) }
func (w *Mem) Buffer() ui.Buffer {
buf := ui.NewBuffer()
buf.Merge(w.Block.Buffer())
buf.Merge(w.InnerLabel.Buffer())
buf.Merge(w.Chart.Buffer())
return buf
}
func newMemLabel() *ui.Par {
p := ui.NewPar("-")
p.X = 1
p.Border = false
p.Height = 1
p.Width = 20
p.TextFgColor = ui.ColorDefault
return p
}
func newMemChart() *ui.MBarChart {
mbar := ui.NewMBarChart()
mbar.X = 1
mbar.Border = false
mbar.BarGap = 1
mbar.BarWidth = 6
mbar.TextColor = ui.ColorDefault
mbar.BarColor[0] = ui.ColorGreen
mbar.BarColor[1] = ui.ColorBlack
mbar.NumColor[1] = ui.ColorBlack
mbar.NumFmt = cwidgets.ByteFormatInt
//mbar.ShowScale = true
return mbar
}
func (w *Mem) Update(val int, limit int) {
w.valHist.Append(val)
w.limitHist.Append(limit - val)
w.InnerLabel.Text = fmt.Sprintf("%v / %v", cwidgets.ByteFormatInt(val), cwidgets.ByteFormatInt(limit))
//w.Data[0] = w.hist.data
} }

View File

@ -8,14 +8,14 @@ import (
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
type ExpandedNet struct { type Net struct {
*ui.Sparklines *ui.Sparklines
rxHist *DiffHist rxHist *DiffHist
txHist *DiffHist txHist *DiffHist
} }
func NewExpandedNet() *ExpandedNet { func NewNet() *Net {
net := &ExpandedNet{ui.NewSparklines(), NewDiffHist(50), NewDiffHist(50)} net := &Net{ui.NewSparklines(), NewDiffHist(60), NewDiffHist(60)}
net.BorderLabel = "NET" net.BorderLabel = "NET"
net.Height = 6 net.Height = 6
net.Width = colWidth[0] net.Width = colWidth[0]
@ -40,7 +40,7 @@ func NewExpandedNet() *ExpandedNet {
return net return net
} }
func (w *ExpandedNet) Update(rx int64, tx int64) { func (w *Net) Update(rx int64, tx int64) {
var rate string var rate string
w.rxHist.Append(int(rx)) w.rxHist.Append(int(rx))