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

55
grid.go
View File

@ -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/<up>", 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()
})

View File

@ -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