Skip to content

Commit

Permalink
table: custom alignment on merged cells; addresses #207 (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Jun 7, 2022
1 parent e314157 commit 1d16aa6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ default: test
all: test bench

tools:
go get github.com/fzipp/gocyclo
go install github.com/fzipp/gocyclo/cmd/[email protected]

bench:
go test -bench=. -benchmem
Expand Down
13 changes: 12 additions & 1 deletion table/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,19 @@ func (c ColumnConfig) getWidthMaxEnforcer() WidthEnforcer {
type RowConfig struct {
// AutoMerge merges cells with similar values and prevents separators from
// being drawn. Caveats:
// * Align is overridden to text.AlignCenter on the merged cell
// * Align is overridden to text.AlignCenter on the merged cell (unless set
// by AutoMergeAlign value below)
// * Does not work in CSV/HTML/Markdown render modes
// * Does not work well with vertical auto-merge (ColumnConfig.AutoMerge)
AutoMerge bool

// Alignment to use on a merge (defaults to text.AlignCenter)
AutoMergeAlign text.Align
}

func (rc RowConfig) getAutoMergeAlign() text.Align {
if rc.AutoMergeAlign == text.AlignDefault {
return text.AlignCenter
}
return rc.AutoMergeAlign
}
5 changes: 3 additions & 2 deletions table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ func (t *Table) renderColumn(out *strings.Builder, row rowStr, colIdx int, maxCo
// if horizontal cell merges are enabled, look ahead and see how many cells
// have the same content and merge them all until a cell with a different
// content is found; override alignment to Center in this case
if t.getRowConfig(hint).AutoMerge && !hint.isSeparatorRow {
rowConfig := t.getRowConfig(hint)
if rowConfig.AutoMerge && !hint.isSeparatorRow {
for idx := colIdx + 1; idx < len(row); idx++ {
if row[colIdx] != row[idx] {
break
}
align = text.AlignCenter
align = rowConfig.getAutoMergeAlign()
maxColumnLength += t.getMaxColumnLengthForMerging(idx)
numColumnsRendered++
}
Expand Down
4 changes: 2 additions & 2 deletions table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func TestTable_Render_AutoMerge_RowsOnly(t *testing.T) {
tw.AppendRow(Row{"1.1.1.1", "Pod 1B", "NS 2", "C 4", "N", "N"}, RowConfig{AutoMerge: true})
tw.AppendRow(Row{"1.1.1.1", "Pod 1B", "NS 2", "C 5", "Y", "N"})
tw.AppendRow(Row{"2.2.2.2", "Pod 2", "NS 3", "C 6", "Y", "Y"}, RowConfig{AutoMerge: true})
tw.AppendRow(Row{"2.2.2.2", "Pod 2", "NS 3", "C 7", "Y", "Y"}, RowConfig{AutoMerge: true})
tw.AppendRow(Row{"2.2.2.2", "Pod 2", "NS 3", "C 7", "Y", "Y"}, RowConfig{AutoMerge: true, AutoMergeAlign: text.AlignRight})
tw.AppendFooter(Row{"", "", "", 7, 5, 3})
tw.SetAutoIndex(true)
tw.SetColumnConfigs([]ColumnConfig{
Expand All @@ -345,7 +345,7 @@ func TestTable_Render_AutoMerge_RowsOnly(t *testing.T) {
├───┼─────────┼────────┼───────────┼───────────┼─────┴─────┤
│ 6 │ 2.2.2.2 │ Pod 2 │ NS 3 │ C 6 │ Y │
├───┼─────────┼────────┼───────────┼───────────┼───────────┤
│ 7 │ 2.2.2.2 │ Pod 2 │ NS 3 │ C 7 │ Y
│ 7 │ 2.2.2.2 │ Pod 2 │ NS 3 │ C 7 │ Y
├───┼─────────┼────────┼───────────┼───────────┼─────┬─────┤
│ │ │ │ │ 7 │ 5 │ 3 │
└───┴─────────┴────────┴───────────┴───────────┴─────┴─────┘`
Expand Down

0 comments on commit 1d16aa6

Please sign in to comment.