Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (t *Table) Render() string {
average := make([]int, t.columnsCount)
widths := make([]int, t.columnsCount)
count := make([]int, t.columnsCount)
minimum := make([]int, t.columnsCount)
for _, row := range t.rows {
for x, cell := range row.cells {
l := cell.Len()
Expand All @@ -98,6 +99,15 @@ func (t *Table) Render() string {
average[x] = average[x] / count[x]
}
}
// table headers will dictate the absolute min width
for x := range minimum {
if t.hasHeader {
minimum[x] = t.rows[0].cells[x].Len()
} else {
minimum[x] = 1
}
}

variance := make([]int, t.columnsCount)
for _, row := range t.rows {
for x, cell := range row.cells {
Expand Down Expand Up @@ -128,6 +138,9 @@ func (t *Table) Render() string {
selectedWidth = average[x] + variance[x]*3
}
}
if selectedWidth < minimum[x] {
selectedWidth = minimum[x]
}
res += separator
res += cell.Pad(selectedWidth)
separator = " "
Expand Down