-
Notifications
You must be signed in to change notification settings - Fork 13
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
Fix table spacing and omitEmpty
option
#3139
Changes from all commits
f46564e
72f0998
fdeda2c
f4d6eab
5c2b63a
449463d
1cc0a89
b6472f9
118a135
402b685
2326e0d
dc58a4c
8393fe3
d3bd42d
b07d536
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ type Table struct { | |
rows []row | ||
|
||
HideHeaders bool | ||
Vertical bool | ||
} | ||
|
||
func New(headers []string) *Table { | ||
|
@@ -103,6 +104,13 @@ func (t *Table) calculateWidth(maxTableWidth int) ([]int, int) { | |
colWidthsCombined += colWidths[n] | ||
} | ||
|
||
// Capture the width of the vertical header before we equalize the column widths. | ||
// We must respect this width when rescaling the columns. | ||
var verticalHeaderWidth int | ||
if len(colWidths) > 0 && t.Vertical { | ||
verticalHeaderWidth = colWidths[0] | ||
} | ||
|
||
if colWidthsCombined >= maxTableWidth { | ||
// Equalize widths by 20% of average width. | ||
// This is to prevent columns that are much larger than others | ||
|
@@ -115,7 +123,7 @@ func (t *Table) calculateWidth(maxTableWidth int) ([]int, int) { | |
tableWidth = mathutils.MinInt(tableWidth, maxTableWidth) | ||
|
||
// Now scale back the row sizes according to the max width | ||
rescaleColumns(colWidths, tableWidth) | ||
rescaleColumns(colWidths, tableWidth, t.Vertical, verticalHeaderWidth) | ||
logging.Debug("Table column widths: %v, total: %d", colWidths, tableWidth) | ||
|
||
return colWidths, tableWidth | ||
|
@@ -138,18 +146,31 @@ func equalizeWidths(colWidths []int, percentage int) { | |
} | ||
} | ||
|
||
func rescaleColumns(colWidths []int, targetTotal int) { | ||
func rescaleColumns(colWidths []int, targetTotal int, vertical bool, verticalHeaderWidth int) { | ||
total := float64(mathutils.Total(colWidths...)) | ||
multiplier := float64(targetTotal) / total | ||
|
||
originalWidths := make([]int, len(colWidths)) | ||
for n := range colWidths { | ||
originalWidths[n] = colWidths[n] | ||
colWidths[n] = int(float64(colWidths[n]) * multiplier) | ||
} | ||
|
||
// Account for floats that got rounded | ||
if len(colWidths) > 0 { | ||
colWidths[len(colWidths)-1] += targetTotal - mathutils.Total(colWidths...) | ||
} | ||
|
||
// If vertical, respect the header width | ||
// verticalHeaderWidth is the width of the header column before we equalized the column widths. | ||
// We compare the current width of the header column with the original width and adjust the other columns accordingly. | ||
if vertical && len(colWidths) > 0 && colWidths[0] < verticalHeaderWidth { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As in my other comment, |
||
diff := verticalHeaderWidth - colWidths[0] | ||
colWidths[0] += diff | ||
for i := 1; i < len(colWidths); i++ { | ||
colWidths[i] -= diff / (len(colWidths) - 1) | ||
} | ||
} | ||
} | ||
|
||
func renderRow(providedColumns []string, colWidths []int) string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only used on
rescaleColumns
and is based on inputs that are also passed to that function. So I suggest moving this logic insiderescaleColumns
.But also; what is the rationale here? Comment explaining this would be good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment, but left this here. There is a call further down to an
equalizeWidths
function that changes the column widths. Before that call the value of the first column is the original header width, which we want to respect when rescaling. I think it makes sense to preserve this, equalize all of the columns, and then ensure the header width is respected when rescaling, but I'm open to move this somewhere else or take a different approach.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I think you missed the first part of my comment though:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry, I didn't explain well exactly what's happening here.
The call to
equalizeWidths
changes the actual values in thecolWidths
slice. So we have to grab the column width of the first entry in the slice at this point as this is the max width of the headers column. SoverticalHeaderWidth
can be a different value fromcolWidths[0]
after theequalizeWidths
call.