diff --git a/container.go b/container.go index c23d7eb..d102b2d 100644 --- a/container.go +++ b/container.go @@ -28,7 +28,7 @@ func (c *Container) Collect(client *docker.Client) { go func() { for s := range c.stats { c.reader.Read(s) - c.widgets.SetCPU(c.reader.CPUUtil) + c.widgets.cpu.Set(c.reader.CPUUtil) c.widgets.SetMem(c.reader.MemUsage, c.reader.MemLimit) c.widgets.SetNet(c.reader.NetRx, c.reader.NetTx) } diff --git a/util.go b/util.go index d570380..7556ff9 100644 --- a/util.go +++ b/util.go @@ -4,8 +4,6 @@ import ( "fmt" "math" "strconv" - - ui "github.com/gizak/termui" ) const ( @@ -33,13 +31,3 @@ func byteFormat(n int64) string { func round(num float64) int { return int(num + math.Copysign(0.5, num)) } - -func colorScale(n int) ui.Attribute { - if n > 70 { - return ui.ColorRed - } - if n > 30 { - return ui.ColorYellow - } - return ui.ColorGreen -} diff --git a/widgets.go b/widgets.go index 7cdfa5a..712f2e2 100644 --- a/widgets.go +++ b/widgets.go @@ -2,8 +2,8 @@ package main import ( "fmt" - "strconv" + "github.com/bcicen/ctop/widgets" ui "github.com/gizak/termui" ) @@ -11,7 +11,7 @@ type Widgets struct { cid *ui.Par net *ui.Par name *ui.Par - cpu *ui.Gauge + cpu *widgets.CPU memory *ui.Gauge } @@ -25,16 +25,6 @@ func (w *Widgets) MakeRow() *ui.Row { ) } -func (w *Widgets) SetCPU(val int) { - w.cpu.BarColor = colorScale(val) - w.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val)) - if val < 5 { - val = 5 - w.cpu.BarColor = ui.ColorBlack - } - w.cpu.Percent = val -} - func (w *Widgets) SetNet(rx int64, tx int64) { w.net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx)) } @@ -71,7 +61,7 @@ func NewWidgets(id string, names string) *Widgets { net.Width = 20 net.TextFgColor = ui.ColorWhite - return &Widgets{cid, net, name, mkGauge(), mkGauge()} + return &Widgets{cid, net, name, widgets.NewCPU(), mkGauge()} } func mkGauge() *ui.Gauge { diff --git a/widgets/cpu.go b/widgets/cpu.go new file mode 100644 index 0000000..4901cdf --- /dev/null +++ b/widgets/cpu.go @@ -0,0 +1,26 @@ +package widgets + +import ( + "fmt" + "strconv" + + ui "github.com/gizak/termui" +) + +type CPU struct { + *ui.Gauge +} + +func NewCPU() *CPU { + return &CPU{mkGauge()} +} + +func (c *CPU) Set(val int) { + c.BarColor = colorScale(val) + c.Label = fmt.Sprintf("%s%%", strconv.Itoa(val)) + if val < 5 { + val = 5 + c.BarColor = ui.ColorBlack + } + c.Percent = val +} diff --git a/widgets/util.go b/widgets/util.go new file mode 100644 index 0000000..1f2be7d --- /dev/null +++ b/widgets/util.go @@ -0,0 +1,26 @@ +package widgets + +import ( + ui "github.com/gizak/termui" +) + +func mkGauge() *ui.Gauge { + g := ui.NewGauge() + g.Height = 1 + g.Border = false + g.Percent = 0 + g.PaddingBottom = 0 + g.BarColor = ui.ColorGreen + g.Label = "-" + return g +} + +func colorScale(n int) ui.Attribute { + if n > 70 { + return ui.ColorRed + } + if n > 30 { + return ui.ColorYellow + } + return ui.ColorGreen +}