mirror of
https://github.com/bcicen/ctop.git
synced 2025-11-11 21:55:40 +01:00
convert expanded mem widget to mbarchart, add innerlabel
This commit is contained in:
@@ -1,41 +1,87 @@
|
||||
package expanded
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bcicen/ctop/cwidgets"
|
||||
ui "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
type ExpandedMem struct {
|
||||
*ui.BarChart
|
||||
hist *IntHist
|
||||
type Mem struct {
|
||||
*ui.Block
|
||||
Chart *ui.MBarChart
|
||||
InnerLabel *ui.Par
|
||||
valHist *IntHist
|
||||
limitHist *IntHist
|
||||
}
|
||||
|
||||
func NewExpandedMem() *ExpandedMem {
|
||||
mem := &ExpandedMem{
|
||||
ui.NewBarChart(),
|
||||
NewIntHist(10),
|
||||
func NewMem() *Mem {
|
||||
mem := &Mem{
|
||||
Block: ui.NewBlock(),
|
||||
Chart: newMemChart(),
|
||||
InnerLabel: newMemLabel(),
|
||||
valHist: NewIntHist(9),
|
||||
limitHist: NewIntHist(9),
|
||||
}
|
||||
mem.BorderLabel = "MEM"
|
||||
mem.Height = 10
|
||||
mem.Height = 13
|
||||
mem.Width = colWidth[0]
|
||||
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
|
||||
mem.BorderLabel = "MEM"
|
||||
|
||||
mem.Chart.Data[0] = mem.valHist.Data
|
||||
mem.Chart.Data[1] = mem.limitHist.Data
|
||||
mem.Chart.DataLabels = mem.valHist.Labels
|
||||
|
||||
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)
|
||||
func (w *Mem) Align() {
|
||||
y := w.Y + 1
|
||||
w.InnerLabel.SetY(y)
|
||||
w.Chart.SetY(y + w.InnerLabel.Height)
|
||||
|
||||
w.Chart.Height = w.Height - w.InnerLabel.Height - 2
|
||||
w.Chart.SetWidth(w.Width - 2)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user