smooth cursor, row rendering. add maxRows calc to grid

This commit is contained in:
Bradley Cicenas 2017-02-20 10:23:59 +11:00
parent 549d4892eb
commit 335ac8d741
2 changed files with 58 additions and 36 deletions

53
grid.go
View File

@ -11,7 +11,8 @@ import (
type Grid struct { type Grid struct {
cursorID string // id of currently selected container cursorID string // id of currently selected container
cmap *ContainerMap cmap *ContainerMap
containers []*Container // sorted slice of containers maxRows int
containers Containers // sorted slice of containers
header *widgets.CTopHeader header *widgets.CTopHeader
} }
@ -25,10 +26,15 @@ func NewGrid() *Grid {
// set initial cursor position // set initial cursor position
if len(g.containers) > 0 { if len(g.containers) > 0 {
g.cursorID = g.containers[0].id g.cursorID = g.containers[0].id
g.containers[0].widgets.Highlight()
} }
return g return g
} }
func (g *Grid) calcMaxRows() {
g.maxRows = ui.TermHeight() - widgets.CompactHeader.Height - ui.Body.Y
}
// Return current cursor index // Return current cursor index
func (g *Grid) cursorIdx() int { func (g *Grid) cursorIdx() int {
for n, c := range g.containers { for n, c := range g.containers {
@ -42,35 +48,40 @@ func (g *Grid) cursorIdx() int {
func (g *Grid) cursorUp() { func (g *Grid) cursorUp() {
idx := g.cursorIdx() idx := g.cursorIdx()
// decrement if possible // decrement if possible
if idx > 0 { if idx <= 0 {
g.cursorID = g.containers[idx-1].id return
g.redrawCursor()
} }
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() { func (g *Grid) cursorDown() {
idx := g.cursorIdx() idx := g.cursorIdx()
// increment if possible // increment if possible
if idx < (len(g.containers) - 1) { if idx > (len(g.containers) - 1) {
g.cursorID = g.containers[idx+1].id return
g.redrawCursor()
} }
} if idx >= g.maxRows-1 {
return
}
active := g.containers[idx]
next := g.containers[idx+1]
// Redraw the cursor with the currently selected row active.widgets.UnHighlight()
func (g *Grid) redrawCursor() { g.cursorID = next.id
for _, c := range g.containers { next.widgets.Highlight()
if c.id == g.cursorID {
c.widgets.Highlight()
} else {
c.widgets.UnHighlight()
}
ui.Render(ui.Body) ui.Render(ui.Body)
}
} }
func (g *Grid) redrawRows() { func (g *Grid) redrawRows() {
// reinit body rows // reinit body rows
g.calcMaxRows()
ui.Body.Rows = []*ui.Row{} ui.Body.Rows = []*ui.Row{}
ui.Clear() ui.Clear()
@ -84,7 +95,10 @@ func (g *Grid) redrawRows() {
ui.Body.Y = 0 ui.Body.Y = 0
} }
ui.Body.AddRows(widgets.CompactHeader) 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()) ui.Body.AddRows(c.widgets.Row())
} }
@ -144,7 +158,6 @@ func Display(g *Grid) bool {
ui.DefaultEvtStream.Hook(logEvent) ui.DefaultEvtStream.Hook(logEvent)
// initial draw // initial draw
g.redrawCursor()
g.redrawRows() g.redrawRows()
ui.Handle("/sys/kbd/<up>", func(ui.Event) { ui.Handle("/sys/kbd/<up>", func(ui.Event) {
@ -194,7 +207,7 @@ func Display(g *Grid) bool {
ui.Handle("/sys/wnd/resize", func(e ui.Event) { ui.Handle("/sys/wnd/resize", func(e ui.Event) {
ui.Body.Width = ui.TermWidth() 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() g.redrawRows()
}) })

View File

@ -24,12 +24,12 @@ type ContainerWidgets interface {
} }
var CompactHeader = ui.NewRow( var CompactHeader = ui.NewRow(
ui.NewCol(1, 0, slimPar("")), ui.NewCol(1, 0, slimHeaderPar("")),
ui.NewCol(2, 0, slimPar("NAME")), ui.NewCol(2, 0, slimHeaderPar("NAME")),
ui.NewCol(2, 0, slimPar("CID")), ui.NewCol(2, 0, slimHeaderPar("CID")),
ui.NewCol(2, 0, slimPar("CPU")), ui.NewCol(2, 0, slimHeaderPar("CPU")),
ui.NewCol(2, 0, slimPar("MEM")), ui.NewCol(2, 0, slimHeaderPar("MEM")),
ui.NewCol(2, 0, slimPar("NET RX/TX")), ui.NewCol(2, 0, slimHeaderPar("NET RX/TX")),
) )
type Compact struct { type Compact struct {
@ -39,24 +39,19 @@ type Compact struct {
Name *ui.Par Name *ui.Par
Cpu *ui.Gauge Cpu *ui.Gauge
Memory *ui.Gauge Memory *ui.Gauge
row *ui.Row
} }
func NewCompact(id string, name string) *Compact { func NewCompact(id string, name string) *Compact {
return &Compact{ w := &Compact{
Status: slimPar(""), Status: slimPar(mark),
Cid: slimPar(id), Cid: slimPar(id),
Net: slimPar("-"), Net: slimPar("-"),
Name: slimPar(name), Name: slimPar(name),
Cpu: slimGauge(), Cpu: slimGauge(),
Memory: slimGauge(), Memory: slimGauge(),
} }
} w.row = ui.NewRow(
func (w *Compact) Render() {
}
func (w *Compact) Row() *ui.Row {
return ui.NewRow(
ui.NewCol(1, 0, w.Status), ui.NewCol(1, 0, w.Status),
ui.NewCol(2, 0, w.Name), ui.NewCol(2, 0, w.Name),
ui.NewCol(2, 0, w.Cid), 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.Memory),
ui.NewCol(2, 0, w.Net), 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() { func (w *Compact) Highlight() {
@ -137,6 +140,12 @@ func centerParText(p *ui.Par) {
p.Text = fmt.Sprintf("%s%s", padding, text) 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 { func slimPar(s string) *ui.Par {
p := ui.NewPar(s) p := ui.NewPar(s)
p.Border = false p.Border = false