Files
ctop/cwidgets/compact/main.go
T

153 lines
2.6 KiB
Go
Raw Normal View History

package compact
import (
2018-09-17 01:33:52 +00:00
"github.com/bcicen/ctop/config"
2017-03-03 07:57:26 +00:00
"github.com/bcicen/ctop/logging"
2017-06-27 15:46:03 +00:00
"github.com/bcicen/ctop/models"
ui "github.com/gizak/termui"
)
2017-03-03 07:57:26 +00:00
var log = logging.Init()
type CompactCol interface {
ui.GridBufferer
Reset()
Highlight()
UnHighlight()
SetMeta(models.Meta)
SetMetrics(models.Metrics)
}
type Compact struct {
2018-09-17 01:24:06 +00:00
Bg *RowBg
Cols []CompactCol
X, Y int
Width int
Height int
}
func NewCompact(id string) *Compact {
// truncate container id
if len(id) > 12 {
id = id[:12]
}
row := &Compact{
Bg: NewRowBg(),
Cols: []CompactCol{
NewStatus(),
&NameCol{NewTextCol("-")},
&CIDCol{NewTextCol(id)},
&CPUCol{NewGaugeCol()},
&MemCol{NewGaugeCol()},
&NetCol{NewTextCol("-")},
&IOCol{NewTextCol("-")},
&PIDCol{NewTextCol("-")},
},
2017-03-06 00:46:00 +00:00
X: 1,
Height: 1,
}
return row
}
func (row *Compact) SetMeta(m models.Meta) {
for _, w := range row.Cols {
w.SetMeta(m)
}
}
2017-06-27 15:46:03 +00:00
func (row *Compact) SetMetrics(m models.Metrics) {
for _, w := range row.Cols {
w.SetMetrics(m)
}
2017-03-03 07:57:26 +00:00
}
// Set gauges, counters, etc. to default unread values
func (row *Compact) Reset() {
for _, w := range row.Cols {
w.Reset()
}
2017-03-06 00:46:00 +00:00
}
func (row *Compact) GetHeight() int { return row.Height }
func (row *Compact) SetX(x int) { row.X = x }
2017-03-06 00:46:00 +00:00
func (row *Compact) SetY(y int) {
2017-03-06 00:46:00 +00:00
if y == row.Y {
return
}
2018-09-17 01:24:06 +00:00
row.Bg.Y = y
for _, w := range row.Cols {
w.SetY(y)
}
row.Y = y
}
func (row *Compact) SetWidth(width int) {
2017-03-06 00:46:00 +00:00
if width == row.Width {
return
}
x := row.X
2018-09-17 01:24:06 +00:00
row.Bg.SetX(x + colWidths[0] + 1)
row.Bg.SetWidth(width)
2017-03-12 09:58:56 +00:00
autoWidth := calcWidth(width)
for n, w := range row.Cols {
// set static width, if provided
2017-03-12 09:58:56 +00:00
if colWidths[n] != 0 {
w.SetX(x)
w.SetWidth(colWidths[n])
2017-03-12 09:58:56 +00:00
x += colWidths[n]
continue
}
// else use auto width
w.SetX(x)
w.SetWidth(autoWidth)
x += autoWidth + colSpacing
}
row.Width = width
}
func (row *Compact) Buffer() ui.Buffer {
buf := ui.NewBuffer()
2018-09-17 01:24:06 +00:00
buf.Merge(row.Bg.Buffer())
for _, w := range row.Cols {
buf.Merge(w.Buffer())
}
return buf
}
2018-09-17 01:24:06 +00:00
func (row *Compact) Highlight() {
row.Cols[1].Highlight()
2018-09-17 01:33:52 +00:00
if config.GetSwitchVal("fullRowCursor") {
for _, w := range row.Cols {
w.Highlight()
}
2018-09-17 01:33:52 +00:00
}
2018-09-17 01:24:06 +00:00
}
func (row *Compact) UnHighlight() {
row.Cols[1].UnHighlight()
2018-09-17 01:33:52 +00:00
if config.GetSwitchVal("fullRowCursor") {
for _, w := range row.Cols {
w.UnHighlight()
}
2018-09-17 01:33:52 +00:00
}
2018-09-17 01:24:06 +00:00
}
type RowBg struct {
*ui.Par
}
func NewRowBg() *RowBg {
bg := ui.NewPar("")
bg.Height = 1
bg.Border = false
bg.Bg = ui.ThemeAttr("par.text.bg")
return &RowBg{bg}
}
func (w *RowBg) Highlight() { w.Bg = ui.ThemeAttr("par.text.fg") }
func (w *RowBg) UnHighlight() { w.Bg = ui.ThemeAttr("par.text.bg") }