ctop/grid.go

116 lines
2.1 KiB
Go
Raw Normal View History

2016-12-22 17:15:22 +01:00
package main
import (
2016-12-27 03:24:02 +01:00
"fmt"
2016-12-22 17:15:22 +01:00
ui "github.com/gizak/termui"
)
type Grid struct {
2016-12-26 19:39:15 +01:00
cursorPos uint
2016-12-30 22:14:07 +01:00
containers *ContainerMap
2016-12-22 17:15:22 +01:00
}
2016-12-30 22:14:07 +01:00
func NewGrid() *Grid {
return &Grid{
cursorPos: 0,
containers: NewContainerMap(),
}
2016-12-26 19:39:15 +01:00
}
2016-12-28 04:01:44 +01:00
// Return sorted list of container IDs
2016-12-22 17:15:22 +01:00
func (g *Grid) CIDs() []string {
var ids []string
2016-12-30 22:14:07 +01:00
for _, c := range g.containers.Sorted() {
2016-12-30 04:22:25 +01:00
ids = append(ids, c.id)
2016-12-22 17:15:22 +01:00
}
return ids
}
2016-12-26 19:39:15 +01:00
// Redraw the cursor with the currently selected row
func (g *Grid) Cursor() {
2016-12-30 22:14:07 +01:00
for n, c := range g.containers.Sorted() {
2016-12-26 19:39:15 +01:00
if uint(n) == g.cursorPos {
2016-12-27 16:37:08 +01:00
c.widgets.name.TextFgColor = ui.ColorDefault
c.widgets.name.TextBgColor = ui.ColorWhite
2016-12-26 19:39:15 +01:00
} else {
2016-12-27 16:37:08 +01:00
c.widgets.name.TextFgColor = ui.ColorWhite
c.widgets.name.TextBgColor = ui.ColorDefault
2016-12-26 19:39:15 +01:00
}
}
ui.Render(ui.Body)
}
2016-12-22 17:15:22 +01:00
func (g *Grid) Rows() (rows []*ui.Row) {
2016-12-30 22:14:07 +01:00
for _, c := range g.containers.Sorted() {
2016-12-27 03:24:02 +01:00
rows = append(rows, c.widgets.MakeRow())
2016-12-22 17:15:22 +01:00
}
return rows
}
func header() *ui.Row {
return ui.NewRow(
2016-12-28 04:01:44 +01:00
ui.NewCol(2, 0, headerPar("NAME")),
2016-12-27 03:24:02 +01:00
ui.NewCol(1, 0, headerPar("CID")),
ui.NewCol(2, 0, headerPar("CPU")),
ui.NewCol(2, 0, headerPar("MEM")),
ui.NewCol(2, 0, headerPar("NET RX/TX")),
2016-12-22 17:15:22 +01:00
)
}
2016-12-27 03:24:02 +01:00
func headerPar(s string) *ui.Par {
p := ui.NewPar(fmt.Sprintf(" %s", s))
p.Border = false
p.Height = 2
p.Width = 20
p.TextFgColor = ui.ColorWhite
return p
}
func Display(g *Grid) {
2016-12-22 17:15:22 +01:00
if err := ui.Init(); err != nil {
panic(err)
}
defer ui.Close()
// build layout
ui.Body.AddRows(header())
2016-12-22 17:15:22 +01:00
for _, row := range g.Rows() {
ui.Body.AddRows(row)
}
// calculate layout
ui.Body.Align()
2016-12-26 19:39:15 +01:00
g.Cursor()
2016-12-22 17:15:22 +01:00
ui.Render(ui.Body)
2016-12-26 19:39:15 +01:00
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
if g.cursorPos > 0 {
g.cursorPos -= 1
g.Cursor()
}
})
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
2016-12-30 22:14:07 +01:00
if g.cursorPos < (g.containers.Len() - 1) {
2016-12-26 19:39:15 +01:00
g.cursorPos += 1
g.Cursor()
}
})
2016-12-22 17:15:22 +01:00
ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("/timer/1s", func(e ui.Event) {
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()
}