2017-03-06 03:03:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GridCursor struct {
|
|
|
|
selectedID string // id of currently selected container
|
2017-03-08 00:46:39 +00:00
|
|
|
filtered Containers
|
2017-03-06 03:03:05 +00:00
|
|
|
containers Containers
|
|
|
|
cSource ContainerSource
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGridCursor() *GridCursor {
|
|
|
|
return &GridCursor{
|
|
|
|
cSource: NewDockerContainerSource(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 00:46:39 +00:00
|
|
|
func (gc *GridCursor) Len() int { return len(gc.filtered) }
|
2017-03-06 03:03:05 +00:00
|
|
|
func (gc *GridCursor) Selected() *Container { return gc.containers[gc.Idx()] }
|
|
|
|
|
2017-03-08 00:10:38 +00:00
|
|
|
// Refresh containers from source
|
|
|
|
func (gc *GridCursor) RefreshContainers() (lenChanged bool) {
|
|
|
|
oldLen := gc.Len()
|
2017-03-08 00:46:39 +00:00
|
|
|
gc.setContainers(gc.cSource.All())
|
2017-03-08 00:10:38 +00:00
|
|
|
if oldLen != gc.Len() {
|
|
|
|
lenChanged = true
|
|
|
|
}
|
2017-03-06 03:03:05 +00:00
|
|
|
if gc.selectedID == "" {
|
|
|
|
gc.Reset()
|
|
|
|
}
|
2017-03-08 00:10:38 +00:00
|
|
|
return lenChanged
|
2017-03-06 03:03:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 00:46:39 +00:00
|
|
|
func (gc *GridCursor) setContainers(c Containers) {
|
|
|
|
gc.containers = c
|
|
|
|
// Containers filtered by display bool
|
|
|
|
gc.filtered = Containers{}
|
|
|
|
for _, c := range gc.containers {
|
|
|
|
if c.display {
|
|
|
|
gc.filtered = append(gc.filtered, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 03:03:05 +00:00
|
|
|
// Set an initial cursor position, if possible
|
|
|
|
func (gc *GridCursor) Reset() {
|
2017-03-08 00:46:39 +00:00
|
|
|
for _, c := range gc.containers {
|
|
|
|
c.Widgets.Name.UnHighlight()
|
|
|
|
}
|
2017-03-06 03:03:05 +00:00
|
|
|
if gc.Len() > 0 {
|
|
|
|
gc.selectedID = gc.containers[0].Id
|
|
|
|
gc.containers[0].Widgets.Name.Highlight()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return current cursor index
|
|
|
|
func (gc *GridCursor) Idx() int {
|
|
|
|
for n, c := range gc.containers {
|
|
|
|
if c.Id == gc.selectedID {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *GridCursor) Up() {
|
|
|
|
idx := gc.Idx()
|
|
|
|
// decrement if possible
|
|
|
|
if idx <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
active := gc.containers[idx]
|
|
|
|
next := gc.containers[idx-1]
|
|
|
|
|
|
|
|
active.Widgets.Name.UnHighlight()
|
|
|
|
gc.selectedID = next.Id
|
|
|
|
next.Widgets.Name.Highlight()
|
|
|
|
|
|
|
|
ui.Render(cGrid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *GridCursor) Down() {
|
|
|
|
idx := gc.Idx()
|
|
|
|
// increment if possible
|
|
|
|
if idx >= (gc.Len() - 1) {
|
|
|
|
return
|
|
|
|
}
|
2017-03-08 00:52:31 +00:00
|
|
|
//if idx >= maxRows()-1 {
|
|
|
|
//return
|
|
|
|
//}
|
2017-03-06 03:03:05 +00:00
|
|
|
active := gc.containers[idx]
|
|
|
|
next := gc.containers[idx+1]
|
|
|
|
|
|
|
|
active.Widgets.Name.UnHighlight()
|
|
|
|
gc.selectedID = next.Id
|
|
|
|
next.Widgets.Name.Highlight()
|
|
|
|
ui.Render(cGrid)
|
|
|
|
}
|