add common column width calculation method, global column spacing

This commit is contained in:
Bradley Cicenas 2017-02-26 21:23:56 +00:00
parent 05b50af87b
commit 4aaf26b63d

View File

@ -11,8 +11,10 @@ import (
var log = logging.Init() var log = logging.Init()
const ( const (
mark = string('\u25C9') mark = string('\u25C9')
vBar = string('\u25AE') vBar = string('\u25AE')
colSpacing = 1
statusWidth = 3
) )
type CompactGrid struct { type CompactGrid struct {
@ -85,10 +87,15 @@ func NewCompactHeader() *CompactHeader {
return header return header
} }
// Calculate per-column width, given total width and number of items
func calcWidth(width, items int) int {
spacing := colSpacing * items
return (width - statusWidth - spacing) / items
}
func (c *CompactHeader) SetWidth(w int) { func (c *CompactHeader) SetWidth(w int) {
x := 1 x := 1
statusWidth := 3 autoWidth := calcWidth(w, 5)
autoWidth := (w - statusWidth) / 5
for n, col := range c.pars { for n, col := range c.pars {
if n == 0 { if n == 0 {
col.SetX(x) col.SetX(x)
@ -98,7 +105,7 @@ func (c *CompactHeader) SetWidth(w int) {
} }
col.SetX(x) col.SetX(x)
col.SetWidth(autoWidth) col.SetWidth(autoWidth)
x += autoWidth x += autoWidth + colSpacing
} }
} }
@ -162,8 +169,7 @@ func (w *Compact) SetY(y int) {
func (w *Compact) SetWidth(width int) { func (w *Compact) SetWidth(width int) {
x := 1 x := 1
statusWidth := 3 autoWidth := calcWidth(width, 5)
autoWidth := (width - statusWidth) / 5
for n, col := range w.all() { for n, col := range w.all() {
if n == 0 { if n == 0 {
col.SetX(x) col.SetX(x)
@ -173,7 +179,7 @@ func (w *Compact) SetWidth(width int) {
} }
col.SetX(x) col.SetX(x)
col.SetWidth(autoWidth) col.SetWidth(autoWidth)
x += autoWidth x += autoWidth + colSpacing
} }
} }
@ -287,6 +293,5 @@ func slimGauge() *ui.Gauge {
g.PaddingBottom = 0 g.PaddingBottom = 0
g.BarColor = ui.ColorGreen g.BarColor = ui.ColorGreen
g.Label = "-" g.Label = "-"
g.Bg = ui.ColorBlack
return g return g
} }