Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: custom alignment on merged cells; addresses #207 #208

Merged
merged 1 commit into from Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
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/gocyclo@v0.5.1

bench:
go test -bench=. -benchmem
Expand Down
13 changes: 12 additions & 1 deletion table/config.go
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
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
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