Skip to content

Commit

Permalink
table: fix case where text is longer than merged columns
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Nov 5, 2022
1 parent cbfda0b commit f6ba3ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 14 additions & 14 deletions table/render_test.go
Expand Up @@ -558,20 +558,20 @@ func TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom(t *testing
tw.Style().Options.SeparateRows = true

compareOutput(t, tw.Render(), `
┌───┬──────────┬──────────┬──────────┬──────────┬─────────────┬─────────────┬──────────────┬──────────────┐
│ │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │ COLUMN 5 │ COLUMN 6 │ COLUMN 7 │ COLUMN 8
├───┼──────────┼──────────┼──────────┼──────────┼─────────────┴─────────────┼──────────────┴──────────────┤
│ 1 │ 1.1.1.1 │ Pod 1A │ NS 1A │ C 1 │ 4F8F5CB531E3D49A61CF417C 4F8F5CB531E3D49A61CF417C
│ │ │ │ │ │ D133792CCFA501FD8DA53EE3 D133792CCFA501FD8DA53EE3
│ │ │ │ │ │ 68FED20E5FE0248C3A0B64F9 68FED20E5FE0248C3A0B64F9
│ │ │ │ │ │ 8A6533CEE1DA614C3A8DDEC7 8A6533CEE1DA614C3A8DDEC7
│ │ │ │ │ │ 91FF05FEE6D971D57C134832 91FF05FEE6D971D57C134832
│ │ │ │ │ │ 0F4EB42DR 0F4EB42DRR
├───┼──────────┼──────────┼──────────┼──────────┼─────────────────────────────┴─────────────────────────────┤
│ 2 │ 1.1.1.1 │ Pod 1A │ NS 1A │ C 2 │ Y
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────────────────────
│ 3 │ 1.1.1.1 │ Pod 1A │ NS 1A │ C 2 │ Y
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────────────────────┘`)
┌───┬──────────┬──────────┬──────────┬──────────┬─────────────┬─────────────┬──────────────────────────┐
│ │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │ COLUMN 5 │ COLUMN 6 │ COLUMN 7 │ COLUMN 8 │
├───┼──────────┼──────────┼──────────┼──────────┼─────────────┴─────────────┼──────────────────────────┤
│ 1 │ 1.1.1.1 │ Pod 1A │ NS 1A │ C 1 │ 4F8F5CB531E3D49A61CF417C │ 4F8F5CB531E3D49A61CF417C │
│ │ │ │ │ │ D133792CCFA501FD8DA53EE3 │ D133792CCFA501FD8DA53EE3 │
│ │ │ │ │ │ 68FED20E5FE0248C3A0B64F9 │ 68FED20E5FE0248C3A0B64F9 │
│ │ │ │ │ │ 8A6533CEE1DA614C3A8DDEC7 │ 8A6533CEE1DA614C3A8DDEC7 │
│ │ │ │ │ │ 91FF05FEE6D971D57C134832 │ 91FF05FEE6D971D57C134832 │
│ │ │ │ │ │ 0F4EB42DR │ 0F4EB42DRR │
├───┼──────────┼──────────┼──────────┼──────────┼──────────────────────────────────────────────────────┤
│ 2 │ 1.1.1.1 │ Pod 1A │ NS 1A │ C 2 │ Y
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────────────────┤
│ 3 │ 1.1.1.1 │ Pod 1A │ NS 1A │ C 2 │ Y
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────────────────┘`)
})

t.Run("Column 5-7 with equal String, column 8 other", func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions table/table.go
Expand Up @@ -891,6 +891,7 @@ func (t *Table) parseRowForMaxColumnLengths(rows []rowStr, hint renderHint) {
}
return mergedColLength
}
separatorLength := text.RuneWidthWithoutEscSequences(t.style.Box.MiddleSeparator)

for rowIdx, row := range rows {
hint.rowNumber = rowIdx + 1
Expand All @@ -906,6 +907,7 @@ func (t *Table) parseRowForMaxColumnLengths(rows []rowStr, hint renderHint) {
if longestLineLen > mergedColumnsLength {
if mergedColumnsLength > 0 {
numMergedColumns := len(mergedColumnsForCol) + 1
longestLineLen -= (numMergedColumns - 1) * separatorLength
maxLengthSplitAcrossColumns := longestLineLen / numMergedColumns
if maxLengthSplitAcrossColumns > t.maxColumnLengths[colIdx] {
t.maxColumnLengths[colIdx] = maxLengthSplitAcrossColumns
Expand Down

0 comments on commit f6ba3ff

Please sign in to comment.