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-27 15:32:45 +00:00
|
|
|
net *ui.Par
|
|
|
|
name *ui.Par
|
2016-12-25 22:39:16 +00:00
|
|
|
cpu *ui.Gauge
|
|
|
|
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(
|
2016-12-28 03:01:44 +00:00
|
|
|
ui.NewCol(2, 0, w.name),
|
2016-12-27 02:24:02 +00:00
|
|
|
ui.NewCol(1, 0, w.cid),
|
|
|
|
ui.NewCol(2, 0, w.cpu),
|
|
|
|
ui.NewCol(2, 0, w.memory),
|
|
|
|
ui.NewCol(2, 0, w.net),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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))
|
2016-12-27 15:32:45 +00:00
|
|
|
if val < 5 {
|
2016-12-25 22:39:16 +00:00
|
|
|
val = 5
|
2016-12-27 15:32:45 +00:00
|
|
|
w.cpu.BarColor = ui.ColorBlack
|
2016-12-25 22:39:16 +00:00
|
|
|
}
|
|
|
|
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) {
|
2016-12-27 15:32:45 +00:00
|
|
|
//w.net.Label = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
|
|
|
|
w.net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
|
|
|
|
//w.net2.Lines[0].Data = []int{0, 2, 5, 10, 20, 20, 2, 2, 0, 0}
|
2016-12-26 17:57:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 22:39:16 +00:00
|
|
|
func (w *Widgets) SetMem(val int64, limit int64) {
|
2016-12-27 15:32:45 +00:00
|
|
|
percent := round((float64(val) / float64(limit)) * 100)
|
2016-12-25 22:39:16 +00:00
|
|
|
w.memory.Label = fmt.Sprintf("%s / %s", byteFormat(val), byteFormat(limit))
|
2016-12-27 15:32:45 +00:00
|
|
|
if percent < 5 {
|
|
|
|
percent = 5
|
|
|
|
w.memory.BarColor = ui.ColorBlack
|
|
|
|
} else {
|
|
|
|
w.memory.BarColor = ui.ColorGreen
|
|
|
|
}
|
|
|
|
w.memory.Percent = percent
|
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-27 15:32:45 +00:00
|
|
|
|
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 15:32:45 +00:00
|
|
|
|
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
|
2016-12-27 15:32:45 +00:00
|
|
|
|
|
|
|
net := ui.NewPar("-")
|
|
|
|
net.Border = false
|
|
|
|
net.Height = 1
|
|
|
|
net.Width = 20
|
|
|
|
net.TextFgColor = ui.ColorWhite
|
|
|
|
|
|
|
|
return &Widgets{cid, net, name, 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
|
|
|
|
}
|