ctop/cwidgets/compact/header.go

71 lines
1.2 KiB
Go
Raw Normal View History

package compact
import (
ui "github.com/gizak/termui"
)
type CompactHeader struct {
X, Y int
Width int
Height int
2017-03-06 00:15:32 +00:00
pars []*ui.Par
}
func NewCompactHeader() *CompactHeader {
2017-03-12 09:58:56 +00:00
fields := []string{"", "NAME", "CID", "CPU", "MEM", "NET RX/TX", "IO R/W", "PIDS"}
ch := &CompactHeader{}
ch.Height = 2
for _, f := range fields {
ch.addFieldPar(f)
}
return ch
}
func (ch *CompactHeader) GetHeight() int {
return ch.Height
}
func (ch *CompactHeader) SetWidth(w int) {
x := ch.X
2017-03-12 09:58:56 +00:00
autoWidth := calcWidth(w)
for n, col := range ch.pars {
2017-03-12 09:58:56 +00:00
// set column to static width
if colWidths[n] != 0 {
col.SetX(x)
2017-03-12 09:58:56 +00:00
col.SetWidth(colWidths[n])
x += colWidths[n]
continue
}
col.SetX(x)
col.SetWidth(autoWidth)
x += autoWidth + colSpacing
}
ch.Width = w
}
func (ch *CompactHeader) SetX(x int) {
ch.X = x
}
func (ch *CompactHeader) SetY(y int) {
for _, p := range ch.pars {
p.SetY(y)
}
ch.Y = y
}
func (ch *CompactHeader) Buffer() ui.Buffer {
buf := ui.NewBuffer()
for _, p := range ch.pars {
buf.Merge(p.Buffer())
}
return buf
}
2017-03-06 00:15:32 +00:00
func (ch *CompactHeader) addFieldPar(s string) {
2017-03-06 00:15:32 +00:00
p := ui.NewPar(s)
p.Height = ch.Height
2017-03-06 00:15:32 +00:00
p.Border = false
ch.pars = append(ch.pars, p)
2017-03-06 00:15:32 +00:00
}