Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: html row automerge support #319

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions table/render_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"html"
"strings"

"github.com/jedib0t/go-pretty/v6/text"
)

const (
Expand Down Expand Up @@ -113,9 +115,9 @@ func (t *Table) htmlRenderColumn(out *strings.Builder, colStr string) {
out.WriteString(colStr)
}

func (t *Table) htmlRenderColumnAttributes(out *strings.Builder, colIdx int, hint renderHint) {
func (t *Table) htmlRenderColumnAttributes(out *strings.Builder, colIdx int, hint renderHint, alignOverride text.Align) {
// determine the HTML "align"/"valign" property values
align := t.getAlign(colIdx, hint).HTMLProperty()
align := alignOverride.HTMLProperty()
vAlign := t.getVAlign(colIdx, hint).HTMLProperty()
// determine the HTML "class" property values for the colors
class := t.getColumnColors(colIdx, hint).HTMLProperty()
Expand Down Expand Up @@ -158,11 +160,31 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
t.htmlRenderColumnAutoIndex(out, hint)
}

align := t.getAlign(colIdx, hint)
rowConfig := t.getRowConfig(hint)
extraColumnsRendered := 0
if rowConfig.AutoMerge && !hint.isSeparatorRow {
// get the real row to consider all lines in each column instead of just
// looking at the current "line"
rowUnwrapped := t.getRow(hint.rowNumber-1, hint)
for idx := colIdx + 1; idx < len(rowUnwrapped); idx++ {
if rowUnwrapped[colIdx] != rowUnwrapped[idx] {
break
}
align = rowConfig.getAutoMergeAlign()
extraColumnsRendered++
}
}

colStr, colTagName := t.htmlGetColStrAndTag(row, colIdx, hint)
// write the row
out.WriteString(" <")
out.WriteString(colTagName)
t.htmlRenderColumnAttributes(out, colIdx, hint)
t.htmlRenderColumnAttributes(out, colIdx, hint, align)
if extraColumnsRendered > 0 {
out.WriteString(" colspan=")
out.WriteString(fmt.Sprint(extraColumnsRendered + 1))
}
out.WriteString(">")
if len(colStr) == 0 {
out.WriteString(t.style.HTML.EmptyColumn)
Expand All @@ -172,6 +194,7 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
out.WriteString("</")
out.WriteString(colTagName)
out.WriteString(">\n")
colIdx += extraColumnsRendered
}
out.WriteString(" </tr>\n")
}
Expand Down
85 changes: 85 additions & 0 deletions table/render_html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,88 @@ func TestTable_RenderHTML_Sorted(t *testing.T) {
</tfoot>
</table>`)
}

func TestTable_RenderHTML_RowAutoMerge(t *testing.T) {
t.Run("simple", func(t *testing.T) {
rcAutoMerge := RowConfig{AutoMerge: true}
tw := NewWriter()
tw.AppendHeader(Row{"A", "B"})
tw.AppendRow(Row{"Y", "Y"}, rcAutoMerge)
tw.AppendRow(Row{"Y", "N"}, rcAutoMerge)
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table">
<thead>
<tr>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" colspan=2>Y</td>
</tr>
<tr>
<td>Y</td>
<td>N</td>
</tr>
</tbody>
</table>`)
})
t.Run("merged and unmerged entries", func(t *testing.T) {
rcAutoMerge := RowConfig{AutoMerge: true}
tw := NewWriter()
tw.AppendHeader(Row{"A", "B", "C", "D"})
tw.AppendRow(Row{"Y", "Y", "0", "1"}, rcAutoMerge)
tw.AppendRow(Row{"0", "Y", "Y", "1"}, rcAutoMerge)
tw.AppendRow(Row{"0", "1", "Y", "Y"}, rcAutoMerge)
tw.AppendRow(Row{"Y", "Y", "Y", "0"}, rcAutoMerge)
tw.AppendRow(Row{"0", "Y", "Y", "Y"}, rcAutoMerge)
tw.AppendRow(Row{"Y", "Y", "Y", "Y"}, rcAutoMerge)
tw.AppendRow(Row{"0", "1", "2", "3"}, rcAutoMerge)
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" colspan=2>Y</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td align="center" colspan=2>Y</td>
<td>1</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td align="center" colspan=2>Y</td>
</tr>
<tr>
<td align="center" colspan=3>Y</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td align="center" colspan=3>Y</td>
</tr>
<tr>
<td align="center" colspan=4>Y</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</tbody>
</table>`)
})
}