aboutsummaryrefslogtreecommitdiff
path: root/internal/ui
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-09-15 16:26:23 -0400
committerhistoria <[not public]>2026-09-15 16:26:23 -0400
commitd89504c3df74706b387b8fa829dd291e31ca01c7 (patch)
tree8924164ed1b061cca04aaa79f283d1ab0995486e /internal/ui
parent0b0b25222b3131ada79ff78c28eb8bf07d977deb (diff)
downloadthehouseoficarus-d89504c3df74706b387b8fa829dd291e31ca01c7.tar.gz
fix: help topics (newplayer, title/body files), help aliases, and table width rebalancing
Diffstat (limited to 'internal/ui')
-rw-r--r--internal/ui/table.go107
-rw-r--r--internal/ui/table_test.go91
2 files changed, 180 insertions, 18 deletions
diff --git a/internal/ui/table.go b/internal/ui/table.go
index 5ef2c99..e85e505 100644
--- a/internal/ui/table.go
+++ b/internal/ui/table.go
@@ -38,6 +38,68 @@ type Table struct {
Rows [][]string
}
+// 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
+// rune at a time from the widest until the table fits. Leftover slack is
+// handed back round-robin to columns that lost width so wide layouts still
+// use the full line.
+func rebalanceWidths(colWidths []int, budget int) {
+ original := make([]int, len(colWidths))
+ copy(original, colWidths)
+
+ fair := budget / len(colWidths)
+ if fair < 1 {
+ fair = 1
+ }
+ for i, w := range colWidths {
+ if w > fair {
+ colWidths[i] = fair
+ }
+ }
+
+ sum := func() int {
+ t := 0
+ for _, w := range colWidths {
+ t += w
+ }
+ return t
+ }
+
+ // Tight tables: trim the widest column until we fit (or hit 1).
+ for sum() > budget {
+ widest := 0
+ for i, w := range colWidths {
+ if w > colWidths[widest] {
+ widest = i
+ }
+ }
+ if colWidths[widest] <= 1 {
+ break
+ }
+ colWidths[widest]--
+ }
+
+ // Give unused width back to the columns that were reduced.
+ for {
+ extra := budget - sum()
+ if extra <= 0 {
+ break
+ }
+ progress := false
+ for i := 0; i < len(colWidths) && extra > 0; i++ {
+ if colWidths[i] < original[i] {
+ colWidths[i]++
+ extra--
+ progress = true
+ }
+ }
+ if !progress {
+ break
+ }
+ }
+}
+
func (t *Table) Render(unicode bool, maxWidth int) []string {
g := tableGlyphSet(unicode)
@@ -65,29 +127,38 @@ func (t *Table) Render(unicode bool, maxWidth int) []string {
}
}
+ headerCells := t.Columns
+ 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
- total := overhead
+ budget := maxWidth - overhead
+ if budget < nCols {
+ budget = nCols
+ }
+
+ total := 0
for _, w := range colWidths {
total += w
}
- if total > maxWidth {
- last := nCols - 1
- budget := maxWidth - overhead
- for i := 0; i < last; i++ {
- budget -= colWidths[i]
- }
- if budget < 5 {
- budget = 5
- }
- colWidths[last] = budget
-
- if len(t.Columns) > last {
- t.Columns[last] = color.TruncateVisible(t.Columns[last], budget, unicode)
+ if total > budget {
+ rebalanceWidths(colWidths, budget)
+
+ // Truncate every cell to its (possibly reduced) column width.
+ // Work on copies so rendering never mutates the table.
+ headerCells = make([]string, len(t.Columns))
+ copy(headerCells, t.Columns)
+ for i := range headerCells {
+ headerCells[i] = color.TruncateVisible(headerCells[i], colWidths[i], unicode)
}
+ bodyRows = make([][]string, len(t.Rows))
for i := range t.Rows {
- if len(t.Rows[i]) > last {
- t.Rows[i][last] = color.TruncateVisible(t.Rows[i][last], budget, unicode)
+ bodyRows[i] = make([]string, len(t.Rows[i]))
+ copy(bodyRows[i], t.Rows[i])
+ for j := 0; j < len(bodyRows[i]) && j < nCols; j++ {
+ bodyRows[i][j] = color.TruncateVisible(bodyRows[i][j], colWidths[j], unicode)
}
}
}
@@ -164,11 +235,11 @@ func (t *Table) Render(unicode bool, maxWidth int) []string {
}
if hasCols {
- out = append(out, makeRow(t.Columns))
+ out = append(out, makeRow(headerCells))
out = append(out, makeSep(g.sepLeft, g.sepCross, g.sepRight, g.fillHSep))
}
- for _, row := range t.Rows {
+ for _, row := range bodyRows {
out = append(out, makeRow(row))
}
diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go
new file mode 100644
index 0000000..68b6d32
--- /dev/null
+++ b/internal/ui/table_test.go
@@ -0,0 +1,91 @@
+package ui
+
+import (
+ "strings"
+ "testing"
+
+ "thehouseoficarus/internal/color"
+)
+
+func renderWidths(t *testing.T, tt *Table, unicode bool, maxWidth int) []string {
+ t.Helper()
+ lines := tt.Render(unicode, maxWidth)
+ for _, l := range lines {
+ if vl := color.VisibleLen(l); maxWidth > 0 && vl > maxWidth+1 {
+ t.Errorf("line exceeds maxWidth %d: %d (%q)", maxWidth, vl, l)
+ }
+ }
+ // All border/content lines must be the same visible width so the box
+ // lines up.
+ widths := map[int]bool{}
+ for _, l := range lines {
+ widths[color.VisibleLen(l)] = true
+ }
+ if len(widths) > 1 {
+ var ws []string
+ for w := range widths {
+ ws = append(ws, string(rune('0'+w)))
+ }
+ t.Errorf("inconsistent line widths: %v", strings.Join(ws, ","))
+ }
+ return lines
+}
+
+func TestTableFitsWithinWidth(t *testing.T) {
+ tt := &Table{
+ Title: "Commands",
+ Columns: []string{"Command", "Type", "Description"},
+ Rows: [][]string{
+ {"north / south / east / west / ne / nw / se / sw / up / down", "Active", "Move in a direction"},
+ {"bet", "Active", "Place a casino wager (e.g. bet 100 on banker)"},
+ {"attack / kill", "Active", "Attack a mob"},
+ },
+ }
+ for _, w := range []int{0, 40, 80, 100} {
+ lines := renderWidths(t, tt, true, w)
+ // Description column must never be crushed: with three columns the
+ // description always keeps at least a fair share of the budget.
+ if w == 80 {
+ // lines: border, title, sep, header, sep, rows...
+ if !strings.Contains(lines[5], "Move i") {
+ t.Errorf("description column crushed at width 80: %q", lines[5])
+ }
+ }
+ }
+}
+
+func TestTableNoMutation(t *testing.T) {
+ tt := &Table{
+ Title: "T",
+ Columns: []string{"A", "B"},
+ Rows: [][]string{{"very long cell content here", "x"}},
+ }
+ before := tt.Columns[0]
+ _ = tt.Render(true, 20)
+ if tt.Columns[0] != before {
+ t.Errorf("Render mutated table columns: %q", tt.Columns[0])
+ }
+}
+
+func TestTableASCIIFallback(t *testing.T) {
+ tt := &Table{Title: "T", Columns: []string{"A", "B"}, Rows: [][]string{{"1", "2"}}}
+ lines := renderWidths(t, tt, false, 0)
+ if !strings.Contains(lines[0], "+") {
+ t.Errorf("expected ASCII border, got %q", lines[0])
+ }
+}
+
+func TestTableColoredCellWidth(t *testing.T) {
+ // ANSI-colored cells must be measured and padded by visible length.
+ red := "\x1b[31m"
+ reset := "\x1b[0m"
+ tt := &Table{Columns: []string{"A", "B"}, Rows: [][]string{{red + "hi" + reset, "x"}}}
+ lines := renderWidths(t, tt, true, 0)
+ // lines: top border, header, sep, data row, bottom border
+ if len(lines) < 5 {
+ t.Fatalf("expected 5 lines, got %d: %q", len(lines), lines)
+ }
+ if !strings.Contains(lines[3], "hi") || !strings.Contains(lines[3], "x") {
+ t.Errorf("unexpected row: %q", lines[3])
+ }
+}