refactor cpu gauge into standalone widget

This commit is contained in:
Bradley Cicenas
2017-01-06 02:01:57 +00:00
parent 11376c24e4
commit 3ec384414d
5 changed files with 56 additions and 26 deletions

26
widgets/cpu.go Normal file
View 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
View 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
}