From 1d16aa66b729270e9d03ef965344ece5a610ccb2 Mon Sep 17 00:00:00 2001 From: Naveen Mahalingam Date: Tue, 7 Jun 2022 10:12:43 -0700 Subject: [PATCH] table: custom alignment on merged cells; addresses #207 (#208) --- Makefile | 2 +- table/config.go | 13 ++++++++++++- table/render.go | 5 +++-- table/render_test.go | 4 ++-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 4ef2107..6efb0ff 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ default: test all: test bench tools: - go get github.com/fzipp/gocyclo + go install github.com/fzipp/gocyclo/cmd/gocyclo@v0.5.1 bench: go test -bench=. -benchmem diff --git a/table/config.go b/table/config.go index 6eb7ce7..23d54f3 100644 --- a/table/config.go +++ b/table/config.go @@ -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 } diff --git a/table/render.go b/table/render.go index dadfa53..379969e 100644 --- a/table/render.go +++ b/table/render.go @@ -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++ } diff --git a/table/render_test.go b/table/render_test.go index b8a967d..2de590a 100644 --- a/table/render_test.go +++ b/table/render_test.go @@ -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{ @@ -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 │ └───┴─────────┴────────┴───────────┴───────────┴─────┴─────┘`