ctop/grid.go
2016-12-22 10:15:22 -06:00

151 lines
2.6 KiB
Go

package main
import (
"sort"
ui "github.com/gizak/termui"
)
type Grid struct {
containers map[string]*Container
}
func (g *Grid) AddContainer(id string) {
cid := ui.NewPar(id)
cid.Border = false
cid.Height = 2
cid.Width = 10
cid.TextFgColor = ui.ColorWhite
g.containers[id] = &Container{cid, mkGauge(), mkGauge()}
}
// Return sorted list of active container IDs
func (g *Grid) CIDs() []string {
var ids []string
for id, _ := range g.containers {
ids = append(ids, id)
}
sort.Strings(ids)
return ids
}
func (g *Grid) Rows() (rows []*ui.Row) {
for _, cid := range g.CIDs() {
c := g.containers[cid]
rows = append(rows, ui.NewRow(
ui.NewCol(1, 0, c.cid),
ui.NewCol(2, 0, c.cpu),
ui.NewCol(2, 0, c.memory),
))
}
return rows
}
func mkGauge() *ui.Gauge {
g := ui.NewGauge()
g.Height = 1
g.Border = false
g.Percent = 5
g.PaddingBottom = 0
g.BarColor = ui.ColorGreen
return g
}
func header() *ui.Row {
//cid
c1 := ui.NewPar(" CID")
c1.Border = false
c1.Height = 2
c1.Width = 10
c1.TextFgColor = ui.ColorWhite
//cpu
c2 := ui.NewPar(" CPU")
c2.Border = false
c2.Height = 2
c2.Width = 10
c2.TextFgColor = ui.ColorWhite
//mem
c3 := ui.NewPar(" MEM")
c3.Border = false
c3.Height = 2
c3.Width = 10
c3.TextFgColor = ui.ColorWhite
//misc
c4 := ui.NewPar(" MISC")
c4.Border = false
c4.Height = 2
c4.Width = 10
c4.TextFgColor = ui.ColorWhite
return ui.NewRow(
ui.NewCol(1, 0, c1),
ui.NewCol(2, 0, c2),
ui.NewCol(2, 0, c3),
ui.NewCol(2, 0, c4),
)
}
func NewGrid() {
if err := ui.Init(); err != nil {
panic(err)
}
defer ui.Close()
g := &Grid{make(map[string]*Container)}
for _, id := range []string{" 12345", " 56789", " 00001"} {
g.AddContainer(id)
}
par := ui.NewPar("<> This row has 3 columns\n<- Widgets can be stacked up like left side\n<- Stacked widgets are treated as a single widget")
par.Height = 5
par.BorderLabel = "Demonstration"
// build layout
ui.Body.AddRows(
header(),
)
for _, row := range g.Rows() {
ui.Body.AddRows(row)
}
// calculate layout
ui.Body.Align()
ui.Render(ui.Body)
ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("/timer/1s", func(e ui.Event) {
t := e.Data.(ui.EvtTimer)
i := t.Count
if i > 103 {
ui.StopLoop()
return
}
for _, c := range g.containers {
c.UpdateCPU((c.cpu.Percent + 5) % 100)
c.UpdateMem((c.memory.Percent + 12) % 100)
}
ui.Render(ui.Body)
})
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
ui.Body.Width = ui.TermWidth()
ui.Body.Align()
ui.Clear()
ui.Render(ui.Body)
})
ui.Loop()
}
func main() {
NewGrid()
}