ctop/cwidgets/compact/status.go

96 lines
1.8 KiB
Go
Raw Normal View History

2017-03-06 00:15:32 +00:00
package compact
import (
"github.com/bcicen/ctop/models"
2017-03-06 00:15:32 +00:00
ui "github.com/gizak/termui"
)
const (
2019-06-22 18:42:48 +00:00
mark = "◉"
healthMark = "✚"
vBar = string('\u25AE') + string('\u25AE')
2017-03-06 00:15:32 +00:00
)
// Status indicator
type Status struct {
*ui.Block
status []ui.Cell
health []ui.Cell
2017-03-06 00:15:32 +00:00
}
2019-11-06 12:31:57 +00:00
func NewStatus() CompactCol {
2019-06-22 18:42:48 +00:00
s := &Status{
Block: ui.NewBlock(),
health: []ui.Cell{{Ch: ' '}},
}
s.Height = 1
s.Border = false
s.setState("")
return s
}
func (s *Status) Buffer() ui.Buffer {
buf := s.Block.Buffer()
x := 0
2019-06-22 18:42:48 +00:00
for _, c := range s.health {
buf.Set(s.InnerX()+x, s.InnerY(), c)
x += c.Width()
}
2019-06-22 18:42:48 +00:00
x += 1
for _, c := range s.status {
buf.Set(s.InnerX()+x, s.InnerY(), c)
x += c.Width()
}
return buf
2017-03-06 00:15:32 +00:00
}
func (s *Status) SetMeta(m models.Meta) {
s.setState(m.Get("state"))
s.setHealth(m.Get("health"))
}
// Status implements CompactCol
func (s *Status) Reset() {}
func (s *Status) SetMetrics(models.Metrics) {}
func (s *Status) Highlight() {}
func (s *Status) UnHighlight() {}
2019-07-05 23:05:21 +00:00
func (s *Status) Header() string { return "" }
func (s *Status) FixedWidth() int { return 3 }
func (s *Status) setState(val string) {
2017-03-06 00:15:32 +00:00
// defaults
text := mark
color := ui.ColorDefault
switch val {
case "running":
color = ui.ThemeAttr("status.ok")
2017-03-06 00:15:32 +00:00
case "exited":
color = ui.ThemeAttr("status.danger")
2017-03-06 00:15:32 +00:00
case "paused":
text = vBar
2017-03-06 00:15:32 +00:00
}
2019-06-22 18:42:48 +00:00
s.status = ui.TextCells(text, color, ui.ColorDefault)
}
func (s *Status) setHealth(val string) {
color := ui.ColorDefault
2019-06-22 18:42:48 +00:00
mark := healthMark
switch val {
case "":
return
case "healthy":
color = ui.ThemeAttr("status.ok")
case "unhealthy":
color = ui.ThemeAttr("status.danger")
case "starting":
color = ui.ThemeAttr("status.warn")
default:
log.Warningf("unknown health state string: \"%v\"", val)
}
2019-06-22 18:42:48 +00:00
s.health = ui.TextCells(mark, color, ui.ColorDefault)
2017-03-06 00:15:32 +00:00
}