mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor cpu gauge into standalone widget
This commit is contained in:
parent
11376c24e4
commit
3ec384414d
@ -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)
|
||||
}
|
||||
|
12
util.go
12
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
|
||||
}
|
||||
|
16
widgets.go
16
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 {
|
||||
|
26
widgets/cpu.go
Normal file
26
widgets/cpu.go
Normal file
@ -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
|
||||
}
|
26
widgets/util.go
Normal file
26
widgets/util.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user