Skip to content

Commit eb654fb

Browse files
authored
table: auto-merge column separators without header/footer; fixes #215 (#216)
1 parent ba4b835 commit eb654fb

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed

table/render.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,19 @@ func (t *Table) renderRows(out *strings.Builder, rows []rowStr, hint renderHint)
320320
}
321321

322322
func (t *Table) renderRowsBorderBottom(out *strings.Builder) {
323-
t.renderRowSeparator(out, renderHint{isBorderBottom: true, isFooterRow: true})
323+
if len(t.rowsFooter) > 0 {
324+
t.renderRowSeparator(out, renderHint{isBorderBottom: true, isFooterRow: true, rowNumber: len(t.rowsFooter)})
325+
} else {
326+
t.renderRowSeparator(out, renderHint{isBorderBottom: true, isFooterRow: false, rowNumber: len(t.rows)})
327+
}
324328
}
325329

326330
func (t *Table) renderRowsBorderTop(out *strings.Builder) {
327-
t.renderRowSeparator(out, renderHint{isBorderTop: true, isHeaderRow: true})
331+
if len(t.rowsHeader) > 0 || t.autoIndex {
332+
t.renderRowSeparator(out, renderHint{isBorderTop: true, isHeaderRow: true, rowNumber: 0})
333+
} else {
334+
t.renderRowSeparator(out, renderHint{isBorderTop: true, isHeaderRow: false, rowNumber: 0})
335+
}
328336
}
329337

330338
func (t *Table) renderRowsFooter(out *strings.Builder) {
@@ -340,17 +348,16 @@ func (t *Table) renderRowsFooter(out *strings.Builder) {
340348

341349
func (t *Table) renderRowsHeader(out *strings.Builder) {
342350
if len(t.rowsHeader) > 0 || t.autoIndex {
351+
hintSeparator := renderHint{isHeaderRow: true, isLastRow: true, isSeparatorRow: true}
352+
343353
if len(t.rowsHeader) > 0 {
344354
t.renderRows(out, t.rowsHeader, renderHint{isHeaderRow: true})
355+
hintSeparator.rowNumber = len(t.rowsHeader)
345356
} else if t.autoIndex {
346357
t.renderRow(out, t.getAutoIndexColumnIDs(), renderHint{isAutoIndexRow: true, isHeaderRow: true})
358+
hintSeparator.rowNumber = 1
347359
}
348-
t.renderRowSeparator(out, renderHint{
349-
isHeaderRow: true,
350-
isLastRow: true,
351-
isSeparatorRow: true,
352-
rowNumber: len(t.rowsHeader),
353-
})
360+
t.renderRowSeparator(out, hintSeparator)
354361
}
355362
}
356363

table/render_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,75 @@ func TestTable_Render_AutoMerge_RowsOnly(t *testing.T) {
347347
└───┴─────────┴────────┴───────────┴───────────┴─────┴─────┘`)
348348
}
349349

350+
func TestTable_Render_AutoMerge_NoHeaderFooter(t *testing.T) {
351+
tw := NewWriter()
352+
tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 1", "Y", "Y"}, RowConfig{AutoMerge: true})
353+
tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "N"})
354+
tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1B", "C 3", "N", "N"})
355+
tw.AppendRow(Row{"1.1.1.1", "Pod 1B", "NS 2", "C 4", "N", "N"}, RowConfig{AutoMerge: true})
356+
tw.AppendRow(Row{"1.1.1.1", "Pod 1B", "NS 2", "C 5", "Y", "N"})
357+
tw.AppendRow(Row{"2.2.2.2", "Pod 2", "NS 3", "C 6", "Y", "Y"}, RowConfig{AutoMerge: true})
358+
tw.AppendRow(Row{"2.2.2.2", "Pod 2", "NS 3", "C 7", "Y", "Y"}, RowConfig{AutoMerge: true, AutoMergeAlign: text.AlignRight})
359+
tw.SetColumnConfigs([]ColumnConfig{
360+
{Number: 5, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
361+
{Number: 6, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
362+
})
363+
tw.SetStyle(StyleLight)
364+
tw.Style().Options.SeparateRows = true
365+
366+
compareOutput(t, tw.Render(), `┌─────────┬────────┬───────┬─────┬───────┐
367+
│ 1.1.1.1 │ Pod 1A │ NS 1A │ C 1 │ Y │
368+
├─────────┼────────┼───────┼─────┼───┬───┤
369+
│ 1.1.1.1 │ Pod 1A │ NS 1A │ C 2 │ Y │ N │
370+
├─────────┼────────┼───────┼─────┼───┼───┤
371+
│ 1.1.1.1 │ Pod 1A │ NS 1B │ C 3 │ N │ N │
372+
├─────────┼────────┼───────┼─────┼───┴───┤
373+
│ 1.1.1.1 │ Pod 1B │ NS 2 │ C 4 │ N │
374+
├─────────┼────────┼───────┼─────┼───┬───┤
375+
│ 1.1.1.1 │ Pod 1B │ NS 2 │ C 5 │ Y │ N │
376+
├─────────┼────────┼───────┼─────┼───┴───┤
377+
│ 2.2.2.2 │ Pod 2 │ NS 3 │ C 6 │ Y │
378+
├─────────┼────────┼───────┼─────┼───────┤
379+
│ 2.2.2.2 │ Pod 2 │ NS 3 │ C 7 │ Y │
380+
└─────────┴────────┴───────┴─────┴───────┘`)
381+
}
382+
383+
func TestTable_Render_AutoMerge_NoHeaderFooter_AutoIndex(t *testing.T) {
384+
tw := NewWriter()
385+
tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 1", "Y", "Y"}, RowConfig{AutoMerge: true})
386+
tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "N"})
387+
tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1B", "C 3", "N", "N"})
388+
tw.AppendRow(Row{"1.1.1.1", "Pod 1B", "NS 2", "C 4", "N", "N"}, RowConfig{AutoMerge: true})
389+
tw.AppendRow(Row{"1.1.1.1", "Pod 1B", "NS 2", "C 5", "Y", "N"})
390+
tw.AppendRow(Row{"2.2.2.2", "Pod 2", "NS 3", "C 6", "Y", "Y"}, RowConfig{AutoMerge: true})
391+
tw.AppendRow(Row{"2.2.2.2", "Pod 2", "NS 3", "C 7", "Y", "Y"}, RowConfig{AutoMerge: true, AutoMergeAlign: text.AlignRight})
392+
tw.SetAutoIndex(true)
393+
tw.SetColumnConfigs([]ColumnConfig{
394+
{Number: 5, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
395+
{Number: 6, Align: text.AlignCenter, AlignHeader: text.AlignCenter},
396+
})
397+
tw.SetStyle(StyleLight)
398+
tw.Style().Options.SeparateRows = true
399+
400+
compareOutput(t, tw.Render(), `┌───┬─────────┬────────┬───────┬─────┬───┬───┐
401+
│ │ A │ B │ C │ D │ E │ F │
402+
├───┼─────────┼────────┼───────┼─────┼───┴───┤
403+
│ 1 │ 1.1.1.1 │ Pod 1A │ NS 1A │ C 1 │ Y │
404+
├───┼─────────┼────────┼───────┼─────┼───┬───┤
405+
│ 2 │ 1.1.1.1 │ Pod 1A │ NS 1A │ C 2 │ Y │ N │
406+
├───┼─────────┼────────┼───────┼─────┼───┼───┤
407+
│ 3 │ 1.1.1.1 │ Pod 1A │ NS 1B │ C 3 │ N │ N │
408+
├───┼─────────┼────────┼───────┼─────┼───┴───┤
409+
│ 4 │ 1.1.1.1 │ Pod 1B │ NS 2 │ C 4 │ N │
410+
├───┼─────────┼────────┼───────┼─────┼───┬───┤
411+
│ 5 │ 1.1.1.1 │ Pod 1B │ NS 2 │ C 5 │ Y │ N │
412+
├───┼─────────┼────────┼───────┼─────┼───┴───┤
413+
│ 6 │ 2.2.2.2 │ Pod 2 │ NS 3 │ C 6 │ Y │
414+
├───┼─────────┼────────┼───────┼─────┼───────┤
415+
│ 7 │ 2.2.2.2 │ Pod 2 │ NS 3 │ C 7 │ Y │
416+
└───┴─────────┴────────┴───────┴─────┴───────┘`)
417+
}
418+
350419
func TestTable_Render_AutoMerge_WithHiddenRows(t *testing.T) {
351420
tw := NewWriter()
352421
tw.AppendHeader(Row{"Node IP", "Pods", "Namespace", "Container", "RCE\nEXE", "RCE\nRUN"})

0 commit comments

Comments
 (0)