mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
refactor cpu gauge into standalone widget
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user