diff options
Diffstat (limited to 'internal/ui')
| -rw-r--r-- | internal/ui/table.go | 8 | ||||
| -rw-r--r-- | internal/ui/table_test.go | 11 |
2 files changed, 17 insertions, 2 deletions
diff --git a/internal/ui/table.go b/internal/ui/table.go index e85e505..8a056fb 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -219,8 +219,12 @@ func (t *Table) Render(unicode bool, maxWidth int) []string { var tr strings.Builder tr.WriteRune(g.side) tr.WriteString(" ") - tr.WriteString(t.Title) - padding := innerWidth - 2 - color.VisibleLen(t.Title) + title := t.Title + if max := innerWidth - 2; max > 0 && color.VisibleLen(title) > max { + title = color.TruncateVisible(title, max, unicode) + } + tr.WriteString(title) + padding := innerWidth - 2 - color.VisibleLen(title) if padding < 0 { padding = 0 } diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go index 68b6d32..170af5c 100644 --- a/internal/ui/table_test.go +++ b/internal/ui/table_test.go @@ -54,6 +54,17 @@ func TestTableFitsWithinWidth(t *testing.T) { } } +func TestTableLongTitleStaysWithinBorder(t *testing.T) { + // A title longer than the column-derived interior must be truncated, not + // allowed to overhang the border. + tt := &Table{ + Title: "What would you like to smelt?", + Columns: []string{"", "Bronze bar"}, + Rows: [][]string{{"1", "Bronze bar"}}, + } + renderWidths(t, tt, true, 0) +} + func TestTableNoMutation(t *testing.T) { tt := &Table{ Title: "T", |
