ctop/cwidgets/compact/status.go

82 lines
1.3 KiB
Go
Raw Normal View History

2017-03-06 00:15:32 +00:00
package compact
import (
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
}
func NewStatus() *Status {
2019-06-22 18:42:48 +00:00
s := &Status{
Block: ui.NewBlock(),
health: []ui.Cell{{Ch: ' '}},
}
s.Height = 1
s.Border = false
s.Set("")
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) Set(val string) {
// 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) {
if val == "" {
return
}
2019-06-22 18:42:48 +00:00
color := ui.ColorDefault
2019-06-22 18:42:48 +00:00
mark := healthMark
switch val {
case "healthy":
color = ui.ThemeAttr("status.ok")
case "unhealthy":
color = ui.ThemeAttr("status.danger")
case "starting":
color = ui.ThemeAttr("status.warn")
}
2019-06-22 18:42:48 +00:00
s.health = ui.TextCells(mark, color, ui.ColorDefault)
2017-03-06 00:15:32 +00:00
}