Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding optional TextStatus collumn #311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions config/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (

// defaults
var defaultColumns = []Column{
{
Name: "textStatus",
Label: "Text Status Indicator",
Enabled: false,
},
{
Name: "status",
Label: "Status Indicator",
Expand Down
29 changes: 15 additions & 14 deletions cwidgets/compact/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)

Expand Down
96 changes: 96 additions & 0 deletions cwidgets/compact/textStatus.go
Original file line number Diff line number Diff line change
@@ -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)
}