Skip to content

Commit 3cdbe59

Browse files
committed
table: html row automerge support
1 parent 027182a commit 3cdbe59

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,81 @@ func TestTable_RenderHTML_Sorted(t *testing.T) {
517517
</tfoot>
518518
</table>`)
519519
}
520+
521+
func TestTable_RenderHTML_RowAutoMerge(t *testing.T) {
522+
t.Run("simple", func(t *testing.T) {
523+
rcAutoMerge := RowConfig{AutoMerge: true}
524+
tw := NewWriter()
525+
tw.AppendHeader(Row{"A", "B"})
526+
tw.AppendRow(Row{"Y", "Y"}, rcAutoMerge)
527+
tw.AppendRow(Row{"Y", "N"}, rcAutoMerge)
528+
compareOutput(t, tw.RenderHTML(), `
529+
<table class="go-pretty-table">
530+
<thead>
531+
<tr>
532+
<th>A</th>
533+
<th>B</th>
534+
</tr>
535+
</thead>
536+
<tbody>
537+
<tr>
538+
<td align="center" colspan=2>Y</td>
539+
</tr>
540+
<tr>
541+
<td>Y</td>
542+
<td>N</td>
543+
</tr>
544+
</tbody>
545+
</table>`)
546+
})
547+
t.Run("merged and unmerged entries", func(t *testing.T) {
548+
rcAutoMerge := RowConfig{AutoMerge: true}
549+
tw := NewWriter()
550+
tw.AppendHeader(Row{"A", "B", "C", "D"})
551+
tw.AppendRow(Row{"Y", "Y", "0", "1"}, rcAutoMerge)
552+
tw.AppendRow(Row{"0", "Y", "Y", "1"}, rcAutoMerge)
553+
tw.AppendRow(Row{"0", "1", "Y", "Y"}, rcAutoMerge)
554+
tw.AppendRow(Row{"Y", "Y", "Y", "0"}, rcAutoMerge)
555+
tw.AppendRow(Row{"0", "Y", "Y", "Y"}, rcAutoMerge)
556+
tw.AppendRow(Row{"Y", "Y", "Y", "Y"}, rcAutoMerge)
557+
compareOutput(t, tw.RenderHTML(), `
558+
<table class="go-pretty-table">
559+
<thead>
560+
<tr>
561+
<th>A</th>
562+
<th>B</th>
563+
<th>C</th>
564+
<th>D</th>
565+
</tr>
566+
</thead>
567+
<tbody>
568+
<tr>
569+
<td align="center" colspan=2>Y</td>
570+
<td>0</td>
571+
<td>1</td>
572+
</tr>
573+
<tr>
574+
<td>0</td>
575+
<td align="center" colspan=2>Y</td>
576+
<td>1</td>
577+
</tr>
578+
<tr>
579+
<td>0</td>
580+
<td>1</td>
581+
<td align="center" colspan=2>Y</td>
582+
</tr>
583+
<tr>
584+
<td align="center" colspan=3>Y</td>
585+
<td>0</td>
586+
</tr>
587+
<tr>
588+
<td>0</td>
589+
<td align="center" colspan=3>Y</td>
590+
</tr>
591+
<tr>
592+
<td align="center" colspan=4>Y</td>
593+
</tr>
594+
</tbody>
595+
</table>`)
596+
})
597+
}

0 commit comments

Comments
 (0)