2016-12-22 16:15:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Grid struct {
|
|
|
|
containers map[string]*Container
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Grid) AddContainer(id string) {
|
2016-12-22 22:04:21 +00:00
|
|
|
g.containers[id] = NewContainer(id)
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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(
|
2016-12-22 22:04:21 +00:00
|
|
|
ui.NewCol(1, 0, c.widgets.cid),
|
|
|
|
ui.NewCol(2, 0, c.widgets.cpu),
|
|
|
|
ui.NewCol(2, 0, c.widgets.memory),
|
2016-12-22 16:15:22 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-12-22 22:04:21 +00:00
|
|
|
func Display(g *Grid) {
|
2016-12-22 16:15:22 +00:00
|
|
|
if err := ui.Init(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer ui.Close()
|
|
|
|
|
|
|
|
// build layout
|
2016-12-22 22:04:21 +00:00
|
|
|
ui.Body.AddRows(header())
|
2016-12-22 16:15:22 +00:00
|
|
|
|
|
|
|
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) {
|
2016-12-22 22:04:21 +00:00
|
|
|
// t := e.Data.(ui.EvtTimer)
|
|
|
|
// i := t.Count
|
|
|
|
// if i > 103 {
|
|
|
|
// ui.StopLoop()
|
|
|
|
// return
|
|
|
|
// }
|
2016-12-22 16:15:22 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|