Skip to content

Commit

Permalink
table: auto-merge: compute column separator length correctly (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Feb 7, 2022
1 parent c65c9bc commit afdd730
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
10 changes: 4 additions & 6 deletions table/render.go
Expand Up @@ -50,7 +50,7 @@ func (t *Table) Render() string {
}

func (t *Table) renderColumn(out *strings.Builder, row rowStr, colIdx int, maxColumnLength int, hint renderHint) int {
numColumnsRenderer := 1
numColumnsRendered := 1

// when working on the first column, and autoIndex is true, insert a new
// column with the row number on it.
Expand Down Expand Up @@ -84,10 +84,8 @@ func (t *Table) renderColumn(out *strings.Builder, row rowStr, colIdx int, maxCo
break
}
align = text.AlignCenter
maxColumnLength += t.maxColumnLengths[idx] +
text.RuneCount(t.style.Box.PaddingRight+t.style.Box.PaddingLeft) +
1
numColumnsRenderer++
maxColumnLength += t.getMaxColumnLengthForMerging(idx)
numColumnsRendered++
}
}
colStr = align.Apply(colStr, maxColumnLength)
Expand All @@ -99,7 +97,7 @@ func (t *Table) renderColumn(out *strings.Builder, row rowStr, colIdx int, maxCo

t.renderColumnColorized(out, colIdx, colStr, hint)

return colIdx + numColumnsRenderer
return colIdx + numColumnsRendered
}

func (t *Table) renderColumnAutoIndex(out *strings.Builder, hint renderHint) {
Expand Down
5 changes: 2 additions & 3 deletions table/render_html_test.go
Expand Up @@ -2,7 +2,6 @@ package table

import (
"fmt"
"os"
"testing"

"github.com/jedib0t/go-pretty/v6/text"
Expand Down Expand Up @@ -99,7 +98,7 @@ func TestTable_RenderHTML_AutoIndex(t *testing.T) {
}
tw.AppendFooter(row)
}
tw.SetOutputMirror(os.Stdout)
tw.SetOutputMirror(nil)
tw.SetAutoIndex(true)
tw.SetStyle(StyleLight)

Expand Down Expand Up @@ -251,7 +250,7 @@ func TestTable_RenderHTML_CustomStyle(t *testing.T) {
EscapeText: false,
Newline: "<!-- newline -->",
}
tw.SetOutputMirror(os.Stdout)
tw.SetOutputMirror(nil)

expectedOut := `<table class="game-of-thrones">
<thead>
Expand Down
3 changes: 2 additions & 1 deletion table/render_test.go
Expand Up @@ -210,7 +210,8 @@ func TestTable_Render_AutoMerge_Complex(t *testing.T) {
│ │ │ ├─────┴─────┼─────┤
│ │ │ │ 5 │ │
└───┴──────────────────────────────┴───────────┴───────────┴─────┘`
assert.Equal(t, expectedOut, tw.Render())
out := tw.Render()
assert.Equal(t, expectedOut, out)
}

func TestTable_Render_AutoMerge_Complex2(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions table/table.go
Expand Up @@ -548,6 +548,15 @@ func (t *Table) getFormat(hint renderHint) text.Format {
return t.style.Format.Row
}

func (t *Table) getMaxColumnLengthForMerging(colIdx int) int {
maxColumnLength := t.maxColumnLengths[colIdx]
maxColumnLength += text.RuneCount(t.style.Box.PaddingRight + t.style.Box.PaddingLeft)
if t.style.Options.SeparateColumns {
maxColumnLength += text.RuneCount(t.style.Box.EmptySeparator)

This comment has been minimized.

Copy link
@lzzzzl

lzzzzl Apr 25, 2022

Is there add more than one separator?

This comment has been minimized.

Copy link
@jedib0t

jedib0t May 8, 2022

Author Owner

When we merge two columns, there is only one separator.

}
return maxColumnLength
}

func (t *Table) getRow(rowIdx int, hint renderHint) rowStr {
switch {
case hint.isHeaderRow:
Expand Down

0 comments on commit afdd730

Please sign in to comment.