Skip to content

Commit da20de3

Browse files
wpjuniorclaude
andcommitted
Add TableWriterExpandRows option for multiline cell expansion
When enabled, cells containing newlines are expanded into multiple output rows instead of being truncated or having newlines replaced with spaces. This allows tabular data with multiline content to be displayed in a more readable format. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 7c452a7 commit da20de3

2 files changed

Lines changed: 417 additions & 12 deletions

File tree

render.go

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ type Table struct {
4545
LineSeparator bool
4646
rows rowSlice
4747

48-
TableWriterTruncate bool
49-
TableWriterPadding int
48+
TableWriterTruncate bool
49+
TableWriterPadding int
50+
TableWriterExpandRows bool
5051
}
5152

5253
type Row []string
@@ -224,22 +225,53 @@ var tableWriterReplacer = strings.NewReplacer(
224225
"\r", " ",
225226
)
226227

228+
// expandRow expands a row with cells containing line breaks into multiple rows.
229+
// Example: ["A", "X\nY", "1"] becomes [["A", "X", "1"], ["", "Y", ""]].
230+
func expandRow(row Row) [][]string {
231+
cols := len(row)
232+
lines := make([][]string, cols)
233+
maxLines := 0
234+
235+
for i, cell := range row {
236+
lines[i] = strings.Split(strings.TrimSpace(cell), "\n")
237+
if len(lines[i]) > maxLines {
238+
maxLines = len(lines[i])
239+
}
240+
}
241+
242+
result := make([][]string, maxLines)
243+
for i := range maxLines {
244+
result[i] = make([]string, cols)
245+
for j, col := range lines {
246+
if i < len(col) {
247+
result[i][j] = col[i]
248+
}
249+
}
250+
}
251+
return result
252+
}
253+
227254
func (t *Table) renderUsingTabWriterLike() string {
228255
padding := strings.Repeat(" ", t.TableWriterPadding)
229256

230257
// Process rows and calculate column widths
231-
processedRows := make([][]string, len(t.rows))
232-
for i, row := range t.rows {
233-
processedRows[i] = make([]string, len(row))
234-
for j, col := range row {
235-
if idx := strings.IndexAny(col, "\f\n\r"); idx >= 0 {
236-
if TableConfig.TabWriterTruncate || t.TableWriterTruncate {
237-
col = col[:idx] + " ..."
238-
} else {
239-
col = tableWriterReplacer.Replace(col)
258+
var processedRows [][]string
259+
for _, row := range t.rows {
260+
if t.TableWriterExpandRows {
261+
processedRows = append(processedRows, expandRow(row)...)
262+
} else {
263+
newRow := make([]string, len(row))
264+
for j, col := range row {
265+
if idx := strings.IndexAny(col, "\f\n\r"); idx >= 0 {
266+
if TableConfig.TabWriterTruncate || t.TableWriterTruncate {
267+
col = col[:idx] + " ..."
268+
} else {
269+
col = tableWriterReplacer.Replace(col)
270+
}
240271
}
272+
newRow[j] = col
241273
}
242-
processedRows[i][j] = col
274+
processedRows = append(processedRows, newRow)
243275
}
244276
}
245277

0 commit comments

Comments
 (0)