ctop/cwidgets/compact/grid.go

71 lines
1.3 KiB
Go
Raw Normal View History

package compact
import (
ui "github.com/gizak/termui"
)
type CompactGrid struct {
ui.GridBufferer
2017-03-03 07:57:26 +00:00
Rows []*Compact // rows to render
X, Y int
Width int
Height int
header *CompactHeader
cursorID string
}
func NewCompactGrid() *CompactGrid {
return &CompactGrid{
header: NewCompactHeader(),
}
}
2017-03-03 07:57:26 +00:00
func (cg *CompactGrid) Align() {
2017-03-06 00:15:32 +00:00
// update header y pos
if cg.header.Y != cg.Y {
cg.header.SetY(cg.Y)
}
// update row y pos recursively
2017-03-03 07:57:26 +00:00
y := cg.Y + 1
for _, r := range cg.Rows {
2017-03-06 00:15:32 +00:00
if r.Y != y {
r.SetY(y)
}
y += r.Height
}
2017-03-06 00:15:32 +00:00
// update header width
if cg.header.Width != cg.Width {
cg.header.SetWidth(cg.Width)
}
// update row width recursively
2017-03-03 07:57:26 +00:00
for _, r := range cg.Rows {
2017-03-06 00:15:32 +00:00
if r.Width != cg.Width {
r.SetWidth(cg.Width)
}
}
}
2017-03-03 07:57:26 +00:00
func (cg *CompactGrid) Clear() { cg.Rows = []*Compact{} }
func (cg *CompactGrid) GetHeight() int { return len(cg.Rows) }
func (cg *CompactGrid) SetX(x int) { cg.X = x }
func (cg *CompactGrid) SetY(y int) { cg.Y = y }
func (cg *CompactGrid) SetWidth(w int) { cg.Width = w }
2017-03-03 07:57:26 +00:00
func (cg *CompactGrid) Buffer() ui.Buffer {
buf := ui.NewBuffer()
2017-03-03 07:57:26 +00:00
buf.Merge(cg.header.Buffer())
for _, r := range cg.Rows {
buf.Merge(r.Buffer())
}
return buf
}
2017-03-03 07:57:26 +00:00
func (cg *CompactGrid) AddRows(rows ...*Compact) {
for _, r := range rows {
cg.Rows = append(cg.Rows, r)
}
}