aboutsummaryrefslogtreecommitdiff
path: root/internal/ui
diff options
context:
space:
mode:
Diffstat (limited to 'internal/ui')
-rw-r--r--internal/ui/table.go22
1 files changed, 15 insertions, 7 deletions
diff --git a/internal/ui/table.go b/internal/ui/table.go
index 437c445..a1800ba 100644
--- a/internal/ui/table.go
+++ b/internal/ui/table.go
@@ -45,6 +45,17 @@ func FitWidths(colWidths []int, budget int) {
rebalanceWidths(colWidths, budget)
}
+// TableBudget returns the total cell width available to nCols columns in a
+// table rendered at maxWidth. Callers that pre-wrap cells must use this so
+// their shrink target matches Render's padding/separator arithmetic.
+func TableBudget(nCols, maxWidth int) int {
+ budget := maxWidth - (3*nCols + 1)
+ if budget < nCols {
+ budget = nCols
+ }
+ return budget
+}
+
// rebalanceWidths shrinks column widths in place so their sum fits budget.
// Rather than crushing only the last column, every column wider than the
// fair share (budget/nCols) is capped first, then columns are trimmed one
@@ -52,6 +63,9 @@ func FitWidths(colWidths []int, budget int) {
// handed back round-robin to columns that lost width so wide layouts still
// use the full line.
func rebalanceWidths(colWidths []int, budget int) {
+ if len(colWidths) == 0 {
+ return
+ }
original := make([]int, len(colWidths))
copy(original, colWidths)
@@ -138,13 +152,7 @@ func (t *Table) Render(unicode bool, maxWidth int) []string {
bodyRows := t.Rows
if maxWidth > 0 {
- // Cell padding: one space on each side of every cell, plus one
- // separator between columns.
- overhead := 3*nCols + 1
- budget := maxWidth - overhead
- if budget < nCols {
- budget = nCols
- }
+ budget := TableBudget(nCols, maxWidth)
total := 0
for _, w := range colWidths {