diff options
author | Robin Jarry <robin@jarry.cc> | 2023-01-31 14:23:03 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-02-20 14:48:42 +0100 |
commit | 34db5942bd7b642107002b75de9d5d5c7fe90e4c (patch) | |
tree | c4da0bf758df1722a1f928dc01d493964985f5c5 /lib/ui | |
parent | 191876182a91659bbeab0c7b5c4ef61ebe636f76 (diff) | |
download | aerc-34db5942bd7b642107002b75de9d5d5c7fe90e4c.tar.gz |
ui/table: do not require width at construction
The width is only required when rendering the table in Draw. Remove the
redundant width attribute.
Fixes: 012be0192c88 ("ui: add reusable table widget")
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'lib/ui')
-rw-r--r-- | lib/ui/table.go | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/lib/ui/table.go b/lib/ui/table.go index 89b59c23..0cd6e706 100644 --- a/lib/ui/table.go +++ b/lib/ui/table.go @@ -14,7 +14,6 @@ import ( type Table struct { Columns []Column Rows []Row - Width int Height int // Optional callback that allows customizing the default drawing routine // of table rows. If true is returned, the default routine is skipped. @@ -41,7 +40,7 @@ type Row struct { } func NewTable( - width, height int, + height int, columnDefs []*config.ColumnDef, separator string, customDraw func(*Table, int, *Context) bool, getRowStyle func(*Table, int) tcell.Style, @@ -68,7 +67,6 @@ func NewTable( } return Table{ Columns: columns, - Width: width, Height: height, CustomDraw: customDraw, GetRowStyle: getRowStyle, @@ -91,7 +89,7 @@ func (t *Table) AddRow(cells []string, priv interface{}) bool { return len(t.Rows) >= t.Height } -func (t *Table) computeWidths() { +func (t *Table) computeWidths(width int) { contentMaxWidths := make([]int, len(t.Columns)) if t.autoFitWidths { for _, row := range t.Rows { @@ -104,7 +102,7 @@ func (t *Table) computeWidths() { } } - nonFixed := t.Width + nonFixed := width autoWidthCount := 0 for c := range t.Columns { col := &t.Columns[c] @@ -121,7 +119,7 @@ func (t *Table) computeWidths() { col.Width = 0 autoWidthCount += 1 case col.Def.Flags.Has(config.WIDTH_FRACTION): - col.Width = int(math.Round(float64(t.Width) * col.Def.Width)) + col.Width = int(math.Round(float64(width) * col.Def.Width)) } nonFixed -= col.Width } @@ -135,7 +133,7 @@ func (t *Table) computeWidths() { } offset := 0 - remain := t.Width + remain := width for c := range t.Columns { col := &t.Columns[c] if col.Def.Flags.Has(config.WIDTH_AUTO) && autoWidth > 0 { @@ -193,7 +191,7 @@ func (col *Column) alignCell(cell string) string { func (t *Table) Draw(ctx *Context) { if !t.widthsComputed { - t.computeWidths() + t.computeWidths(ctx.Width()) t.widthsComputed = true } for r, row := range t.Rows { |