refactor widgets, add wrapper structs

This commit is contained in:
Bradley Cicenas
2017-03-03 07:57:26 +00:00
parent 9f5cd42b73
commit 56be64367b
12 changed files with 249 additions and 195 deletions

25
cwidgets/compact/gauge.go Normal file
View File

@@ -0,0 +1,25 @@
package compact
import (
ui "github.com/gizak/termui"
)
type GaugeCol struct {
*ui.Gauge
}
func NewGaugeCol() *GaugeCol {
g := ui.NewGauge()
g.Height = 1
g.Border = false
g.Percent = 0
g.PaddingBottom = 0
g.BarColor = ui.ColorGreen
g.Label = "-"
return &GaugeCol{g}
}
func (w *GaugeCol) Reset() {
w.Label = "-"
w.Percent = 0
}

View File

@@ -1,13 +1,12 @@
package compact
import (
"github.com/bcicen/ctop/cwidgets"
ui "github.com/gizak/termui"
)
type CompactGrid struct {
ui.GridBufferer
Rows []cwidgets.ContainerWidgets
Rows []*Compact // rows to render
X, Y int
Width int
Height int
@@ -21,31 +20,37 @@ func NewCompactGrid() *CompactGrid {
}
}
func (c *CompactGrid) Align() {
func (cg *CompactGrid) Align() {
// Update y recursively
c.header.SetY(c.Y)
y := c.Y + 1
for n, r := range c.Rows {
cg.header.SetY(cg.Y)
y := cg.Y + 1
for n, r := range cg.Rows {
r.SetY(y + n)
}
// Update width recursively
c.header.SetWidth(c.Width)
for _, r := range c.Rows {
r.SetWidth(c.Width)
cg.header.SetWidth(cg.Width)
for _, r := range cg.Rows {
r.SetWidth(cg.Width)
}
}
func (c *CompactGrid) Clear() { c.Rows = []cwidgets.ContainerWidgets{} }
func (c *CompactGrid) GetHeight() int { return len(c.Rows) }
func (c *CompactGrid) SetX(x int) { c.X = x }
func (c *CompactGrid) SetY(y int) { c.Y = y }
func (c *CompactGrid) SetWidth(w int) { c.Width = w }
func (cg *CompactGrid) Clear() { cg.Rows = []*Compact{} }
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 }
func (cg *CompactGrid) SetWidth(w int) { cg.Width = w }
func (c *CompactGrid) Buffer() ui.Buffer {
func (cg *CompactGrid) Buffer() ui.Buffer {
buf := ui.NewBuffer()
buf.Merge(c.header.Buffer())
for _, r := range c.Rows {
buf.Merge(cg.header.Buffer())
for _, r := range cg.Rows {
buf.Merge(r.Buffer())
}
return buf
}
func (cg *CompactGrid) AddRows(rows ...*Compact) {
for _, r := range rows {
cg.Rows = append(cg.Rows, r)
}
}

View File

@@ -5,50 +5,53 @@ import (
"strconv"
"github.com/bcicen/ctop/cwidgets"
"github.com/bcicen/ctop/logging"
"github.com/bcicen/ctop/metrics"
ui "github.com/gizak/termui"
)
var log = logging.Init()
const (
mark = string('\u25C9')
vBar = string('\u25AE')
colSpacing = 1
statusWidth = 3
colSpacing = 1
)
type Compact struct {
Status *ui.Par
Name *ui.Par
Cid *ui.Par
Cpu *ui.Gauge
Memory *ui.Gauge
Net *ui.Par
Status *Status
Name *TextCol
Cid *TextCol
Cpu *GaugeCol
Memory *GaugeCol
Net *TextCol
X, Y int
Width int
Height int
}
func NewCompact(id, name, status string) *Compact {
func NewCompact(id, name string) *Compact {
row := &Compact{
Status: slimPar(mark),
Name: slimPar(name),
Cid: slimPar(id),
Cpu: slimGauge(),
Memory: slimGauge(),
Net: slimPar("-"),
Status: NewStatus(),
Name: NewTextCol(name),
Cid: NewTextCol(id),
Cpu: NewGaugeCol(),
Memory: NewGaugeCol(),
Net: NewTextCol("-"),
Height: 1,
}
row.Reset()
row.SetStatus(status)
return row
}
func (row *Compact) SetMetrics(m metrics.Metrics) {
row.SetCPU(m.CPUUtil)
row.SetNet(m.NetRx, m.NetTx)
row.SetMem(m.MemUsage, m.MemLimit, m.MemPercent)
}
// Set gauges, counters to default unread values
func (row *Compact) Reset() {
row.Cpu.Percent = 0
row.Cpu.Label = "-"
row.Memory.Percent = 0
row.Memory.Label = "-"
row.Net.Text = "-"
row.Cpu.Reset()
row.Memory.Reset()
row.Net.Reset()
}
func (row *Compact) all() []ui.GridBufferer {
@@ -115,24 +118,9 @@ func (row *Compact) UnHighlight() {
row.Name.TextBgColor = ui.ColorDefault
}
func (row *Compact) SetStatus(val string) {
switch val {
case "running":
row.Status.Text = mark
row.Status.TextFgColor = ui.ColorGreen
case "exited":
row.Status.Text = mark
row.Status.TextFgColor = ui.ColorRed
case "paused":
row.Status.Text = fmt.Sprintf("%s%s", vBar, vBar)
row.Status.TextFgColor = ui.ColorDefault
case "created":
row.Status.Text = mark
row.Status.TextFgColor = ui.ColorDefault
default:
row.Status.Text = mark
row.Status.TextFgColor = ui.ColorRed
}
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) {
@@ -145,10 +133,6 @@ func (row *Compact) SetCPU(val int) {
row.Cpu.Percent = val
}
func (row *Compact) SetNet(rx int64, tx int64) {
row.Net.Text = fmt.Sprintf("%s / %s", cwidgets.ByteFormat(rx), cwidgets.ByteFormat(tx))
}
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 {

63
cwidgets/compact/text.go Normal file
View File

@@ -0,0 +1,63 @@
package compact
import (
"fmt"
ui "github.com/gizak/termui"
)
const (
mark = string('\u25C9')
vBar = string('\u25AE')
statusWidth = 3
)
type TextCol struct {
*ui.Par
}
func NewTextCol(s string) *TextCol {
p := ui.NewPar(s)
p.Border = false
p.Height = 1
p.Width = 20
return &TextCol{p}
}
func (w *TextCol) Reset() {
w.Text = "-"
}
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
}

View File

@@ -14,32 +14,14 @@ func calcWidth(width, items int) int {
}
func slimHeaderPar(s string) *ui.Par {
p := slimPar(s)
p := ui.NewPar(s)
p.Y = 2
p.Height = 2
return p
}
func slimPar(s string) *ui.Par {
p := ui.NewPar(s)
p.Border = false
p.Height = 1
p.Width = 20
p.TextFgColor = ui.ColorWhite
p.Border = false
return p
}
func slimGauge() *ui.Gauge {
g := ui.NewGauge()
g.Height = 1
g.Border = false
g.Percent = 0
g.PaddingBottom = 0
g.BarColor = ui.ColorGreen
g.Label = "-"
return g
}
func centerParText(p *ui.Par) {
var text string
var padding string

View File

@@ -8,7 +8,6 @@ import (
var log = logging.Init()
type ContainerWidgets interface {
Reset()
Buffer() ui.Buffer
Highlight()
UnHighlight()