From 4709624b17f0d23b4871a80a659b9e7ba1102b8c Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Sun, 26 Feb 2017 07:31:23 +0000 Subject: [PATCH] move field header into CompactGrid, add Align() method --- containermap.go | 5 ---- grid.go | 35 ++++++++++++---------------- widgets/compact.go | 57 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 59 insertions(+), 38 deletions(-) diff --git a/containermap.go b/containermap.go index d19f45e..e76735e 100644 --- a/containermap.go +++ b/containermap.go @@ -131,11 +131,6 @@ func (cm *ContainerMap) UpdateLoop() { } } -// Return number of containers/rows -func (cm *ContainerMap) Len() uint { - return uint(len(cm.containers)) -} - // Get a single container, by ID func (cm *ContainerMap) Get(id string) (*Container, bool) { for _, c := range cm.containers { diff --git a/grid.go b/grid.go index a526233..318fdd2 100644 --- a/grid.go +++ b/grid.go @@ -8,27 +8,25 @@ import ( ui "github.com/gizak/termui" ) -var cGrid = &widgets.CompactGrid{} +var cGrid = widgets.NewCompactGrid() func maxRows() int { return ui.TermHeight() - 2 - cGrid.Y } type Grid struct { - cursorID string // id of currently selected container - cmap *ContainerMap - containers Containers // sorted slice of containers - header *widgets.CTopHeader - fieldHeader *widgets.CompactHeader + cursorID string // id of currently selected container + cmap *ContainerMap + containers Containers // sorted slice of containers + header *widgets.CTopHeader } func NewGrid() *Grid { cmap := NewContainerMap() g := &Grid{ - cmap: cmap, - containers: cmap.All(), - header: widgets.NewCTopHeader(), - fieldHeader: widgets.NewCompactHeader(), + cmap: cmap, + containers: cmap.All(), + header: widgets.NewCTopHeader(), } return g } @@ -88,7 +86,6 @@ func (g *Grid) cursorDown() { func (g *Grid) redrawRows() { // reinit body rows cGrid.Rows = []widgets.ContainerWidgets{} - ui.Clear() // build layout y := 1 @@ -98,10 +95,10 @@ func (g *Grid) redrawRows() { g.header.Render() y += g.header.Height() } + cGrid.SetY(y) var cursorVisible bool max := maxRows() - y += 2 // for field header for n, c := range g.containers.Filter() { if n >= max { break @@ -111,20 +108,16 @@ func (g *Grid) redrawRows() { cursorVisible = true } } - cGrid.SetY(y) - cGrid.SetWidth(ui.TermWidth()) - - //log.Infof("rows: %d", len(cGrid.Rows)) - //log.Infof("Width: %d", cGrid.Width) - //log.Infof("Height: %d", cGrid.Height) - //log.Infof("X: %d", cGrid.X) - //log.Infof("Y: %d", cGrid.Y) if !cursorVisible { g.cursorReset() } - ui.Render(g.fieldHeader) + ui.Clear() + if config.GetSwitchVal("enableHeader") { + g.header.Render() + } + cGrid.Align() ui.Render(cGrid) } diff --git a/widgets/compact.go b/widgets/compact.go index 15c8801..8663f20 100644 --- a/widgets/compact.go +++ b/widgets/compact.go @@ -21,26 +21,37 @@ type CompactGrid struct { X, Y int Width int Height int + header *CompactHeader } -func (c CompactGrid) SetX(x int) { c.X = x } -func (c CompactGrid) SetY(y int) { - c.Y = y +func NewCompactGrid() *CompactGrid { + return &CompactGrid{ + header: NewCompactHeader(), + } +} + +func (c *CompactGrid) Align() { + // Update y recursively + c.header.SetY(c.Y) + y := c.Y + 1 for n, r := range c.Rows { - log.Infof("row %d: y=%d", n, c.Y+n) - r.SetY(c.Y + n) + r.SetY(y + n) } -} -func (c CompactGrid) GetHeight() int { return len(c.Rows) } -func (c CompactGrid) SetWidth(w int) { - c.Width = w + // Update width recursively + c.header.SetWidth(c.Width) for _, r := range c.Rows { - r.SetWidth(w) + r.SetWidth(c.Width) } } -func (c CompactGrid) Buffer() ui.Buffer { +func (c *CompactGrid) GetHeight() int { return len(c.Rows) } +func (c *CompactGrid) SetX(x int) { c.X = x } +func (c *CompactGrid) SetY(y int) { c.Y = y } +func (c *CompactGrid) SetWidth(w int) { c.Width = w } + +func (c *CompactGrid) Buffer() ui.Buffer { buf := ui.NewBuffer() + buf.Merge(c.header.Buffer()) for _, r := range c.Rows { buf.Merge(r.Buffer()) } @@ -74,6 +85,29 @@ func NewCompactHeader() *CompactHeader { return header } +func (c *CompactHeader) SetWidth(w int) { + x := 1 + statusWidth := 3 + autoWidth := (w - statusWidth) / 5 + for n, col := range c.pars { + if n == 0 { + col.SetX(x) + col.SetWidth(statusWidth) + x += statusWidth + continue + } + col.SetX(x) + col.SetWidth(autoWidth) + x += autoWidth + } +} + +func (c *CompactHeader) SetY(y int) { + for _, p := range c.pars { + p.SetY(y) + } +} + func (c *CompactHeader) Buffer() ui.Buffer { buf := ui.NewBuffer() for _, p := range c.pars { @@ -130,7 +164,6 @@ func (w *Compact) SetWidth(width int) { x := 1 statusWidth := 3 autoWidth := (width - statusWidth) / 5 - log.Infof("autowidth: %d", autoWidth) for n, col := range w.all() { if n == 0 { col.SetX(x)