Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed May 22, 2024
1 parent ed879c5 commit 1b0510a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions internal/pkg/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func (t *Table) String() string {
t.maxColWidth = (t.LineLength - uint(headerSpacing)) / uint(numHeaders)

// determine the width for each column (cell in a row)
var colwidths []uint
var colWidths []uint
var rawColWidths []uint
for _, row := range t.rows {
for i, cell := range row.cells {
// resize colwidth array
if i+1 > len(colwidths) {
colwidths = append(colwidths, 0)
if i+1 > len(colWidths) {
colWidths = append(colWidths, 0)
rawColWidths = append(rawColWidths, 0)
}
cellwidth := cell.lineWidth()
Expand All @@ -82,8 +82,8 @@ func (t *Table) String() string {
if t.maxColWidth != 0 && cellwidth > t.maxColWidth {
cellwidth = t.maxColWidth
}
if cellwidth > colwidths[i] {
colwidths[i] = cellwidth
if cellwidth > colWidths[i] {
colWidths[i] = cellwidth
}
}
}
Expand All @@ -92,21 +92,21 @@ func (t *Table) String() string {
// the remaining width to the columns whose colwidth is less than the
// rawColWidths.
if t.LineLength > 0 {
totalWidth := uint(int(t.SeparatorSpaces) * (len(colwidths) - 1))
for _, w := range colwidths {
totalWidth := uint(int(t.SeparatorSpaces) * (len(colWidths) - 1))
for _, w := range colWidths {
totalWidth += w
}

// Determine the remaining width to distribute.
remainingWidth := t.LineLength - totalWidth
if remainingWidth > 0 {
for i, w := range colwidths {
for i, w := range colWidths {
if desiredWidth := rawColWidths[i]; w < desiredWidth {
add := desiredWidth - w
if add > remainingWidth {
add = remainingWidth
}
colwidths[i] += add
colWidths[i] += add
remainingWidth -= add
}
}
Expand All @@ -122,7 +122,7 @@ func (t *Table) String() string {
row.firstColumnFormatter = t.FirstColumnFormatter
}
for i, cell := range row.cells {
cell.width = colwidths[i]
cell.width = colWidths[i]
cell.wrap = t.Wrap
}
lines = append(lines, row.string())
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestTable_MoreThanLineWidth(t *testing.T) {
tbl.AddRow("short", "alongervalue") // 5 + 2 + 12 = 19
tbl.AddRow("alongervalue", "alongervalue") // 12 + 2 + 12 = 26

// Only have two rows and but expect the second to be wrapped.
// Expect both rows to be wrapped.
// header1 header2
// short alongerv
// alue
Expand Down

0 comments on commit 1b0510a

Please sign in to comment.