From 68d2207cb1683e77a62d6dad39c559719df396b6 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Mon, 2 Jan 2017 16:04:22 +0000 Subject: [PATCH] refactor multiview cursor --- grid.go | 66 ++++++++++++++++++++++++++++++++++-------------------- widgets.go | 2 -- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/grid.go b/grid.go index c14d77b..3cac7d5 100644 --- a/grid.go +++ b/grid.go @@ -8,38 +8,61 @@ import ( ) type Grid struct { - cursorPos uint - containers *ContainerMap + cursorID string // id of currently selected container + containers []*Container + containerMap *ContainerMap } func NewGrid() *Grid { + containerMap := NewContainerMap() + containers := containerMap.Sorted() return &Grid{ - cursorPos: 0, - containers: NewContainerMap(), + cursorID: containers[0].id, + containers: containers, + containerMap: containerMap, } } -// Return sorted list of container IDs -func (g *Grid) CIDs() []string { - var ids []string - for _, c := range g.containers.Sorted() { - ids = append(ids, c.id) +// 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 { + 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 -func (g *Grid) Cursor() { - for n, c := range g.containers.Sorted() { - if uint(n) == g.cursorPos { +func (g *Grid) RedrawCursor() { + for _, c := range g.containers { + if c.id == g.cursorID { c.widgets.name.TextFgColor = ui.ColorDefault c.widgets.name.TextBgColor = ui.ColorWhite } else { c.widgets.name.TextFgColor = ui.ColorWhite c.widgets.name.TextBgColor = ui.ColorDefault } + ui.Render(ui.Body) } - ui.Render(ui.Body) } func (g *Grid) Redraw() { @@ -48,7 +71,7 @@ func (g *Grid) Redraw() { // build layout ui.Body.AddRows(header()) - for _, c := range g.containers.Sorted() { + for _, c := range g.containers { ui.Body.AddRows(c.widgets.MakeRow()) } @@ -93,20 +116,14 @@ func Display(g *Grid) bool { // calculate layout ui.Body.Align() - g.Cursor() + g.RedrawCursor() ui.Render(ui.Body) ui.Handle("/sys/kbd/", func(ui.Event) { - if g.cursorPos > 0 { - g.cursorPos -= 1 - g.Cursor() - } + g.CursorUp() }) ui.Handle("/sys/kbd/", func(ui.Event) { - if g.cursorPos < (g.containers.Len() - 1) { - g.cursorPos += 1 - g.Cursor() - } + g.CursorDown() }) ui.Handle("/sys/kbd/h", func(ui.Event) { newView = views.Help @@ -116,6 +133,7 @@ func Display(g *Grid) bool { ui.StopLoop() }) ui.Handle("/timer/1s", func(e ui.Event) { + g.containers = g.containerMap.Sorted() // refresh containers for current sort order g.Redraw() }) diff --git a/widgets.go b/widgets.go index 43ec344..7cdfa5a 100644 --- a/widgets.go +++ b/widgets.go @@ -36,9 +36,7 @@ func (w *Widgets) SetCPU(val int) { } 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.net2.Lines[0].Data = []int{0, 2, 5, 10, 20, 20, 2, 2, 0, 0} } func (w *Widgets) SetMem(val int64, limit int64) {