From c62e11416fd1aed5fdbd34a2d83388a52853209c Mon Sep 17 00:00:00 2001 From: Arthur Conti Date: Sun, 28 Aug 2022 23:30:18 -0300 Subject: [PATCH] adding optional TextStatus collumn --- colors.go | 2 + config/columns.go | 5 ++ cwidgets/compact/column.go | 29 +++++----- cwidgets/compact/textStatus.go | 96 ++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 14 deletions(-) create mode 100644 cwidgets/compact/textStatus.go diff --git a/colors.go b/colors.go index 7619e286..40c1f622 100644 --- a/colors.go +++ b/colors.go @@ -48,6 +48,8 @@ var ColorMap = map[string]ui.Attribute{ "status.ok": ui.ColorGreen, "status.warn": ui.ColorYellow, "status.danger": ui.ColorRed, + "status.paused": ui.ColorBlue, + "status.exited": ui.ColorYellow, } func InvertColorMap() { diff --git a/config/columns.go b/config/columns.go index 6f564fa3..6bfb3878 100644 --- a/config/columns.go +++ b/config/columns.go @@ -6,6 +6,11 @@ import ( // defaults var defaultColumns = []Column{ + { + Name: "textStatus", + Label: "Text Status Indicator", + Enabled: false, + }, { Name: "status", Label: "Status Indicator", diff --git a/cwidgets/compact/column.go b/cwidgets/compact/column.go index eeb16af8..1a9db94d 100644 --- a/cwidgets/compact/column.go +++ b/cwidgets/compact/column.go @@ -9,20 +9,21 @@ import ( var ( allCols = map[string]NewCompactColFn{ - "status": NewStatus, - "name": NewNameCol, - "id": NewCIDCol, - "image": NewImageCol, - "ports": NewPortsCol, - "IPs": NewIpsCol, - "created": NewCreatedCol, - "cpu": NewCPUCol, - "cpus": NewCpuScaledCol, - "mem": NewMemCol, - "net": NewNetCol, - "io": NewIOCol, - "pids": NewPIDCol, - "uptime": NewUptimeCol, + "textStatus": NewTextStatus, + "status": NewStatus, + "name": NewNameCol, + "id": NewCIDCol, + "image": NewImageCol, + "ports": NewPortsCol, + "IPs": NewIpsCol, + "created": NewCreatedCol, + "cpu": NewCPUCol, + "cpus": NewCpuScaledCol, + "mem": NewMemCol, + "net": NewNetCol, + "io": NewIOCol, + "pids": NewPIDCol, + "uptime": NewUptimeCol, } ) diff --git a/cwidgets/compact/textStatus.go b/cwidgets/compact/textStatus.go new file mode 100644 index 00000000..895718b4 --- /dev/null +++ b/cwidgets/compact/textStatus.go @@ -0,0 +1,96 @@ +package compact + +import ( + "github.com/bcicen/ctop/models" + + ui "github.com/gizak/termui" +) + +// Status indicator +type TextStatus struct { + *ui.Block + status []ui.Cell + health []ui.Cell +} + +func NewTextStatus() CompactCol { + s := &TextStatus{ + Block: ui.NewBlock(), + status: []ui.Cell{{Ch: ' '}}, + health: []ui.Cell{{Ch: ' '}}, + } + s.Height = 1 + s.Border = false + return s +} + +func (s *TextStatus) Buffer() ui.Buffer { + buf := s.Block.Buffer() + buf.Set(s.InnerX(), s.InnerY(), s.health[0]) + buf.Set(s.InnerX()+2, s.InnerY(), s.status[0]) + return buf +} + +func (s *TextStatus) SetMeta(m models.Meta) { + s.setState(m.Get("state")) + s.setHealth(m.Get("health")) +} + +// Status implements CompactCol +func (s *TextStatus) Reset() {} +func (s *TextStatus) SetMetrics(models.Metrics) {} +func (s *TextStatus) Highlight() {} +func (s *TextStatus) UnHighlight() {} +func (s *TextStatus) Header() string { return "" } +func (s *TextStatus) FixedWidth() int { return 2 } + +func (s *TextStatus) setState(val string) { + color := ui.ColorDefault + var mark string + + switch val { + case "": + return + case "created": + mark = "C" + color = ui.ThemeAttr("fg") + case "running": + mark = "R" + color = ui.ThemeAttr("status.ok") + case "exited": + mark = "X" + color = ui.ThemeAttr("status.exited") + case "paused": + mark = "P" + color = ui.ThemeAttr("status.paused") + default: + mark = " " + log.Warningf("unknown status string: \"%v\"", val) + } + + s.status = ui.TextCells(mark, color, ui.ColorDefault) +} + +func (s *TextStatus) setHealth(val string) { + color := ui.ColorDefault + var mark string + + switch val { + case "": + return + case "healthy": + mark = "H" + color = ui.ThemeAttr("status.ok") + case "unhealthy": + mark = "!" + color = ui.ThemeAttr("status.danger") + case "starting": + mark = "s" + color = ui.ThemeAttr("fg") + default: + mark = " " + log.Warningf("unknown health state string: \"%v\"", val) + } + + s.health = ui.TextCells(mark, color, ui.ColorDefault) +}