use GridBufferer as rows in CompactGrid

This commit is contained in:
Bradley Cicenas 2017-03-06 00:46:00 +00:00
parent 3bb48e91ae
commit 1dd5e53b37
3 changed files with 36 additions and 29 deletions

View File

@ -4,51 +4,36 @@ import (
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
) )
var header = NewCompactHeader()
type CompactGrid struct { type CompactGrid struct {
ui.GridBufferer ui.GridBufferer
Rows []*Compact // rows to render Rows []ui.GridBufferer
X, Y int X, Y int
Width int Width int
Height int Height int
header *CompactHeader
cursorID string cursorID string
} }
func NewCompactGrid() *CompactGrid { func NewCompactGrid() *CompactGrid {
return &CompactGrid{ return &CompactGrid{}
header: NewCompactHeader(),
}
} }
func (cg *CompactGrid) Align() { func (cg *CompactGrid) Align() {
// update header y pos
if cg.header.Y != cg.Y {
cg.header.SetY(cg.Y)
}
// update row y pos recursively // update row y pos recursively
y := cg.Y + 1 y := cg.Y
for _, r := range cg.Rows { for _, r := range cg.Rows {
if r.Y != y { r.SetY(y)
r.SetY(y) y += r.GetHeight()
}
y += r.Height
}
// update header width
if cg.header.Width != cg.Width {
cg.header.SetWidth(cg.Width)
} }
// update row width recursively // update row width recursively
for _, r := range cg.Rows { 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) GetHeight() int { return len(cg.Rows) }
func (cg *CompactGrid) SetX(x int) { cg.X = x } func (cg *CompactGrid) SetX(x int) { cg.X = x }
func (cg *CompactGrid) SetY(y int) { cg.Y = y } 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 { func (cg *CompactGrid) Buffer() ui.Buffer {
buf := ui.NewBuffer() buf := ui.NewBuffer()
buf.Merge(cg.header.Buffer())
for _, r := range cg.Rows { for _, r := range cg.Rows {
buf.Merge(r.Buffer()) buf.Merge(r.Buffer())
} }
return buf return buf
} }
func (cg *CompactGrid) AddRows(rows ...*Compact) { func (cg *CompactGrid) AddRows(rows ...ui.GridBufferer) {
for _, r := range rows { for _, r := range rows {
cg.Rows = append(cg.Rows, r) cg.Rows = append(cg.Rows, r)
} }

View File

@ -13,15 +13,19 @@ type CompactHeader struct {
func NewCompactHeader() *CompactHeader { func NewCompactHeader() *CompactHeader {
fields := []string{"", "NAME", "CID", "CPU", "MEM", "NET RX/TX"} fields := []string{"", "NAME", "CID", "CPU", "MEM", "NET RX/TX"}
header := &CompactHeader{} header := &CompactHeader{Height: 2}
for _, f := range fields { for _, f := range fields {
header.pars = append(header.pars, headerPar(f)) header.pars = append(header.pars, headerPar(f))
} }
return header return header
} }
func (c *CompactHeader) GetHeight() int {
return c.Height
}
func (c *CompactHeader) SetWidth(w int) { func (c *CompactHeader) SetWidth(w int) {
x := 1 x := c.X
autoWidth := calcWidth(w, 5) autoWidth := calcWidth(w, 5)
for n, col := range c.pars { for n, col := range c.pars {
// set status column to static width // set status column to static width
@ -38,6 +42,10 @@ func (c *CompactHeader) SetWidth(w int) {
c.Width = w c.Width = w
} }
func (c *CompactHeader) SetX(x int) {
c.X = x
}
func (c *CompactHeader) SetY(y int) { func (c *CompactHeader) SetY(y int) {
for _, p := range c.pars { for _, p := range c.pars {
p.SetY(y) p.SetY(y)

View File

@ -32,6 +32,7 @@ func NewCompact(id string) *Compact {
Cpu: NewGaugeCol(), Cpu: NewGaugeCol(),
Memory: NewGaugeCol(), Memory: NewGaugeCol(),
Net: NewTextCol("-"), Net: NewTextCol("-"),
X: 1,
Height: 1, Height: 1,
} }
return row return row
@ -58,7 +59,18 @@ func (row *Compact) Reset() {
row.Net.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) { func (row *Compact) SetY(y int) {
if y == row.Y {
return
}
for _, col := range row.all() { for _, col := range row.all() {
col.SetY(y) col.SetY(y)
} }
@ -66,7 +78,10 @@ func (row *Compact) SetY(y int) {
} }
func (row *Compact) SetWidth(width int) { func (row *Compact) SetWidth(width int) {
x := 1 if width == row.Width {
return
}
x := row.X
autoWidth := calcWidth(width, 5) autoWidth := calcWidth(width, 5)
for n, col := range row.all() { for n, col := range row.all() {
// set status column to static width // set status column to static width