2016-12-25 19:06:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
2016-12-25 22:39:16 +00:00
|
|
|
type Widgets struct {
|
|
|
|
cid *ui.Par
|
2016-12-26 18:39:15 +00:00
|
|
|
names *ui.Par
|
2016-12-25 22:39:16 +00:00
|
|
|
cpu *ui.Gauge
|
2016-12-26 17:57:55 +00:00
|
|
|
net *ui.Gauge
|
2016-12-25 22:39:16 +00:00
|
|
|
memory *ui.Gauge
|
2016-12-25 19:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 02:24:02 +00:00
|
|
|
func (w *Widgets) MakeRow() *ui.Row {
|
|
|
|
return ui.NewRow(
|
|
|
|
ui.NewCol(1, 0, w.cid),
|
|
|
|
ui.NewCol(2, 0, w.cpu),
|
|
|
|
ui.NewCol(2, 0, w.memory),
|
|
|
|
ui.NewCol(2, 0, w.net),
|
|
|
|
ui.NewCol(2, 0, w.names),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-12-25 22:39:16 +00:00
|
|
|
func (w *Widgets) SetCPU(val int) {
|
|
|
|
w.cpu.BarColor = colorScale(val)
|
|
|
|
w.cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
|
|
|
|
if val < 5 && val > 0 {
|
|
|
|
val = 5
|
|
|
|
}
|
|
|
|
w.cpu.Percent = val
|
2016-12-25 19:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 17:57:55 +00:00
|
|
|
func (w *Widgets) SetNet(rx int64, tx int64) {
|
|
|
|
w.net.Label = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
|
|
|
|
}
|
|
|
|
|
2016-12-25 22:39:16 +00:00
|
|
|
func (w *Widgets) SetMem(val int64, limit int64) {
|
|
|
|
if val < 5 {
|
|
|
|
val = 5
|
|
|
|
}
|
|
|
|
w.memory.Percent = round((float64(val) / float64(limit)) * 100)
|
|
|
|
w.memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
|
2016-12-25 19:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 02:24:02 +00:00
|
|
|
func NewWidgets(id string, names string) *Widgets {
|
2016-12-25 19:06:57 +00:00
|
|
|
cid := ui.NewPar(id)
|
|
|
|
cid.Border = false
|
|
|
|
cid.Height = 1
|
2016-12-25 23:05:22 +00:00
|
|
|
cid.Width = 20
|
2016-12-25 19:06:57 +00:00
|
|
|
cid.TextFgColor = ui.ColorWhite
|
2016-12-27 02:24:02 +00:00
|
|
|
name := ui.NewPar(names)
|
|
|
|
name.Border = false
|
|
|
|
name.Height = 1
|
|
|
|
name.Width = 20
|
|
|
|
name.TextFgColor = ui.ColorWhite
|
|
|
|
return &Widgets{cid, name, mkGauge(), mkGauge(), mkGauge()}
|
2016-12-25 19:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|