mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
158 lines
3.0 KiB
Go
158 lines
3.0 KiB
Go
package compact
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/bcicen/ctop/cwidgets"
|
|
"github.com/bcicen/ctop/logging"
|
|
ui "github.com/gizak/termui"
|
|
)
|
|
|
|
var log = logging.Init()
|
|
|
|
const (
|
|
mark = string('\u25C9')
|
|
vBar = string('\u25AE')
|
|
colSpacing = 1
|
|
statusWidth = 3
|
|
)
|
|
|
|
type Compact struct {
|
|
Status *ui.Par
|
|
Cid *ui.Par
|
|
Net *ui.Par
|
|
Name *ui.Par
|
|
Cpu *ui.Gauge
|
|
Memory *ui.Gauge
|
|
X, Y int
|
|
Width int
|
|
Height int
|
|
}
|
|
|
|
func NewCompact(id, name, status string) *Compact {
|
|
row := &Compact{
|
|
Status: slimPar(mark),
|
|
Cid: slimPar(id),
|
|
Name: slimPar(name),
|
|
Height: 1,
|
|
}
|
|
row.Reset()
|
|
row.SetStatus(status)
|
|
return row
|
|
}
|
|
|
|
// Set gauges, counters to default unread values
|
|
func (row *Compact) Reset() {
|
|
row.Net = slimPar("-")
|
|
row.Cpu = slimGauge()
|
|
row.Memory = slimGauge()
|
|
}
|
|
|
|
func (row *Compact) all() []ui.GridBufferer {
|
|
return []ui.GridBufferer{
|
|
row.Status,
|
|
row.Name,
|
|
row.Cid,
|
|
row.Cpu,
|
|
row.Memory,
|
|
row.Net,
|
|
}
|
|
}
|
|
|
|
func (row *Compact) SetY(y int) {
|
|
if y == row.Y {
|
|
return
|
|
}
|
|
for _, col := range row.all() {
|
|
col.SetY(y)
|
|
}
|
|
row.Y = y
|
|
}
|
|
|
|
func (row *Compact) SetWidth(width int) {
|
|
if row.Width == width {
|
|
return
|
|
}
|
|
x := 1
|
|
autoWidth := calcWidth(width, 5)
|
|
for n, col := range row.all() {
|
|
if n == 0 {
|
|
col.SetX(x)
|
|
col.SetWidth(statusWidth)
|
|
x += statusWidth
|
|
continue
|
|
}
|
|
col.SetX(x)
|
|
col.SetWidth(autoWidth)
|
|
x += autoWidth + colSpacing
|
|
}
|
|
row.Width = width
|
|
log.Info("resized row width")
|
|
}
|
|
|
|
func (row *Compact) Buffer() ui.Buffer {
|
|
buf := ui.NewBuffer()
|
|
|
|
buf.Merge(row.Status.Buffer())
|
|
buf.Merge(row.Name.Buffer())
|
|
buf.Merge(row.Cid.Buffer())
|
|
buf.Merge(row.Cpu.Buffer())
|
|
buf.Merge(row.Memory.Buffer())
|
|
buf.Merge(row.Net.Buffer())
|
|
|
|
return buf
|
|
}
|
|
|
|
func (row *Compact) Highlight() {
|
|
row.Name.TextFgColor = ui.ColorDefault
|
|
row.Name.TextBgColor = ui.ColorWhite
|
|
}
|
|
|
|
func (row *Compact) UnHighlight() {
|
|
row.Name.TextFgColor = ui.ColorWhite
|
|
row.Name.TextBgColor = ui.ColorDefault
|
|
}
|
|
|
|
func (row *Compact) SetStatus(val string) {
|
|
switch val {
|
|
case "running":
|
|
row.Status.Text = mark
|
|
row.Status.TextFgColor = ui.ColorGreen
|
|
case "exited":
|
|
row.Status.Text = mark
|
|
row.Status.TextFgColor = ui.ColorRed
|
|
case "paused":
|
|
row.Status.Text = fmt.Sprintf("%s%s", vBar, vBar)
|
|
row.Status.TextFgColor = ui.ColorDefault
|
|
default:
|
|
row.Status.Text = mark
|
|
row.Status.TextFgColor = ui.ColorRed
|
|
}
|
|
}
|
|
|
|
func (row *Compact) SetCPU(val int) {
|
|
row.Cpu.BarColor = cwidgets.ColorScale(val)
|
|
row.Cpu.Label = fmt.Sprintf("%s%%", strconv.Itoa(val))
|
|
if val < 5 {
|
|
val = 5
|
|
row.Cpu.BarColor = ui.ColorBlack
|
|
}
|
|
row.Cpu.Percent = val
|
|
}
|
|
|
|
func (row *Compact) SetNet(rx int64, tx int64) {
|
|
row.Net.Text = fmt.Sprintf("%s / %s", cwidgets.ByteFormat(rx), cwidgets.ByteFormat(tx))
|
|
}
|
|
|
|
func (row *Compact) SetMem(val int64, limit int64, percent int) {
|
|
row.Memory.Label = fmt.Sprintf("%s / %s", cwidgets.ByteFormat(val), cwidgets.ByteFormat(limit))
|
|
if percent < 5 {
|
|
percent = 5
|
|
row.Memory.BarColor = ui.ColorBlack
|
|
} else {
|
|
row.Memory.BarColor = ui.ColorGreen
|
|
}
|
|
row.Memory.Percent = percent
|
|
}
|