diff --git a/cwidgets/compact/grid.go b/cwidgets/compact/grid.go index ec03f77..f90bc67 100644 --- a/cwidgets/compact/grid.go +++ b/cwidgets/compact/grid.go @@ -21,17 +21,30 @@ func NewCompactGrid() *CompactGrid { } func (cg *CompactGrid) Align() { - // Update y recursively - cg.header.SetY(cg.Y) + // update header y pos + if cg.header.Y != cg.Y { + cg.header.SetY(cg.Y) + } + + // update row y pos recursively y := cg.Y + 1 for _, r := range cg.Rows { - r.SetY(y) + if r.Y != y { + r.SetY(y) + } y += r.Height } - // Update width recursively - cg.header.SetWidth(cg.Width) + + // update header width + if cg.header.Width != cg.Width { + cg.header.SetWidth(cg.Width) + } + + // update row width recursively for _, r := range cg.Rows { - r.SetWidth(cg.Width) + if r.Width != cg.Width { + r.SetWidth(cg.Width) + } } } diff --git a/cwidgets/compact/header.go b/cwidgets/compact/header.go index 9a739f5..324322f 100644 --- a/cwidgets/compact/header.go +++ b/cwidgets/compact/header.go @@ -5,28 +5,26 @@ import ( ) type CompactHeader struct { - pars []*ui.Par X, Y int Width int Height int + pars []*ui.Par } func NewCompactHeader() *CompactHeader { fields := []string{"", "NAME", "CID", "CPU", "MEM", "NET RX/TX"} header := &CompactHeader{} for _, f := range fields { - header.pars = append(header.pars, slimHeaderPar(f)) + header.pars = append(header.pars, headerPar(f)) } return header } func (c *CompactHeader) SetWidth(w int) { - if w == c.Width { - return - } x := 1 autoWidth := calcWidth(w, 5) for n, col := range c.pars { + // set status column to static width if n == 0 { col.SetX(x) col.SetWidth(statusWidth) @@ -41,9 +39,6 @@ func (c *CompactHeader) SetWidth(w int) { } func (c *CompactHeader) SetY(y int) { - if y == c.Y { - return - } for _, p := range c.pars { p.SetY(y) } @@ -57,3 +52,12 @@ func (c *CompactHeader) Buffer() ui.Buffer { } return buf } + +func headerPar(s string) *ui.Par { + p := ui.NewPar(s) + p.Y = 2 + p.Height = 2 + p.Width = 20 + p.Border = false + return p +} diff --git a/cwidgets/compact/main.go b/cwidgets/compact/main.go index c45712a..cadf9d6 100644 --- a/cwidgets/compact/main.go +++ b/cwidgets/compact/main.go @@ -1,10 +1,6 @@ package compact import ( - "fmt" - "strconv" - - "github.com/bcicen/ctop/cwidgets" "github.com/bcicen/ctop/logging" "github.com/bcicen/ctop/metrics" ui "github.com/gizak/termui" @@ -12,10 +8,6 @@ import ( var log = logging.Init() -const ( - colSpacing = 1 -) - type Compact struct { Status *Status Name *TextCol @@ -66,21 +58,7 @@ func (row *Compact) Reset() { row.Net.Reset() } -func (row *Compact) all() []ui.GridBufferer { - return []ui.GridBufferer{ - row.Status, - row.Name, - row.Cid, - row.Cpu, - row.Memory, - row.Net, - } -} - func (row *Compact) SetY(y int) { - if y == row.Y { - return - } for _, col := range row.all() { col.SetY(y) } @@ -88,12 +66,10 @@ func (row *Compact) SetY(y int) { } func (row *Compact) SetWidth(width int) { - if row.Width == width { - return - } x := 1 autoWidth := calcWidth(width, 5) for n, col := range row.all() { + // set status column to static width if n == 0 { col.SetX(x) col.SetWidth(statusWidth) @@ -120,28 +96,13 @@ func (row *Compact) Buffer() ui.Buffer { return buf } -func (row *Compact) SetNet(rx int64, tx int64) { - label := fmt.Sprintf("%s / %s", cwidgets.ByteFormat(rx), cwidgets.ByteFormat(tx)) - row.Net.Set(label) -} - -func (row *Compact) SetCPU(val int) { - row.Cpu.BarColor = colorScale(val) - row.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val)) - if val < 5 { - val = 5 - row.Cpu.BarColor = ui.ColorBlack +func (row *Compact) all() []ui.GridBufferer { + return []ui.GridBufferer{ + row.Status, + row.Name, + row.Cid, + row.Cpu, + row.Memory, + row.Net, } - row.Cpu.Percent = val -} - -func (row *Compact) SetMem(val int64, limit int64, percent int) { - row.Memory.Label = fmt.Sprintf("%s / %s", cwidgets.ByteFormat(val), cwidgets.ByteFormat(limit)) - if percent < 5 { - percent = 5 - row.Memory.BarColor = ui.ColorBlack - } else { - row.Memory.BarColor = ui.ColorGreen - } - row.Memory.Percent = percent } diff --git a/cwidgets/compact/setters.go b/cwidgets/compact/setters.go new file mode 100644 index 0000000..cc2eb0e --- /dev/null +++ b/cwidgets/compact/setters.go @@ -0,0 +1,35 @@ +package compact + +import ( + "fmt" + "strconv" + + "github.com/bcicen/ctop/cwidgets" + ui "github.com/gizak/termui" +) + +func (row *Compact) SetNet(rx int64, tx int64) { + label := fmt.Sprintf("%s / %s", cwidgets.ByteFormat(rx), cwidgets.ByteFormat(tx)) + row.Net.Set(label) +} + +func (row *Compact) SetCPU(val int) { + row.Cpu.BarColor = colorScale(val) + row.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val)) + if val < 5 { + val = 5 + row.Cpu.BarColor = ui.ColorBlack + } + row.Cpu.Percent = val +} + +func (row *Compact) SetMem(val int64, limit int64, percent int) { + row.Memory.Label = fmt.Sprintf("%s / %s", cwidgets.ByteFormat(val), cwidgets.ByteFormat(limit)) + if percent < 5 { + percent = 5 + row.Memory.BarColor = ui.ColorBlack + } else { + row.Memory.BarColor = ui.ColorGreen + } + row.Memory.Percent = percent +} diff --git a/cwidgets/compact/status.go b/cwidgets/compact/status.go new file mode 100644 index 0000000..e83ceab --- /dev/null +++ b/cwidgets/compact/status.go @@ -0,0 +1,44 @@ +package compact + +import ( + "fmt" + + ui "github.com/gizak/termui" +) + +const ( + mark = string('\u25C9') + vBar = string('\u25AE') + statusWidth = 3 +) + +// Status indicator +type Status struct { + *ui.Par +} + +func NewStatus() *Status { + p := ui.NewPar(mark) + p.Border = false + p.Height = 1 + p.Width = statusWidth + return &Status{p} +} + +func (s *Status) Set(val string) { + // defaults + text := mark + color := ui.ColorDefault + + switch val { + case "running": + color = ui.ColorGreen + case "exited": + color = ui.ColorRed + case "paused": + text = fmt.Sprintf("%s%s", vBar, vBar) + } + + s.Text = text + s.TextFgColor = color +} diff --git a/cwidgets/compact/text.go b/cwidgets/compact/text.go index 84064f9..4f4c651 100644 --- a/cwidgets/compact/text.go +++ b/cwidgets/compact/text.go @@ -1,17 +1,9 @@ package compact import ( - "fmt" - ui "github.com/gizak/termui" ) -const ( - mark = string('\u25C9') - vBar = string('\u25AE') - statusWidth = 3 -) - type TextCol struct { *ui.Par } @@ -41,33 +33,3 @@ func (w *TextCol) Reset() { func (w *TextCol) Set(s string) { w.Text = s } - -type Status struct { - *ui.Par -} - -func NewStatus() *Status { - p := ui.NewPar(mark) - p.Border = false - p.Height = 1 - p.Width = statusWidth - return &Status{p} -} - -func (s *Status) Set(val string) { - // defaults - text := mark - color := ui.ColorDefault - - switch val { - case "running": - color = ui.ColorGreen - case "exited": - color = ui.ColorRed - case "paused": - text = fmt.Sprintf("%s%s", vBar, vBar) - } - - s.Text = text - s.TextFgColor = color -} diff --git a/cwidgets/compact/util.go b/cwidgets/compact/util.go index b7f10aa..356b9a0 100644 --- a/cwidgets/compact/util.go +++ b/cwidgets/compact/util.go @@ -7,21 +7,14 @@ import ( ui "github.com/gizak/termui" ) +const colSpacing = 1 + // 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 slimHeaderPar(s string) *ui.Par { - p := ui.NewPar(s) - p.Y = 2 - p.Height = 2 - p.Width = 20 - p.Border = false - return p -} - func centerParText(p *ui.Par) { var text string var padding string