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 | |
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>
-rw-r--r-- | lib/ui/table.go | 14 | ||||
-rw-r--r-- | widgets/msglist.go | 8 |
2 files changed, 10 insertions, 12 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 { diff --git a/widgets/msglist.go b/widgets/msglist.go index 4b2bf721..ab8b89fa 100644 --- a/widgets/msglist.go +++ b/widgets/msglist.go @@ -98,7 +98,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) { params, _ := row.Priv.(messageRowParams) if params.needsHeaders { needsHeaders = append(needsHeaders, params.uid) - ml.spinner.Draw(ctx.Subcontext(0, r, t.Width, 1)) + ml.spinner.Draw(ctx.Subcontext(0, r, c.Width(), 1)) return true } return false @@ -119,7 +119,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) { } table := ui.NewTable( - textWidth, ml.height, + ml.height, uiConfig.IndexColumns, uiConfig.ColumnSeparator, customDraw, @@ -185,10 +185,10 @@ func (ml *MessageList) Draw(ctx *ui.Context) { } } - table.Draw(ctx) + table.Draw(ctx.Subcontext(0, 0, textWidth, ctx.Height())) if ml.NeedScrollbar() { - scrollbarCtx := ctx.Subcontext(ctx.Width()-1, 0, 1, ctx.Height()) + scrollbarCtx := ctx.Subcontext(textWidth, 0, 1, ctx.Height()) ml.drawScrollbar(scrollbarCtx) } |