Skip to content

Commit 33ba34e

Browse files
committed
table: html row automerge support
1 parent 027182a commit 33ba34e

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

table/render_html.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"html"
66
"strings"
7+
8+
"github.com/jedib0t/go-pretty/v6/text"
79
)
810

911
const (
@@ -113,9 +115,9 @@ func (t *Table) htmlRenderColumn(out *strings.Builder, colStr string) {
113115
out.WriteString(colStr)
114116
}
115117

116-
func (t *Table) htmlRenderColumnAttributes(out *strings.Builder, colIdx int, hint renderHint) {
118+
func (t *Table) htmlRenderColumnAttributes(out *strings.Builder, colIdx int, hint renderHint, alignOverride text.Align) {
117119
// determine the HTML "align"/"valign" property values
118-
align := t.getAlign(colIdx, hint).HTMLProperty()
120+
align := alignOverride.HTMLProperty()
119121
vAlign := t.getVAlign(colIdx, hint).HTMLProperty()
120122
// determine the HTML "class" property values for the colors
121123
class := t.getColumnColors(colIdx, hint).HTMLProperty()
@@ -158,11 +160,31 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
158160
t.htmlRenderColumnAutoIndex(out, hint)
159161
}
160162

163+
align := t.getAlign(colIdx, hint)
164+
rowConfig := t.getRowConfig(hint)
165+
extraColumnsRendered := 0
166+
if rowConfig.AutoMerge && !hint.isSeparatorRow {
167+
// get the real row to consider all lines in each column instead of just
168+
// looking at the current "line"
169+
rowUnwrapped := t.getRow(hint.rowNumber-1, hint)
170+
for idx := colIdx + 1; idx < len(rowUnwrapped); idx++ {
171+
if rowUnwrapped[colIdx] != rowUnwrapped[idx] {
172+
break
173+
}
174+
align = rowConfig.getAutoMergeAlign()
175+
extraColumnsRendered++
176+
}
177+
}
178+
161179
colStr, colTagName := t.htmlGetColStrAndTag(row, colIdx, hint)
162180
// write the row
163181
out.WriteString(" <")
164182
out.WriteString(colTagName)
165-
t.htmlRenderColumnAttributes(out, colIdx, hint)
183+
t.htmlRenderColumnAttributes(out, colIdx, hint, align)
184+
if extraColumnsRendered > 0 {
185+
out.WriteString(" colspan=")
186+
out.WriteString(fmt.Sprint(extraColumnsRendered + 1))
187+
}
166188
out.WriteString(">")
167189
if len(colStr) == 0 {
168190
out.WriteString(t.style.HTML.EmptyColumn)
@@ -172,6 +194,7 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
172194
out.WriteString("</")
173195
out.WriteString(colTagName)
174196
out.WriteString(">\n")
197+
colIdx += extraColumnsRendered
175198
}
176199
out.WriteString(" </tr>\n")
177200
}

table/render_html_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,29 @@ func TestTable_RenderHTML_Sorted(t *testing.T) {
517517
</tfoot>
518518
</table>`)
519519
}
520+
521+
func TestTable_RenderHTML_RowAutoMerge(t *testing.T) {
522+
rcAutoMerge := RowConfig{AutoMerge: true}
523+
tw := NewWriter()
524+
tw.AppendHeader(Row{"A", "B"})
525+
tw.AppendRow(Row{"Y", "Y"}, rcAutoMerge)
526+
tw.AppendRow(Row{"Y", "N"}, rcAutoMerge)
527+
compareOutput(t, tw.RenderHTML(), `
528+
<table class="go-pretty-table">
529+
<thead>
530+
<tr>
531+
<th>A</th>
532+
<th>B</th>
533+
</tr>
534+
</thead>
535+
<tbody>
536+
<tr>
537+
<td align="center" colspan=2>Y</td>
538+
</tr>
539+
<tr>
540+
<td>Y</td>
541+
<td>N</td>
542+
</tr>
543+
</tbody>
544+
</table>`)
545+
}

0 commit comments

Comments
 (0)