ctop/grid.go

201 lines
3.8 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
2017-02-07 04:33:09 +01:00
"github.com/bcicen/ctop/config"
2017-01-06 14:49:22 +01:00
"github.com/bcicen/ctop/widgets"
2016-12-22 17:15:22 +01:00
ui "github.com/gizak/termui"
)
type Grid struct {
cursorID string // id of currently selected container
cmap *ContainerMap
containers []*Container // sorted slice of containers
header *widgets.CTopHeader
2016-12-22 17:15:22 +01:00
}
2016-12-30 22:14:07 +01:00
func NewGrid() *Grid {
cmap := NewContainerMap()
2017-01-25 20:57:22 +01:00
g := &Grid{
cmap: cmap,
2017-01-25 20:57:22 +01:00
containers: cmap.All(),
header: widgets.NewCTopHeader(),
2016-12-30 22:14:07 +01:00
}
2017-01-25 20:57:22 +01:00
// set initial cursor position
if len(g.containers) > 0 {
g.cursorID = g.containers[0].id
}
return g
2016-12-26 19:39:15 +01:00
}
2017-01-02 17:04:22 +01:00
// Return current cursor index
2017-01-02 21:14:35 +01:00
func (g *Grid) cursorIdx() int {
2017-01-02 17:04:22 +01:00
for n, c := range g.containers {
if c.id == g.cursorID {
return n
}
}
return 0
}
2017-01-02 21:14:35 +01:00
func (g *Grid) cursorUp() {
idx := g.cursorIdx()
2017-01-02 17:04:22 +01:00
// decrement if possible
if idx > 0 {
g.cursorID = g.containers[idx-1].id
2017-01-02 21:14:35 +01:00
g.redrawCursor()
2017-01-02 17:04:22 +01:00
}
}
2017-01-02 21:14:35 +01:00
func (g *Grid) cursorDown() {
idx := g.cursorIdx()
2017-01-02 17:04:22 +01:00
// increment if possible
if idx < (len(g.containers) - 1) {
g.cursorID = g.containers[idx+1].id
2017-01-02 21:14:35 +01:00
g.redrawCursor()
2016-12-22 17:15:22 +01:00
}
}
2016-12-26 19:39:15 +01:00
// Redraw the cursor with the currently selected row
2017-01-02 21:14:35 +01:00
func (g *Grid) redrawCursor() {
2017-01-02 17:04:22 +01:00
for _, c := range g.containers {
if c.id == g.cursorID {
c.widgets.Highlight()
2016-12-26 19:39:15 +01:00
} else {
c.widgets.UnHighlight()
2016-12-26 19:39:15 +01:00
}
2017-01-02 17:04:22 +01:00
ui.Render(ui.Body)
2016-12-26 19:39:15 +01:00
}
}
2017-01-02 21:14:35 +01:00
func (g *Grid) redrawRows() {
2016-12-30 23:10:49 +01:00
// reinit body rows
ui.Body.Rows = []*ui.Row{}
2017-01-23 16:00:33 +01:00
ui.Clear()
2017-01-02 21:14:35 +01:00
2016-12-30 23:10:49 +01:00
// build layout
if config.GetToggle("enableHeader") {
2017-01-06 14:49:22 +01:00
g.header.SetCount(len(g.containers))
ui.Body.AddRows(g.header.Row())
}
ui.Body.AddRows(fieldHeader())
2017-01-02 17:04:22 +01:00
for _, c := range g.containers {
ui.Body.AddRows(c.widgets.Row())
2016-12-22 17:15:22 +01:00
}
2016-12-30 23:10:49 +01:00
ui.Body.Align()
ui.Render(ui.Body)
2016-12-22 17:15:22 +01:00
}
2017-01-06 14:49:22 +01:00
func fieldHeader() *ui.Row {
2016-12-22 17:15:22 +01:00
return ui.NewRow(
2017-02-01 06:44:24 +01:00
ui.NewCol(1, 0, headerPar("STATUS")),
2016-12-28 04:01:44 +01:00
ui.NewCol(2, 0, headerPar("NAME")),
ui.NewCol(2, 0, headerPar("CID")),
2016-12-27 03:24:02 +01:00
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 ResetView() {
ui.DefaultEvtStream.ResetHandlers()
ui.Clear()
}
2017-01-06 20:46:30 +01:00
func (g *Grid) ExpandView() {
ResetView()
defer ResetView()
container := g.cmap.Get(g.cursorID)
2017-01-06 20:46:30 +01:00
container.Expand()
container.widgets.Render()
container.Collapse()
}
2017-02-05 01:56:45 +01:00
func logEvent(e ui.Event) {
var s string
s += fmt.Sprintf("Type: %s\n", e.Type)
s += fmt.Sprintf("Path: %s\n", e.Path)
s += fmt.Sprintf("From: %s\n", e.From)
s += fmt.Sprintf("To: %s", e.To)
log.Debugf("new event:\n%s", s)
}
func Display(g *Grid) bool {
2017-01-09 00:07:58 +01:00
var menu func()
2017-01-06 20:46:30 +01:00
var expand bool
var loopIter int
2017-02-05 01:56:45 +01:00
ui.DefaultEvtStream.Hook(logEvent)
2016-12-22 17:15:22 +01:00
// calculate layout
ui.Body.Align()
2017-01-02 21:14:35 +01:00
g.redrawCursor()
g.redrawRows()
2016-12-22 17:15:22 +01:00
2016-12-26 19:39:15 +01:00
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
2017-01-02 21:14:35 +01:00
g.cursorUp()
2016-12-26 19:39:15 +01:00
})
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
2017-01-02 21:14:35 +01:00
g.cursorDown()
2016-12-26 19:39:15 +01:00
})
2017-01-06 20:46:30 +01:00
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
expand = true
ui.StopLoop()
})
2017-01-21 19:15:29 +01:00
ui.Handle("/sys/kbd/f", func(ui.Event) {
menu = FilterMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/h", func(ui.Event) {
2017-01-09 00:07:58 +01:00
menu = HelpMenu
2017-01-03 00:40:55 +01:00
ui.StopLoop()
})
2017-01-12 20:48:29 +01:00
ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.StopLoop()
})
2017-01-21 19:15:29 +01:00
ui.Handle("/sys/kbd/r", func(e ui.Event) {
2017-02-07 04:33:09 +01:00
config.Toggle("sortReversed")
2017-01-12 20:48:29 +01:00
})
ui.Handle("/sys/kbd/s", func(ui.Event) {
menu = SortMenu
2016-12-22 17:15:22 +01:00
ui.StopLoop()
})
ui.Handle("/timer/1s", func(e ui.Event) {
loopIter++
if loopIter%5 == 0 {
g.cmap.Refresh()
}
g.containers = g.cmap.All() // refresh containers for current sort order
2017-01-02 21:14:35 +01:00
g.redrawRows()
2016-12-22 17:15:22 +01:00
})
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()
2017-01-09 00:07:58 +01:00
if menu != nil {
menu()
return false
}
2017-01-06 20:46:30 +01:00
if expand {
g.ExpandView()
return false
}
return true
2016-12-22 17:15:22 +01:00
}