2017-02-26 22:04:24 +00:00
|
|
|
package compact
|
|
|
|
|
|
|
|
import (
|
|
|
|
ui "github.com/gizak/termui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CompactGrid struct {
|
|
|
|
ui.GridBufferer
|
2019-07-05 23:05:21 +00:00
|
|
|
header *CompactHeader
|
|
|
|
cols []CompactCol // reference columns
|
|
|
|
Rows []RowBufferer
|
2017-03-08 07:45:31 +00:00
|
|
|
X, Y int
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
Offset int // starting row offset
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCompactGrid() *CompactGrid {
|
2019-07-05 23:05:21 +00:00
|
|
|
cg := &CompactGrid{header: NewCompactHeader()}
|
2020-01-02 23:02:53 +00:00
|
|
|
cg.rebuildHeader()
|
2019-07-05 23:05:21 +00:00
|
|
|
return cg
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 07:57:26 +00:00
|
|
|
func (cg *CompactGrid) Align() {
|
2017-03-06 00:46:00 +00:00
|
|
|
y := cg.Y
|
2017-07-07 12:43:03 +00:00
|
|
|
|
2018-02-01 16:55:23 +00:00
|
|
|
if cg.Offset >= len(cg.Rows) || cg.Offset < 0 {
|
2017-07-07 12:43:03 +00:00
|
|
|
cg.Offset = 0
|
|
|
|
}
|
|
|
|
|
2017-03-08 11:15:49 +00:00
|
|
|
// update row ypos, width recursively
|
2019-07-05 23:05:21 +00:00
|
|
|
colWidths := cg.calcWidths()
|
2017-03-08 07:45:31 +00:00
|
|
|
for _, r := range cg.pageRows() {
|
2017-03-06 00:46:00 +00:00
|
|
|
r.SetY(y)
|
|
|
|
y += r.GetHeight()
|
2019-07-05 23:05:21 +00:00
|
|
|
r.SetWidths(cg.Width, colWidths)
|
2017-02-26 22:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 23:02:53 +00:00
|
|
|
func (cg *CompactGrid) Clear() {
|
|
|
|
cg.Rows = []RowBufferer{}
|
|
|
|
cg.rebuildHeader()
|
2020-01-02 14:00:55 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 23:05:21 +00:00
|
|
|
func (cg *CompactGrid) GetHeight() int { return len(cg.Rows) + cg.header.Height }
|
2017-03-03 07:57:26 +00:00
|
|
|
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 }
|
2019-07-05 23:05:21 +00:00
|
|
|
func (cg *CompactGrid) MaxRows() int { return ui.TermHeight() - cg.header.Height - cg.Y }
|
|
|
|
|
|
|
|
// calculate and return per-column width
|
|
|
|
func (cg *CompactGrid) calcWidths() []int {
|
|
|
|
var autoCols int
|
|
|
|
width := cg.Width
|
|
|
|
colWidths := make([]int, len(cg.cols))
|
|
|
|
|
|
|
|
for n, w := range cg.cols {
|
|
|
|
colWidths[n] = w.FixedWidth()
|
|
|
|
width -= w.FixedWidth()
|
|
|
|
if w.FixedWidth() == 0 {
|
|
|
|
autoCols++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spacing := colSpacing * len(cg.cols)
|
|
|
|
autoWidth := (width - spacing) / autoCols
|
|
|
|
for n, val := range colWidths {
|
|
|
|
if val == 0 {
|
|
|
|
colWidths[n] = autoWidth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return colWidths
|
|
|
|
}
|
2017-02-26 22:04:24 +00:00
|
|
|
|
2019-07-05 23:05:21 +00:00
|
|
|
func (cg *CompactGrid) pageRows() (rows []RowBufferer) {
|
|
|
|
rows = append(rows, cg.header)
|
2017-03-08 07:45:31 +00:00
|
|
|
rows = append(rows, cg.Rows[cg.Offset:]...)
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
2017-03-03 07:57:26 +00:00
|
|
|
func (cg *CompactGrid) Buffer() ui.Buffer {
|
2017-02-26 22:04:24 +00:00
|
|
|
buf := ui.NewBuffer()
|
2017-03-08 07:45:31 +00:00
|
|
|
for _, r := range cg.pageRows() {
|
2017-02-26 22:04:24 +00:00
|
|
|
buf.Merge(r.Buffer())
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
2017-03-03 07:57:26 +00:00
|
|
|
|
2019-07-05 23:05:21 +00:00
|
|
|
func (cg *CompactGrid) AddRows(rows ...RowBufferer) {
|
2018-10-25 20:12:46 +00:00
|
|
|
cg.Rows = append(cg.Rows, rows...)
|
2017-03-03 07:57:26 +00:00
|
|
|
}
|
2020-01-02 23:02:53 +00:00
|
|
|
|
|
|
|
func (cg *CompactGrid) rebuildHeader() {
|
|
|
|
cg.cols = newRowWidgets()
|
|
|
|
cg.header.clearFieldPars()
|
|
|
|
for _, col := range cg.cols {
|
|
|
|
cg.header.addFieldPar(col.Header())
|
|
|
|
}
|
|
|
|
}
|