Skip to content

Commit

Permalink
fix: textfmt/table display
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Kungla <marko@mkungla.dev>
  • Loading branch information
mkungla committed Jun 2, 2024
1 parent b6e4158 commit 784a063
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/strings/textfmt/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package textfmt

import (
"fmt"
"strings"
"unicode/utf8"
)
Expand Down Expand Up @@ -46,14 +47,15 @@ func (t *Table) String() string {
maxColWidth, tableWidth := t.calculateMaxColumnWidths()

if t.Title != "" {
title := fmt.Sprint(t.Title)
b.WriteString(t.buildBorder('┌', '─', '┐', maxColWidth))
suffixlen := tableWidth - utf8.RuneCountInString(t.Title)
suffixlen := tableWidth - utf8.RuneCountInString(title) - 4

suffix := ""
if suffixlen > 0 {
suffix = strings.Repeat(" ", suffixlen)
}
b.WriteString("│ " + t.Title + suffix + " │\n")
b.WriteString("│ " + title + suffix + " │\n")
b.WriteString(t.buildBorder('├', '┬', '┤', maxColWidth))
} else {
b.WriteString(t.buildBorder('┌', '┬', '┐', maxColWidth))
Expand Down Expand Up @@ -90,11 +92,11 @@ func (t *Table) calculateMaxColumnWidths() (cw []int, total int) {
}
}

totalWidthOfCols := 0
totalWidthOfCols := 1
for _, w := range maxColWidth {
totalWidthOfCols += w
totalWidthOfCols += w + 1
}
totalWidthOfCols++

return maxColWidth, totalWidthOfCols
}

Expand Down

0 comments on commit 784a063

Please sign in to comment.