From 335ac8d741546003e6dc20c345553b8851845542 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Mon, 20 Feb 2017 10:23:59 +1100 Subject: [PATCH] smooth cursor, row rendering. add maxRows calc to grid --- grid.go | 55 ++++++++++++++++++++++++++++------------------ widgets/compact.go | 39 +++++++++++++++++++------------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/grid.go b/grid.go index 37e2d54..2d1b756 100644 --- a/grid.go +++ b/grid.go @@ -11,7 +11,8 @@ import ( type Grid struct { cursorID string // id of currently selected container cmap *ContainerMap - containers []*Container // sorted slice of containers + maxRows int + containers Containers // sorted slice of containers header *widgets.CTopHeader } @@ -25,10 +26,15 @@ func NewGrid() *Grid { // set initial cursor position if len(g.containers) > 0 { g.cursorID = g.containers[0].id + g.containers[0].widgets.Highlight() } return g } +func (g *Grid) calcMaxRows() { + g.maxRows = ui.TermHeight() - widgets.CompactHeader.Height - ui.Body.Y +} + // Return current cursor index func (g *Grid) cursorIdx() int { for n, c := range g.containers { @@ -42,35 +48,40 @@ func (g *Grid) cursorIdx() int { func (g *Grid) cursorUp() { idx := g.cursorIdx() // decrement if possible - if idx > 0 { - g.cursorID = g.containers[idx-1].id - g.redrawCursor() + if idx <= 0 { + return } + active := g.containers[idx] + next := g.containers[idx-1] + + active.widgets.UnHighlight() + g.cursorID = next.id + next.widgets.Highlight() + + ui.Render(ui.Body) } 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() + if idx > (len(g.containers) - 1) { + return } -} + if idx >= g.maxRows-1 { + return + } + active := g.containers[idx] + next := g.containers[idx+1] -// Redraw the cursor with the currently selected row -func (g *Grid) redrawCursor() { - for _, c := range g.containers { - if c.id == g.cursorID { - c.widgets.Highlight() - } else { - c.widgets.UnHighlight() - } - ui.Render(ui.Body) - } + active.widgets.UnHighlight() + g.cursorID = next.id + next.widgets.Highlight() + ui.Render(ui.Body) } func (g *Grid) redrawRows() { // reinit body rows + g.calcMaxRows() ui.Body.Rows = []*ui.Row{} ui.Clear() @@ -84,7 +95,10 @@ func (g *Grid) redrawRows() { ui.Body.Y = 0 } ui.Body.AddRows(widgets.CompactHeader) - for _, c := range g.containers { + for n, c := range g.containers.Filter() { + if n >= g.maxRows { + break + } ui.Body.AddRows(c.widgets.Row()) } @@ -144,7 +158,6 @@ func Display(g *Grid) bool { ui.DefaultEvtStream.Hook(logEvent) // initial draw - g.redrawCursor() g.redrawRows() ui.Handle("/sys/kbd/", func(ui.Event) { @@ -194,7 +207,7 @@ func Display(g *Grid) bool { ui.Handle("/sys/wnd/resize", func(e ui.Event) { ui.Body.Width = ui.TermWidth() - log.Infof("resize: width=%v", ui.Body.Width) + log.Infof("resize: width=%v max-rows=%v", ui.Body.Width, g.maxRows) g.redrawRows() }) diff --git a/widgets/compact.go b/widgets/compact.go index fd3ea62..9070f3c 100644 --- a/widgets/compact.go +++ b/widgets/compact.go @@ -24,12 +24,12 @@ type ContainerWidgets interface { } var CompactHeader = ui.NewRow( - ui.NewCol(1, 0, slimPar("")), - ui.NewCol(2, 0, slimPar("NAME")), - ui.NewCol(2, 0, slimPar("CID")), - ui.NewCol(2, 0, slimPar("CPU")), - ui.NewCol(2, 0, slimPar("MEM")), - ui.NewCol(2, 0, slimPar("NET RX/TX")), + ui.NewCol(1, 0, slimHeaderPar("")), + ui.NewCol(2, 0, slimHeaderPar("NAME")), + ui.NewCol(2, 0, slimHeaderPar("CID")), + ui.NewCol(2, 0, slimHeaderPar("CPU")), + ui.NewCol(2, 0, slimHeaderPar("MEM")), + ui.NewCol(2, 0, slimHeaderPar("NET RX/TX")), ) type Compact struct { @@ -39,24 +39,19 @@ type Compact struct { Name *ui.Par Cpu *ui.Gauge Memory *ui.Gauge + row *ui.Row } func NewCompact(id string, name string) *Compact { - return &Compact{ - Status: slimPar(""), + w := &Compact{ + Status: slimPar(mark), Cid: slimPar(id), Net: slimPar("-"), Name: slimPar(name), Cpu: slimGauge(), Memory: slimGauge(), } -} - -func (w *Compact) Render() { -} - -func (w *Compact) Row() *ui.Row { - return ui.NewRow( + w.row = ui.NewRow( ui.NewCol(1, 0, w.Status), ui.NewCol(2, 0, w.Name), ui.NewCol(2, 0, w.Cid), @@ -64,6 +59,14 @@ func (w *Compact) Row() *ui.Row { ui.NewCol(2, 0, w.Memory), ui.NewCol(2, 0, w.Net), ) + return w +} + +func (w *Compact) Render() { +} + +func (w *Compact) Row() *ui.Row { + return w.row } func (w *Compact) Highlight() { @@ -137,6 +140,12 @@ func centerParText(p *ui.Par) { p.Text = fmt.Sprintf("%s%s", padding, text) } +func slimHeaderPar(s string) *ui.Par { + p := slimPar(s) + p.Height = 2 + return p +} + func slimPar(s string) *ui.Par { p := ui.NewPar(s) p.Border = false