Skip to content

Commit ee81c6b

Browse files
committed
Fix padding
1 parent d911c65 commit ee81c6b

1 file changed

Lines changed: 19 additions & 49 deletions

File tree

pkg/table/table.go

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ func PrintTableNoPad(data pterm.TableData, hasHeader bool) {
3030
return
3131
}
3232

33-
// Pre-compute max width per column for all but the last column
33+
// Pre-compute max width per column (including last column for proper alignment)
3434
maxColWidths := make([]int, numCols)
3535
for _, row := range data {
3636
for colIdx := 0; colIdx < numCols && colIdx < len(row); colIdx++ {
37-
if colIdx == numCols-1 {
38-
continue
39-
}
4037
for _, line := range strings.Split(row[colIdx], "\n") {
41-
if w := utf8.RuneCountInString(line); w > maxColWidths[colIdx] {
38+
// Strip color codes for accurate width measurement
39+
visibleLine := pterm.RemoveColorFromString(line)
40+
if w := utf8.RuneCountInString(visibleLine); w > maxColWidths[colIdx] {
4241
maxColWidths[colIdx] = w
4342
}
4443
}
@@ -50,67 +49,38 @@ func PrintTableNoPad(data pterm.TableData, hasHeader bool) {
5049
sepStyled := pterm.ThemeDefault.TableSeparatorStyle.Sprint(sep)
5150

5251
renderRow := func(row []string, styleHeader bool) {
53-
// Build first-line-only for non-last columns; last column is full string
54-
firstLineParts := make([]string, 0, numCols)
52+
// Build and pad all columns for proper alignment
53+
parts := make([]string, 0, numCols)
5554
for colIdx := 0; colIdx < numCols; colIdx++ {
5655
var cell string
5756
if colIdx < len(row) {
5857
cell = row[colIdx]
5958
}
6059

61-
if colIdx < numCols-1 {
62-
// Only the first line for non-last columns
63-
lines := strings.Split(cell, "\n")
64-
first := ""
65-
if len(lines) > 0 {
66-
first = lines[0]
67-
}
68-
padCount := maxColWidths[colIdx] - utf8.RuneCountInString(first)
69-
if padCount < 0 {
70-
padCount = 0
71-
}
72-
firstLineParts = append(firstLineParts, first+strings.Repeat(" ", padCount))
73-
} else {
74-
// Last column: render the first line now; remaining lines after
75-
lines := strings.Split(cell, "\n")
76-
if len(lines) > 0 {
77-
firstLineParts = append(firstLineParts, lines[0])
78-
} else {
79-
firstLineParts = append(firstLineParts, "")
80-
}
60+
// Get first line only
61+
lines := strings.Split(cell, "\n")
62+
first := ""
63+
if len(lines) > 0 {
64+
first = lines[0]
8165
}
82-
}
8366

84-
line := strings.Join(firstLineParts[:numCols-1], sepStyled)
85-
if numCols > 1 {
86-
if line != "" {
87-
line += sepStyled
67+
// Pad to column width (measure visible chars, accounting for color codes)
68+
visibleFirst := pterm.RemoveColorFromString(first)
69+
padCount := maxColWidths[colIdx] - utf8.RuneCountInString(visibleFirst)
70+
if padCount < 0 {
71+
padCount = 0
8872
}
89-
line += firstLineParts[numCols-1]
73+
parts = append(parts, first+strings.Repeat(" ", padCount))
9074
}
9175

76+
line := strings.Join(parts, sepStyled)
77+
9278
if styleHeader {
9379
b.WriteString(pterm.ThemeDefault.TableHeaderStyle.Sprint(line))
9480
} else {
9581
b.WriteString(line)
9682
}
9783
b.WriteString("\n")
98-
99-
// Print remaining lines from the last column without alignment padding
100-
if numCols > 0 {
101-
var lastCell string
102-
if len(row) >= numCols {
103-
lastCell = row[numCols-1]
104-
}
105-
lines := strings.Split(lastCell, "\n")
106-
if len(lines) > 1 {
107-
rest := strings.Join(lines[1:], "\n")
108-
if rest != "" {
109-
b.WriteString(rest)
110-
b.WriteString("\n")
111-
}
112-
}
113-
}
11484
}
11585

11686
for idx, row := range data {

0 commit comments

Comments
 (0)