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