2016-12-22 16:15:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-12-27 02:24:02 +00:00
|
|
|
"fmt"
|
2016-12-22 16:15:22 +00:00
|
|
|
|
2017-02-07 03:33:09 +00:00
|
|
|
"github.com/bcicen/ctop/config"
|
2017-01-06 13:49:22 +00:00
|
|
|
"github.com/bcicen/ctop/widgets"
|
2016-12-22 16:15:22 +00:00
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
2017-02-26 07:31:23 +00:00
|
|
|
var cGrid = widgets.NewCompactGrid()
|
2017-02-26 06:22:50 +00:00
|
|
|
|
2017-02-25 07:16:00 +00:00
|
|
|
func maxRows() int {
|
2017-02-26 06:22:50 +00:00
|
|
|
return ui.TermHeight() - 2 - cGrid.Y
|
2017-02-25 07:16:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:15:22 +00:00
|
|
|
type Grid struct {
|
2017-02-26 07:31:23 +00:00
|
|
|
cursorID string // id of currently selected container
|
2017-02-26 21:12:28 +00:00
|
|
|
cSource ContainerSource
|
2017-02-26 07:31:23 +00:00
|
|
|
containers Containers // sorted slice of containers
|
|
|
|
header *widgets.CTopHeader
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 21:14:07 +00:00
|
|
|
func NewGrid() *Grid {
|
2017-02-26 21:12:28 +00:00
|
|
|
cs := NewDockerContainerSource()
|
2017-01-25 19:57:22 +00:00
|
|
|
g := &Grid{
|
2017-02-26 21:12:28 +00:00
|
|
|
cSource: cs,
|
|
|
|
containers: cs.All(),
|
2017-02-26 07:31:23 +00:00
|
|
|
header: widgets.NewCTopHeader(),
|
2016-12-30 21:14:07 +00:00
|
|
|
}
|
2017-01-25 19:57:22 +00:00
|
|
|
return g
|
2016-12-26 18:39:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 09:10:14 +00:00
|
|
|
// Set an initial cursor position, if possible
|
|
|
|
func (g *Grid) cursorReset() {
|
|
|
|
if len(g.containers) > 0 {
|
|
|
|
g.cursorID = g.containers[0].id
|
|
|
|
g.containers[0].widgets.Highlight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-02 16:04:22 +00:00
|
|
|
// Return current cursor index
|
2017-01-02 20:14:35 +00:00
|
|
|
func (g *Grid) cursorIdx() int {
|
2017-01-02 16:04:22 +00:00
|
|
|
for n, c := range g.containers {
|
|
|
|
if c.id == g.cursorID {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-01-02 20:14:35 +00:00
|
|
|
func (g *Grid) cursorUp() {
|
|
|
|
idx := g.cursorIdx()
|
2017-01-02 16:04:22 +00:00
|
|
|
// decrement if possible
|
2017-02-19 23:23:59 +00:00
|
|
|
if idx <= 0 {
|
|
|
|
return
|
2017-01-02 16:04:22 +00:00
|
|
|
}
|
2017-02-19 23:23:59 +00:00
|
|
|
active := g.containers[idx]
|
|
|
|
next := g.containers[idx-1]
|
|
|
|
|
|
|
|
active.widgets.UnHighlight()
|
|
|
|
g.cursorID = next.id
|
|
|
|
next.widgets.Highlight()
|
|
|
|
|
2017-02-26 06:22:50 +00:00
|
|
|
ui.Render(cGrid)
|
2017-01-02 16:04:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 20:14:35 +00:00
|
|
|
func (g *Grid) cursorDown() {
|
|
|
|
idx := g.cursorIdx()
|
2017-01-02 16:04:22 +00:00
|
|
|
// increment if possible
|
2017-02-22 05:20:37 +00:00
|
|
|
if idx >= (len(g.containers) - 1) {
|
2017-02-19 23:23:59 +00:00
|
|
|
return
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|
2017-02-25 07:16:00 +00:00
|
|
|
if idx >= maxRows()-1 {
|
2017-02-19 23:23:59 +00:00
|
|
|
return
|
2016-12-26 18:39:15 +00:00
|
|
|
}
|
2017-02-19 23:23:59 +00:00
|
|
|
active := g.containers[idx]
|
|
|
|
next := g.containers[idx+1]
|
|
|
|
|
|
|
|
active.widgets.UnHighlight()
|
|
|
|
g.cursorID = next.id
|
|
|
|
next.widgets.Highlight()
|
2017-02-26 06:22:50 +00:00
|
|
|
ui.Render(cGrid)
|
2016-12-26 18:39:15 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 20:14:35 +00:00
|
|
|
func (g *Grid) redrawRows() {
|
2016-12-30 22:10:49 +00:00
|
|
|
// reinit body rows
|
2017-02-26 06:22:50 +00:00
|
|
|
cGrid.Rows = []widgets.ContainerWidgets{}
|
2017-01-02 20:14:35 +00:00
|
|
|
|
2016-12-30 22:10:49 +00:00
|
|
|
// build layout
|
2017-02-26 06:22:50 +00:00
|
|
|
y := 1
|
2017-02-16 04:06:05 +00:00
|
|
|
if config.GetSwitchVal("enableHeader") {
|
2017-01-06 13:49:22 +00:00
|
|
|
g.header.SetCount(len(g.containers))
|
2017-02-16 04:06:05 +00:00
|
|
|
g.header.SetFilter(config.GetVal("filterStr"))
|
2017-02-18 03:31:50 +00:00
|
|
|
g.header.Render()
|
2017-02-26 06:22:50 +00:00
|
|
|
y += g.header.Height()
|
2017-01-06 13:49:22 +00:00
|
|
|
}
|
2017-02-26 07:31:23 +00:00
|
|
|
cGrid.SetY(y)
|
2017-02-24 09:10:14 +00:00
|
|
|
|
|
|
|
var cursorVisible bool
|
2017-02-25 07:16:00 +00:00
|
|
|
max := maxRows()
|
2017-02-19 23:23:59 +00:00
|
|
|
for n, c := range g.containers.Filter() {
|
2017-02-25 07:16:00 +00:00
|
|
|
if n >= max {
|
2017-02-19 23:23:59 +00:00
|
|
|
break
|
|
|
|
}
|
2017-02-26 06:22:50 +00:00
|
|
|
cGrid.Rows = append(cGrid.Rows, c.widgets)
|
2017-02-24 09:10:14 +00:00
|
|
|
if c.id == g.cursorID {
|
|
|
|
cursorVisible = true
|
|
|
|
}
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|
2016-12-30 22:10:49 +00:00
|
|
|
|
2017-02-24 09:10:14 +00:00
|
|
|
if !cursorVisible {
|
|
|
|
g.cursorReset()
|
|
|
|
}
|
|
|
|
|
2017-02-26 07:31:23 +00:00
|
|
|
ui.Clear()
|
|
|
|
if config.GetSwitchVal("enableHeader") {
|
|
|
|
g.header.Render()
|
|
|
|
}
|
|
|
|
cGrid.Align()
|
2017-02-26 06:22:50 +00:00
|
|
|
ui.Render(cGrid)
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 19:46:30 +00:00
|
|
|
func (g *Grid) ExpandView() {
|
2017-02-18 03:31:50 +00:00
|
|
|
ui.Clear()
|
|
|
|
ui.DefaultEvtStream.ResetHandlers()
|
|
|
|
defer ui.DefaultEvtStream.ResetHandlers()
|
2017-02-26 21:12:28 +00:00
|
|
|
container, _ := g.cSource.Get(g.cursorID)
|
2017-01-06 19:46:30 +00:00
|
|
|
container.Expand()
|
|
|
|
}
|
|
|
|
|
2017-02-05 00:56:45 +00: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)
|
|
|
|
}
|
|
|
|
|
2017-01-01 22:42:13 +00:00
|
|
|
func Display(g *Grid) bool {
|
2017-01-08 23:07:58 +00:00
|
|
|
var menu func()
|
2017-01-06 19:46:30 +00:00
|
|
|
var expand bool
|
2017-01-01 22:42:13 +00:00
|
|
|
|
2017-02-26 06:22:50 +00:00
|
|
|
cGrid.SetWidth(ui.TermWidth())
|
2017-02-05 00:56:45 +00:00
|
|
|
ui.DefaultEvtStream.Hook(logEvent)
|
|
|
|
|
2017-02-18 03:31:50 +00:00
|
|
|
// initial draw
|
2017-02-02 07:09:43 +00:00
|
|
|
g.redrawRows()
|
2016-12-22 16:15:22 +00:00
|
|
|
|
2016-12-26 18:39:15 +00:00
|
|
|
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
|
2017-01-02 20:14:35 +00:00
|
|
|
g.cursorUp()
|
2016-12-26 18:39:15 +00:00
|
|
|
})
|
|
|
|
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
|
2017-01-02 20:14:35 +00:00
|
|
|
g.cursorDown()
|
2016-12-26 18:39:15 +00:00
|
|
|
})
|
2017-01-06 19:46:30 +00:00
|
|
|
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
|
|
|
expand = true
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2017-02-23 22:00:05 +00:00
|
|
|
|
2017-02-16 00:00:31 +00:00
|
|
|
ui.Handle("/sys/kbd/a", func(ui.Event) {
|
|
|
|
config.Toggle("allContainers")
|
|
|
|
g.redrawRows()
|
|
|
|
})
|
2017-01-21 18:15:29 +00:00
|
|
|
ui.Handle("/sys/kbd/f", func(ui.Event) {
|
|
|
|
menu = FilterMenu
|
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2017-01-01 22:42:13 +00:00
|
|
|
ui.Handle("/sys/kbd/h", func(ui.Event) {
|
2017-01-08 23:07:58 +00:00
|
|
|
menu = HelpMenu
|
2017-01-02 23:40:55 +00:00
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2017-02-13 03:37:17 +00:00
|
|
|
ui.Handle("/sys/kbd/H", func(ui.Event) {
|
|
|
|
config.Toggle("enableHeader")
|
|
|
|
g.redrawRows()
|
|
|
|
})
|
2017-01-12 19:48:29 +00:00
|
|
|
ui.Handle("/sys/kbd/q", func(ui.Event) {
|
2017-01-01 22:42:13 +00:00
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2017-01-21 18:15:29 +00:00
|
|
|
ui.Handle("/sys/kbd/r", func(e ui.Event) {
|
2017-02-07 03:33:09 +00:00
|
|
|
config.Toggle("sortReversed")
|
2017-01-12 19:48:29 +00:00
|
|
|
})
|
|
|
|
ui.Handle("/sys/kbd/s", func(ui.Event) {
|
|
|
|
menu = SortMenu
|
2016-12-22 16:15:22 +00:00
|
|
|
ui.StopLoop()
|
|
|
|
})
|
2017-02-23 22:00:05 +00:00
|
|
|
|
2016-12-22 16:15:22 +00:00
|
|
|
ui.Handle("/timer/1s", func(e ui.Event) {
|
2017-02-26 21:12:28 +00:00
|
|
|
g.containers = g.cSource.All() // refresh containers for current sort order
|
2017-01-02 20:14:35 +00:00
|
|
|
g.redrawRows()
|
2016-12-22 16:15:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
|
2017-02-19 23:52:59 +00:00
|
|
|
g.header.Align()
|
2017-02-26 06:22:50 +00:00
|
|
|
cGrid.SetWidth(ui.TermWidth())
|
|
|
|
log.Infof("resize: width=%v max-rows=%v", cGrid.Width, maxRows())
|
2017-02-18 00:59:13 +00:00
|
|
|
g.redrawRows()
|
2016-12-22 16:15:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ui.Loop()
|
2017-01-08 23:07:58 +00:00
|
|
|
if menu != nil {
|
2017-02-13 03:22:32 +00:00
|
|
|
menu()
|
2017-01-01 22:42:13 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-01-06 19:46:30 +00:00
|
|
|
if expand {
|
|
|
|
g.ExpandView()
|
|
|
|
return false
|
|
|
|
}
|
2017-01-01 22:42:13 +00:00
|
|
|
return true
|
2016-12-22 16:15:22 +00:00
|
|
|
}
|