Skip to content

Commit

Permalink
fix(table): improve resizing with headers
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Jan 24, 2025
1 parent e73aa1c commit 142b09d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func (t *Table) String() string {
// time.
for i, cell := range t.headers {
t.widths[i] = max(t.widths[i], lipgloss.Width(t.style(HeaderRow, i).Render(cell)))
// TODO if header is greater than calculated width, wrap it at an acceptable %.

Check failure on line 246 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

table/table.go:246: Line contains TODO/BUG/FIXME: "TODO if header is greater than calculate..." (godox)

Check failure on line 246 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

table/table.go:246: Line contains TODO/BUG/FIXME: "TODO if header is greater than calculate..." (godox)

Check failure on line 246 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

\table\table.go:246: Line contains TODO/BUG/FIXME: "TODO if header is greater than calculate..." (godox)

Check failure on line 246 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

table/table.go:246: Line contains TODO/BUG/FIXME: "TODO if header is greater than calculate..." (godox)

Check failure on line 246 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

table/table.go:246: Line contains TODO/BUG/FIXME: "TODO if header is greater than calculate..." (godox)

Check failure on line 246 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

\table\table.go:246: Line contains TODO/BUG/FIXME: "TODO if header is greater than calculate..." (godox)
t.heights[0] = max(t.heights[0], lipgloss.Height(t.style(HeaderRow, i).Render(cell)))
}

Expand All @@ -253,7 +254,6 @@ func (t *Table) String() string {
rendered := t.style(r, i).Render(cell)
t.heights[r+btoi(hasHeaders)] = max(t.heights[r+btoi(hasHeaders)], lipgloss.Height(rendered))
// TODO not sure about this part... We want to know how wide the headers are so we can compare the resulting wrapped width to the header width

Check failure on line 256 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

table/table.go:256: Line contains TODO/BUG/FIXME: "TODO not sure about this part... We want..." (godox)

Check failure on line 256 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

table/table.go:256: Line contains TODO/BUG/FIXME: "TODO not sure about this part... We want..." (godox)

Check failure on line 256 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

\table\table.go:256: Line contains TODO/BUG/FIXME: "TODO not sure about this part... We want..." (godox)

Check failure on line 256 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

table/table.go:256: Line contains TODO/BUG/FIXME: "TODO not sure about this part... We want..." (godox)

Check failure on line 256 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

table/table.go:256: Line contains TODO/BUG/FIXME: "TODO not sure about this part... We want..." (godox)

Check failure on line 256 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

\table\table.go:256: Line contains TODO/BUG/FIXME: "TODO not sure about this part... We want..." (godox)

t.widths[i] = max(t.widths[i], lipgloss.Width(rendered))
}
}
Expand Down Expand Up @@ -335,29 +335,31 @@ func (t *Table) String() string {
columnMedians[c] = median(trimmedWidth)
}

// TODO if header is greater than calculated width, wrap it at an acceptable %.

// Find the biggest differences between the median and the column width.
// Shrink the columns based on the largest difference.
differences := make([]int, len(t.widths))
for i := range t.widths {
differences[i] = t.widths[i] - columnMedians[i]
}

// Go through all of the differences to see where we can possibly cut
// TODO Go through all of the differences to see where we can possibly cut

Check failure on line 345 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

table/table.go:345: Line contains TODO/BUG/FIXME: "TODO Go through all of the differences t..." (godox)

Check failure on line 345 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

table/table.go:345: Line contains TODO/BUG/FIXME: "TODO Go through all of the differences t..." (godox)

Check failure on line 345 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

\table\table.go:345: Line contains TODO/BUG/FIXME: "TODO Go through all of the differences t..." (godox)

Check failure on line 345 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

table/table.go:345: Line contains TODO/BUG/FIXME: "TODO Go through all of the differences t..." (godox)

Check failure on line 345 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

table/table.go:345: Line contains TODO/BUG/FIXME: "TODO Go through all of the differences t..." (godox)

Check failure on line 345 in table/table.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

\table\table.go:345: Line contains TODO/BUG/FIXME: "TODO Go through all of the differences t..." (godox)
// content. Don't cut content if the difference is smaller than the
// header. In case of a very long header, we should wrap that... but
// when?
//
// In this loop, each column is shrunk if the median width is bigger
// than the header (preserve headers where possible) or if the column
// width is bigger than the minimum width. The minimum width would be 1/5 if
// there are 5 columns.
for width > t.width {
// check difference between width and median AND width and evenColumnWidth??
index, _ := largest(differences)
// is there a difference and is that value greater than the size of the column header?
// If all columns and rows are equally sized, exit the loop.
if differences[index] < 1 {
break
}

// Ignore columns that are already an acceptable size.
if columnMedians[index] < headerWidths[index] && headerWidths[index] <= t.evenColumnWidth() {
if t.widths[index] <= t.evenColumnWidth() || columnMedians[index] < headerWidths[index] && headerWidths[index] <= t.evenColumnWidth() {
differences[index] = 0
continue
}
Expand Down Expand Up @@ -435,7 +437,8 @@ func (t *Table) String() string {
Render(sb.String())
}

// returns the even widths of columns only
// evenColumnWidth returns the column widths, excluding borders, if the total
// table width is evenly distributed.
func (t *Table) evenColumnWidth() int {
numCols := len(t.headers)
colWidth := t.width - btoi(t.borderLeft) - btoi(t.borderRight)
Expand Down

0 comments on commit 142b09d

Please sign in to comment.