Repository navigation
Closed
Labels
Milestone
Description
Hi,
Thanks for this awesome library, I love it!
I run into a cosmetic issue though, I would like to be able to merge the cells in the footer like I want.
I can do it when the merged columns are first, but not when they're last.
Here is an example:
┌─────────────┬──────────┬───────────────┬─────────┐
│ DEPARTMENT │ DIVISION │ TEAM │ LEAD │
├─────────────┼──────────┼───────────────┼─────────┤
│ Engineering │ Backend │ API Team │ Alice │
│ │ ├───────────────┼─────────┤
│ │ │ Database Team │ Bob │
│ ├──────────┼───────────────┼─────────┤
│ │ Frontend │ UI Team │ Charlie │
├─────────────┼──────────┼───────────────┼─────────┤
│ Marketing │ Digital │ SEO Team │ Dave │
│ │ ├───────────────┼─────────┤
│ │ │ Content Team │ Eve │
├─────────────┴──────────┴───────────────┼─────────┤
│ Total Teams │ 5 │
└────────────────────────────────────────┴─────────┘
the first 2 "empty" columns are getting merged with the first non-empty one table.Footer([]string{"", "", "Total Teams", "5"})
in that example:
┌─────────────┬──────────┬───────────────┬─────────┐
│ DEPARTMENT │ DIVISION │ TEAM │ LEAD │
├─────────────┼──────────┼───────────────┼─────────┤
│ Engineering │ Backend │ API Team │ Alice │
│ │ ├───────────────┼─────────┤
│ │ │ Database Team │ Bob │
│ ├──────────┼───────────────┼─────────┤
│ │ Frontend │ UI Team │ Charlie │
├─────────────┼──────────┼───────────────┼─────────┤
│ Marketing │ Digital │ SEO Team │ Dave │
│ │ ├───────────────┼─────────┤
│ │ │ Content Team │ Eve │
├─────────────┼──────────┼───────────────┼─────────┤
│ Total Teams │ 5 │ │ │
└─────────────┴──────────┴───────────────┴─────────┘
the 3 last columns are not merged at all table.Footer([]string{"Total Teams", "5"})
here is the complete code to reproduce:
package main
import (
"os"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/renderer"
"github.com/olekukonko/tablewriter/tw"
)
func main() {
data := [][]string{
{"Engineering", "Backend", "API Team", "Alice"},
{"Engineering", "Backend", "Database Team", "Bob"},
{"Engineering", "Frontend", "UI Team", "Charlie"},
{"Marketing", "Digital", "SEO Team", "Dave"},
{"Marketing", "Digital", "Content Team", "Eve"},
}
table := tablewriter.NewTable(os.Stdout,
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
Settings: tw.Settings{Separators: tw.Separators{BetweenRows: tw.On}},
})),
tablewriter.WithConfig(tablewriter.Config{
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}},
Row: tw.CellConfig{
Formatting: tw.CellFormatting{MergeMode: tw.MergeHierarchical},
Alignment: tw.CellAlignment{Global: tw.AlignLeft},
},
Footer: tw.CellConfig{
Formatting: tw.CellFormatting{
MergeMode: tw.MergeHorizontal,
},
},
}),
)
table.Header([]string{"Department", "Division", "Team", "Lead"})
table.Bulk(data)
table.Footer([]string{"Total Teams", "5"})
table.Render()
}
Activity
EppO commentedon Jul 17, 2025
I found a workaround repeating the value I want to merge in the other columns like so:
but in that case, it doesn't follow the specified alignment
AlignLeft:If I remove the MergeHorizontal setting, I can see the repeated values have the correct alignment, there seems to be a bug when MergeHorizontal is involved in the alignment calculation
EppO commentedon Jul 17, 2025
Running in debug, I see these interesting logs:
It states
Original/default align was: leftbut it overrides itApplying AlignRight HMergeas the following line saysalign=right.Then it applies the left padding but I assume it's because it's in right alignment mode.
Why does the horizontal merge switches the alignment to right only?
update #285
olekukonko commentedon Jul 18, 2025
Hi @EppO, thanks for pointing this out.
As you can see, the message states:
Applying AlignRight HMerge/TOTAL override for Footer col 1. Original/default align was: left.This was part of the initial behavior defined for merged cells in the footer. However, I've decided to update the logic so that the override is now only applied when
align == tw.AlignNone.With this update
output
EppO commentedon Jul 18, 2025
That's exactly what I wanted to achieve! Thank you so much for the quick fix!
One very last thing I wanted to discuss to make that library perfect: vertical alignment. To make it look like
I don't think that's possible today.
Would like me to open another issue to track as feature request?
olekukonko commentedon Jul 18, 2025
I initially explored vertical alignment but discovered it was expensive. To properly center text vertically in merged cells, the renderer must know the total visual height of the span before rendering the first line requiring multiple passes and heavy use of buffered iterators.
To optimize this, I implemented olekukonko/it, which enabled lookahead ("peeking") without consuming the iterator. While functional, Introduced significant complexity hence I deemed the trade-off unjustified .
Logical vs. Visual Height Mismatch
applyVerticalMerges|applyHierarchicalMergesknows a cell spans N logical rows but not their visual height due toword-wrapping┴,┬,├,┤,┼) during merges is nontrivial.This risks breaking changes, especially in the renderer’s public functions & interface, major reimplementation, and careful versioning to avoid something like #270 happening again. Hence i believe, simplicity and performance take precedence over edge-case alignment. If demand grows, we could revisit this.
EppO commentedon Jul 18, 2025
That's completely fair. Thank you for taking the time to provide such clear and thorough explanation. Really appreciate it!
I will close that comment when 1.0.9 is out
add test for #285
EppO commentedon Aug 25, 2025
@olekukonko: I just tried 1.0.9 and I found it doesn't work when using
renderer.NewColorized. It works well withrenderer.NewBlueprintthough.here are the logs in debug when using
renderer.NewColorizedYou can see there is still some overriding in place
Applying AlignRight override for Footer HMerge/TOTAL pattern at col 1. Original/default align was: leftTo compare with
renderer.NewBlueprintrenderer.NewBlueprint
EppO commentedon Aug 25, 2025
I can open a new bug request if you need
olekukonko commentedon Aug 25, 2025
@EppO thank you for the pull request.
olekukonko commentedon Mar 24, 2026
@EppO it’s been a while… I’ve been working on a project and wanted to ask if you would help review ?