mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
refactor status widget, include health indicator
This commit is contained in:
@@ -57,7 +57,7 @@ func (row *Compact) SetMeta(k, v string) {
|
|||||||
case "state":
|
case "state":
|
||||||
row.Status.Set(v)
|
row.Status.Set(v)
|
||||||
case "health":
|
case "health":
|
||||||
row.Name.Color(v)
|
row.Status.SetHealth(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,42 @@
|
|||||||
package compact
|
package compact
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
mark = string('\u25C9')
|
mark = string('\u25C9')
|
||||||
vBar = string('\u25AE')
|
healthMark = string('\u207A')
|
||||||
statusWidth = 3
|
vBar = string('\u25AE') + string('\u25AE')
|
||||||
)
|
)
|
||||||
|
|
||||||
// Status indicator
|
// Status indicator
|
||||||
type Status struct {
|
type Status struct {
|
||||||
*ui.Par
|
*ui.Block
|
||||||
|
status []ui.Cell
|
||||||
|
health []ui.Cell
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStatus() *Status {
|
func NewStatus() *Status {
|
||||||
p := ui.NewPar(mark)
|
s := &Status{Block: ui.NewBlock()}
|
||||||
p.Border = false
|
s.Height = 1
|
||||||
p.Height = 1
|
s.Border = false
|
||||||
p.Width = statusWidth
|
s.Set("")
|
||||||
return &Status{p}
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Status) Buffer() ui.Buffer {
|
||||||
|
buf := s.Block.Buffer()
|
||||||
|
x := 0
|
||||||
|
for _, c := range s.status {
|
||||||
|
buf.Set(s.InnerX()+x, s.InnerY(), c)
|
||||||
|
x += c.Width()
|
||||||
|
}
|
||||||
|
for _, c := range s.health {
|
||||||
|
buf.Set(s.InnerX()+x, s.InnerY(), c)
|
||||||
|
x += c.Width()
|
||||||
|
}
|
||||||
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Status) Set(val string) {
|
func (s *Status) Set(val string) {
|
||||||
@@ -36,9 +50,34 @@ func (s *Status) Set(val string) {
|
|||||||
case "exited":
|
case "exited":
|
||||||
color = ui.ThemeAttr("status.danger")
|
color = ui.ThemeAttr("status.danger")
|
||||||
case "paused":
|
case "paused":
|
||||||
text = fmt.Sprintf("%s%s", vBar, vBar)
|
text = vBar
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Text = text
|
var cells []ui.Cell
|
||||||
s.TextFgColor = color
|
for _, ch := range text {
|
||||||
|
cells = append(cells, ui.Cell{Ch: ch, Fg: color})
|
||||||
|
}
|
||||||
|
s.status = cells
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Status) SetHealth(val string) {
|
||||||
|
if val == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
color := ui.ColorDefault
|
||||||
|
|
||||||
|
switch val {
|
||||||
|
case "healthy":
|
||||||
|
color = ui.ThemeAttr("status.ok")
|
||||||
|
case "unhealthy":
|
||||||
|
color = ui.ThemeAttr("status.danger")
|
||||||
|
case "starting":
|
||||||
|
color = ui.ThemeAttr("status.warn")
|
||||||
|
}
|
||||||
|
|
||||||
|
var cells []ui.Cell
|
||||||
|
for _, ch := range healthMark {
|
||||||
|
cells = append(cells, ui.Cell{Ch: ch, Fg: color})
|
||||||
|
}
|
||||||
|
s.health = cells
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user