From 1dd5e53b377762c81dbd5c39ebaa841d56b1dc60 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Mon, 6 Mar 2017 00:46:00 +0000 Subject: [PATCH] use GridBufferer as rows in CompactGrid --- cwidgets/compact/grid.go | 36 ++++++++++-------------------------- cwidgets/compact/header.go | 12 ++++++++++-- cwidgets/compact/main.go | 17 ++++++++++++++++- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/cwidgets/compact/grid.go b/cwidgets/compact/grid.go index f90bc67..a48dee0 100644 --- a/cwidgets/compact/grid.go +++ b/cwidgets/compact/grid.go @@ -4,51 +4,36 @@ import ( ui "github.com/gizak/termui" ) +var header = NewCompactHeader() + type CompactGrid struct { ui.GridBufferer - Rows []*Compact // rows to render + Rows []ui.GridBufferer X, Y int Width int Height int - header *CompactHeader cursorID string } func NewCompactGrid() *CompactGrid { - return &CompactGrid{ - header: NewCompactHeader(), - } + return &CompactGrid{} } func (cg *CompactGrid) Align() { - // update header y pos - if cg.header.Y != cg.Y { - cg.header.SetY(cg.Y) - } - // update row y pos recursively - y := cg.Y + 1 + y := cg.Y for _, r := range cg.Rows { - if r.Y != y { - r.SetY(y) - } - y += r.Height - } - - // update header width - if cg.header.Width != cg.Width { - cg.header.SetWidth(cg.Width) + r.SetY(y) + y += r.GetHeight() } // update row width recursively for _, r := range cg.Rows { - if r.Width != cg.Width { - r.SetWidth(cg.Width) - } + r.SetWidth(cg.Width) } } -func (cg *CompactGrid) Clear() { cg.Rows = []*Compact{} } +func (cg *CompactGrid) Clear() { cg.Rows = []ui.GridBufferer{header} } func (cg *CompactGrid) GetHeight() int { return len(cg.Rows) } func (cg *CompactGrid) SetX(x int) { cg.X = x } func (cg *CompactGrid) SetY(y int) { cg.Y = y } @@ -56,14 +41,13 @@ func (cg *CompactGrid) SetWidth(w int) { cg.Width = w } func (cg *CompactGrid) Buffer() ui.Buffer { buf := ui.NewBuffer() - buf.Merge(cg.header.Buffer()) for _, r := range cg.Rows { buf.Merge(r.Buffer()) } return buf } -func (cg *CompactGrid) AddRows(rows ...*Compact) { +func (cg *CompactGrid) AddRows(rows ...ui.GridBufferer) { for _, r := range rows { cg.Rows = append(cg.Rows, r) } diff --git a/cwidgets/compact/header.go b/cwidgets/compact/header.go index 324322f..08fb4a0 100644 --- a/cwidgets/compact/header.go +++ b/cwidgets/compact/header.go @@ -13,15 +13,19 @@ type CompactHeader struct { func NewCompactHeader() *CompactHeader { fields := []string{"", "NAME", "CID", "CPU", "MEM", "NET RX/TX"} - header := &CompactHeader{} + header := &CompactHeader{Height: 2} for _, f := range fields { header.pars = append(header.pars, headerPar(f)) } return header } +func (c *CompactHeader) GetHeight() int { + return c.Height +} + func (c *CompactHeader) SetWidth(w int) { - x := 1 + x := c.X autoWidth := calcWidth(w, 5) for n, col := range c.pars { // set status column to static width @@ -38,6 +42,10 @@ func (c *CompactHeader) SetWidth(w int) { c.Width = w } +func (c *CompactHeader) SetX(x int) { + c.X = x +} + func (c *CompactHeader) SetY(y int) { for _, p := range c.pars { p.SetY(y) diff --git a/cwidgets/compact/main.go b/cwidgets/compact/main.go index cadf9d6..665bbb8 100644 --- a/cwidgets/compact/main.go +++ b/cwidgets/compact/main.go @@ -32,6 +32,7 @@ func NewCompact(id string) *Compact { Cpu: NewGaugeCol(), Memory: NewGaugeCol(), Net: NewTextCol("-"), + X: 1, Height: 1, } return row @@ -58,7 +59,18 @@ func (row *Compact) Reset() { row.Net.Reset() } +func (row *Compact) GetHeight() int { + return row.Height +} + +func (row *Compact) SetX(x int) { + row.X = x +} + func (row *Compact) SetY(y int) { + if y == row.Y { + return + } for _, col := range row.all() { col.SetY(y) } @@ -66,7 +78,10 @@ func (row *Compact) SetY(y int) { } func (row *Compact) SetWidth(width int) { - x := 1 + if width == row.Width { + return + } + x := row.X autoWidth := calcWidth(width, 5) for n, col := range row.all() { // set status column to static width