include addFieldPar method with CompactHeader struct

This commit is contained in:
Bradley Cicenas 2017-03-06 01:09:46 +00:00
parent 1dd5e53b37
commit 4b035ad52a

View File

@ -13,21 +13,22 @@ 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{Height: 2} ch := &CompactHeader{}
ch.Height = 2
for _, f := range fields { for _, f := range fields {
header.pars = append(header.pars, headerPar(f)) ch.addFieldPar(f)
} }
return header return ch
} }
func (c *CompactHeader) GetHeight() int { func (ch *CompactHeader) GetHeight() int {
return c.Height return ch.Height
} }
func (c *CompactHeader) SetWidth(w int) { func (ch *CompactHeader) SetWidth(w int) {
x := c.X x := ch.X
autoWidth := calcWidth(w, 5) autoWidth := calcWidth(w, 5)
for n, col := range c.pars { for n, col := range ch.pars {
// set status column to static width // set status column to static width
if n == 0 { if n == 0 {
col.SetX(x) col.SetX(x)
@ -39,33 +40,31 @@ func (c *CompactHeader) SetWidth(w int) {
col.SetWidth(autoWidth) col.SetWidth(autoWidth)
x += autoWidth + colSpacing x += autoWidth + colSpacing
} }
c.Width = w ch.Width = w
} }
func (c *CompactHeader) SetX(x int) { func (ch *CompactHeader) SetX(x int) {
c.X = x ch.X = x
} }
func (c *CompactHeader) SetY(y int) { func (ch *CompactHeader) SetY(y int) {
for _, p := range c.pars { for _, p := range ch.pars {
p.SetY(y) p.SetY(y)
} }
c.Y = y ch.Y = y
} }
func (c *CompactHeader) Buffer() ui.Buffer { func (ch *CompactHeader) Buffer() ui.Buffer {
buf := ui.NewBuffer() buf := ui.NewBuffer()
for _, p := range c.pars { for _, p := range ch.pars {
buf.Merge(p.Buffer()) buf.Merge(p.Buffer())
} }
return buf return buf
} }
func headerPar(s string) *ui.Par { func (ch *CompactHeader) addFieldPar(s string) {
p := ui.NewPar(s) p := ui.NewPar(s)
p.Y = 2 p.Height = ch.Height
p.Height = 2
p.Width = 20
p.Border = false p.Border = false
return p ch.pars = append(ch.pars, p)
} }