Repository: jedib0t/go-pretty
Pull request #208 (2022-06-07 17:11:11)

table: custom alignment on merged cells; addresses #207
Address the enhancement requested in #207

sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=jedib0t_go-pretty&pullRequest=208)

[![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=BUG)  
[![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=VULNERABILITY)  
[![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=SECURITY_HOTSPOT)  
[![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=jedib0t_go-pretty&pullRequest=208&resolved=false&types=CODE_SMELL)

[![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=jedib0t_go-pretty&pullRequest=208) No Coverage information  
[![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=jedib0t_go-pretty&pullRequest=208&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=jedib0t_go-pretty&pullRequest=208&metric=new_duplicated_lines_density&view=list)



Patch:
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++
 		}


Test patch:
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  │
 └───┴─────────┴────────┴───────────┴───────────┴─────┴─────┘`