refactor multiview cursor

This commit is contained in:
Bradley Cicenas 2017-01-02 16:04:22 +00:00
parent e09bcd0272
commit 68d2207cb1
2 changed files with 42 additions and 26 deletions

66
grid.go
View File

@ -8,38 +8,61 @@ import (
) )
type Grid struct { type Grid struct {
cursorPos uint cursorID string // id of currently selected container
containers *ContainerMap containers []*Container
containerMap *ContainerMap
} }
func NewGrid() *Grid { func NewGrid() *Grid {
containerMap := NewContainerMap()
containers := containerMap.Sorted()
return &Grid{ return &Grid{
cursorPos: 0, cursorID: containers[0].id,
containers: NewContainerMap(), containers: containers,
containerMap: containerMap,
} }
} }
// Return sorted list of container IDs // Return current cursor index
func (g *Grid) CIDs() []string { func (g *Grid) CursorIdx() int {
var ids []string for n, c := range g.containers {
for _, c := range g.containers.Sorted() { if c.id == g.cursorID {
ids = append(ids, c.id) return n
}
}
return 0
}
func (g *Grid) CursorUp() {
idx := g.CursorIdx()
// decrement if possible
if idx > 0 {
g.cursorID = g.containers[idx-1].id
g.RedrawCursor()
}
}
func (g *Grid) CursorDown() {
idx := g.CursorIdx()
// increment if possible
if idx < (len(g.containers) - 1) {
g.cursorID = g.containers[idx+1].id
g.RedrawCursor()
} }
return ids
} }
// Redraw the cursor with the currently selected row // Redraw the cursor with the currently selected row
func (g *Grid) Cursor() { func (g *Grid) RedrawCursor() {
for n, c := range g.containers.Sorted() { for _, c := range g.containers {
if uint(n) == g.cursorPos { if c.id == g.cursorID {
c.widgets.name.TextFgColor = ui.ColorDefault c.widgets.name.TextFgColor = ui.ColorDefault
c.widgets.name.TextBgColor = ui.ColorWhite c.widgets.name.TextBgColor = ui.ColorWhite
} else { } else {
c.widgets.name.TextFgColor = ui.ColorWhite c.widgets.name.TextFgColor = ui.ColorWhite
c.widgets.name.TextBgColor = ui.ColorDefault c.widgets.name.TextBgColor = ui.ColorDefault
} }
}
ui.Render(ui.Body) ui.Render(ui.Body)
}
} }
func (g *Grid) Redraw() { func (g *Grid) Redraw() {
@ -48,7 +71,7 @@ func (g *Grid) Redraw() {
// build layout // build layout
ui.Body.AddRows(header()) ui.Body.AddRows(header())
for _, c := range g.containers.Sorted() { for _, c := range g.containers {
ui.Body.AddRows(c.widgets.MakeRow()) ui.Body.AddRows(c.widgets.MakeRow())
} }
@ -93,20 +116,14 @@ func Display(g *Grid) bool {
// calculate layout // calculate layout
ui.Body.Align() ui.Body.Align()
g.Cursor() g.RedrawCursor()
ui.Render(ui.Body) ui.Render(ui.Body)
ui.Handle("/sys/kbd/<up>", func(ui.Event) { ui.Handle("/sys/kbd/<up>", func(ui.Event) {
if g.cursorPos > 0 { g.CursorUp()
g.cursorPos -= 1
g.Cursor()
}
}) })
ui.Handle("/sys/kbd/<down>", func(ui.Event) { ui.Handle("/sys/kbd/<down>", func(ui.Event) {
if g.cursorPos < (g.containers.Len() - 1) { g.CursorDown()
g.cursorPos += 1
g.Cursor()
}
}) })
ui.Handle("/sys/kbd/h", func(ui.Event) { ui.Handle("/sys/kbd/h", func(ui.Event) {
newView = views.Help newView = views.Help
@ -116,6 +133,7 @@ func Display(g *Grid) bool {
ui.StopLoop() ui.StopLoop()
}) })
ui.Handle("/timer/1s", func(e ui.Event) { ui.Handle("/timer/1s", func(e ui.Event) {
g.containers = g.containerMap.Sorted() // refresh containers for current sort order
g.Redraw() g.Redraw()
}) })

View File

@ -36,9 +36,7 @@ func (w *Widgets) SetCPU(val int) {
} }
func (w *Widgets) SetNet(rx int64, tx int64) { func (w *Widgets) SetNet(rx int64, tx int64) {
//w.net.Label = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
w.net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx)) w.net.Text = fmt.Sprintf("%s / %s", byteFormat(rx), byteFormat(tx))
//w.net2.Lines[0].Data = []int{0, 2, 5, 10, 20, 20, 2, 2, 0, 0}
} }
func (w *Widgets) SetMem(val int64, limit int64) { func (w *Widgets) SetMem(val int64, limit int64) {