ctop/cwidgets/compact/status.go

95 lines
1.9 KiB
Go
Raw Normal View History

2017-03-06 01:15:32 +01:00
package compact
import (
"github.com/bcicen/ctop/models"
2017-03-06 01:15:32 +01:00
ui "github.com/gizak/termui"
)
// Status indicator
type Status struct {
*ui.Block
status []ui.Cell
health []ui.Cell
2017-03-06 01:15:32 +01:00
}
2019-11-06 13:31:57 +01:00
func NewStatus() CompactCol {
2019-06-22 20:42:48 +02:00
s := &Status{
Block: ui.NewBlock(),
2020-11-25 21:21:19 +01:00
status: []ui.Cell{{Ch: ' '}},
2019-06-22 20:42:48 +02:00
health: []ui.Cell{{Ch: ' '}},
}
s.Height = 1
s.Border = false
return s
}
func (s *Status) 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
2017-03-06 01:15:32 +01: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-06 01:05:21 +02:00
func (s *Status) Header() string { return "" }
func (s *Status) FixedWidth() int { return 3 }
func (s *Status) setState(val string) {
2017-03-06 01:15:32 +01:00
color := ui.ColorDefault
2020-11-25 21:29:10 +01:00
var mark string
2017-03-06 01:15:32 +01:00
switch val {
2020-11-25 21:21:19 +01:00
case "":
return
case "created":
2020-11-25 21:29:10 +01:00
mark = "◉"
2017-03-06 01:15:32 +01:00
case "running":
mark = "▶"
color = ui.ThemeAttr("status.ok")
2017-03-06 01:15:32 +01:00
case "exited":
2020-11-25 21:29:10 +01:00
mark = "⏹"
color = ui.ThemeAttr("status.danger")
2017-03-06 01:15:32 +01:00
case "paused":
2020-11-25 21:29:10 +01:00
mark = "⏸"
2020-11-25 21:21:19 +01:00
default:
2020-11-25 21:29:10 +01:00
mark = " "
2020-11-25 21:21:19 +01:00
log.Warningf("unknown status string: \"%v\"", val)
2017-03-06 01:15:32 +01:00
}
2020-11-25 21:29:10 +01:00
s.status = ui.TextCells(mark, color, ui.ColorDefault)
}
func (s *Status) setHealth(val string) {
color := ui.ColorDefault
2020-11-25 21:21:19 +01:00
var mark string
switch val {
case "":
return
case "healthy":
2020-11-25 21:29:10 +01:00
mark = "☼"
color = ui.ThemeAttr("status.ok")
case "unhealthy":
2020-11-25 21:29:10 +01:00
mark = "⚠"
color = ui.ThemeAttr("status.danger")
case "starting":
2020-11-25 21:29:10 +01:00
mark = "◌"
color = ui.ThemeAttr("status.warn")
default:
2020-11-25 21:21:19 +01:00
mark = " "
log.Warningf("unknown health state string: \"%v\"", val)
}
2019-06-22 20:42:48 +02:00
s.health = ui.TextCells(mark, color, ui.ColorDefault)
2017-03-06 01:15:32 +01:00
}