mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
remove Grid struct, move cursor into own package file
This commit is contained in:
116
grid.go
116
grid.go
@@ -4,94 +4,21 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bcicen/ctop/config"
|
||||
"github.com/bcicen/ctop/cwidgets/compact"
|
||||
"github.com/bcicen/ctop/widgets"
|
||||
ui "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
var (
|
||||
cGrid = compact.NewCompactGrid()
|
||||
header = widgets.NewCTopHeader()
|
||||
)
|
||||
|
||||
func maxRows() int {
|
||||
return ui.TermHeight() - 2 - cGrid.Y
|
||||
}
|
||||
|
||||
type Grid struct {
|
||||
cursorID string // id of currently selected container
|
||||
cSource ContainerSource
|
||||
containers Containers // sorted slice of containers
|
||||
}
|
||||
|
||||
func NewGrid() *Grid {
|
||||
g := &Grid{
|
||||
//cSource: NewDockerContainerSource(),
|
||||
cSource: NewMockContainerSource(),
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
// 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.Name.Highlight()
|
||||
}
|
||||
}
|
||||
|
||||
// Return current cursor index
|
||||
func (g *Grid) cursorIdx() int {
|
||||
for n, c := range g.containers {
|
||||
if c.Id == g.cursorID {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (g *Grid) cursorUp() {
|
||||
idx := g.cursorIdx()
|
||||
// decrement if possible
|
||||
if idx <= 0 {
|
||||
return
|
||||
}
|
||||
active := g.containers[idx]
|
||||
next := g.containers[idx-1]
|
||||
|
||||
active.Widgets.Name.UnHighlight()
|
||||
g.cursorID = next.Id
|
||||
next.Widgets.Name.Highlight()
|
||||
|
||||
ui.Render(cGrid)
|
||||
}
|
||||
|
||||
func (g *Grid) cursorDown() {
|
||||
idx := g.cursorIdx()
|
||||
// increment if possible
|
||||
if idx >= (len(g.containers) - 1) {
|
||||
return
|
||||
}
|
||||
if idx >= maxRows()-1 {
|
||||
return
|
||||
}
|
||||
active := g.containers[idx]
|
||||
next := g.containers[idx+1]
|
||||
|
||||
active.Widgets.Name.UnHighlight()
|
||||
g.cursorID = next.Id
|
||||
next.Widgets.Name.Highlight()
|
||||
ui.Render(cGrid)
|
||||
}
|
||||
|
||||
func (g *Grid) redrawRows() {
|
||||
func RedrawRows() {
|
||||
// reinit body rows
|
||||
cGrid.Clear()
|
||||
|
||||
// build layout
|
||||
y := 1
|
||||
if config.GetSwitchVal("enableHeader") {
|
||||
header.SetCount(len(g.containers))
|
||||
header.SetCount(cursor.Len())
|
||||
header.SetFilter(config.GetVal("filterStr"))
|
||||
y += header.Height()
|
||||
}
|
||||
@@ -99,18 +26,18 @@ func (g *Grid) redrawRows() {
|
||||
|
||||
var cursorVisible bool
|
||||
max := maxRows()
|
||||
for n, c := range g.containers {
|
||||
for n, c := range cursor.containers {
|
||||
if n >= max {
|
||||
break
|
||||
}
|
||||
cGrid.AddRows(c.Widgets)
|
||||
if c.Id == g.cursorID {
|
||||
if c.Id == cursor.selectedID {
|
||||
cursorVisible = true
|
||||
}
|
||||
}
|
||||
|
||||
if !cursorVisible {
|
||||
g.cursorReset()
|
||||
cursor.Reset()
|
||||
}
|
||||
|
||||
ui.Clear()
|
||||
@@ -121,13 +48,8 @@ func (g *Grid) redrawRows() {
|
||||
ui.Render(cGrid)
|
||||
}
|
||||
|
||||
func (g *Grid) refreshContainers() {
|
||||
g.containers = g.cSource.All().Filter()
|
||||
}
|
||||
|
||||
// Log current container and widget state
|
||||
func (g *Grid) dumpContainer() {
|
||||
c, _ := g.cSource.Get(g.cursorID)
|
||||
func dumpContainer(c *Container) {
|
||||
msg := fmt.Sprintf("logging state for container: %s\n", c.Id)
|
||||
msg += fmt.Sprintf("Id = %s\nname = %s\nstate = %s\n", c.Id, c.Name, c.State)
|
||||
msg += inspect(&c.Metrics)
|
||||
@@ -157,7 +79,7 @@ func (g *Grid) dumpContainer() {
|
||||
//container.widgets.Reset()
|
||||
//}
|
||||
|
||||
func Display(g *Grid) bool {
|
||||
func Display() bool {
|
||||
var menu func()
|
||||
|
||||
cGrid.SetWidth(ui.TermWidth())
|
||||
@@ -165,28 +87,28 @@ func Display(g *Grid) bool {
|
||||
|
||||
// initial draw
|
||||
header.Align()
|
||||
g.refreshContainers()
|
||||
g.redrawRows()
|
||||
cursor.RefreshContainers()
|
||||
RedrawRows()
|
||||
|
||||
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
|
||||
g.cursorUp()
|
||||
cursor.Up()
|
||||
})
|
||||
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
|
||||
g.cursorDown()
|
||||
cursor.Down()
|
||||
})
|
||||
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
|
||||
//c := g.containers[g.cursorIdx()]
|
||||
//c.Widgets.ToggleExpand()
|
||||
g.redrawRows()
|
||||
RedrawRows()
|
||||
})
|
||||
|
||||
ui.Handle("/sys/kbd/a", func(ui.Event) {
|
||||
config.Toggle("allContainers")
|
||||
g.refreshContainers()
|
||||
g.redrawRows()
|
||||
cursor.RefreshContainers()
|
||||
RedrawRows()
|
||||
})
|
||||
ui.Handle("/sys/kbd/D", func(ui.Event) {
|
||||
g.dumpContainer()
|
||||
dumpContainer(cursor.Selected())
|
||||
})
|
||||
ui.Handle("/sys/kbd/f", func(ui.Event) {
|
||||
menu = FilterMenu
|
||||
@@ -198,7 +120,7 @@ func Display(g *Grid) bool {
|
||||
})
|
||||
ui.Handle("/sys/kbd/H", func(ui.Event) {
|
||||
config.Toggle("enableHeader")
|
||||
g.redrawRows()
|
||||
RedrawRows()
|
||||
})
|
||||
ui.Handle("/sys/kbd/q", func(ui.Event) {
|
||||
ui.StopLoop()
|
||||
@@ -212,15 +134,15 @@ func Display(g *Grid) bool {
|
||||
})
|
||||
|
||||
ui.Handle("/timer/1s", func(e ui.Event) {
|
||||
g.refreshContainers()
|
||||
g.redrawRows()
|
||||
cursor.RefreshContainers()
|
||||
RedrawRows()
|
||||
})
|
||||
|
||||
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
|
||||
header.Align()
|
||||
cGrid.SetWidth(ui.TermWidth())
|
||||
log.Infof("resize: width=%v max-rows=%v", cGrid.Width, maxRows())
|
||||
g.redrawRows()
|
||||
RedrawRows()
|
||||
})
|
||||
|
||||
ui.Loop()
|
||||
|
||||
Reference in New Issue
Block a user