fix too wide tables when in !tty mode

This commit is contained in:
2026-07-17 10:09:05 +02:00
parent 281499166b
commit aba5985a2b
3 changed files with 26 additions and 5 deletions

View File

@@ -155,12 +155,16 @@ func (table *Table) PrintTSV() error {
for _, entries := range table.rows {
currentWidth := 0
columns := len(entries)
for idx, entry := range entries {
length := visibleLen(entry)
if length+currentWidth > table.maxwidth && table.maxwidth-currentWidth > 1 {
// // text is too wide to be put into one line, wrap it
if length+currentWidth > table.maxwidth &&
table.maxwidth-currentWidth > 1 &&
idx == columns-1 {
// text is too wide to be put into one line, and
// it's the last cell, so wrap it
entry = wrap(table.maxwidth-currentWidth, currentWidth+2, entry)
}
@@ -193,6 +197,10 @@ func (table *Table) PrintTSV() error {
// further lines will be indented. Used within Print() to print large
// cell text.
func wrap(width, indent int, text string) string {
if len(text) <= width {
return text
}
wrapped := ""
line := ""
@@ -267,7 +275,7 @@ func (table *Table) Addheaders(headers ...string) {
case "json", "yaml":
table.Headers[idx] = strings.ReplaceAll(strings.ToLower(header), " ", "_")
default:
table.Headers[idx] = bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-"))
table.Headers[idx] = Bold(strings.ReplaceAll(strings.ToUpper(header), " ", "-"))
}
table.RawHeaders[idx] = header