aboutsummaryrefslogtreecommitdiff
path: root/internal/ui/table.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/ui/table.go')
-rw-r--r--internal/ui/table.go30
1 files changed, 29 insertions, 1 deletions
diff --git a/internal/ui/table.go b/internal/ui/table.go
index 0ac41ad..5061417 100644
--- a/internal/ui/table.go
+++ b/internal/ui/table.go
@@ -38,7 +38,7 @@ type Table struct {
Rows [][]string
}
-func (t *Table) Render(unicode bool) []string {
+func (t *Table) Render(unicode bool, maxWidth int) []string {
g := tableGlyphSet(unicode)
nCols := len(t.Columns)
@@ -65,6 +65,34 @@ func (t *Table) Render(unicode bool) []string {
}
}
+ if maxWidth > 0 {
+ overhead := 3*nCols + 1
+ total := overhead
+ 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)
+ }
+ for i := range t.Rows {
+ if len(t.Rows[i]) > last {
+ t.Rows[i][last] = color.TruncateVisible(t.Rows[i][last], budget)
+ }
+ }
+ }
+ }
+
makeSep := func(left, cross, right rune, fill rune) string {
var b strings.Builder
b.WriteRune(left)