Skip to content

Commit

Permalink
Add support for horizontal scrolling of all columns for #188 (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddworken committed Aug 11, 2024
1 parent 8f51433 commit 8f4aff3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,8 @@ func testTui_scroll(t *testing.T) {
out = stripTuiCommandPrefix(t, out)
testutils.CompareGoldens(t, out, "TestTui-RightScrollTwo")

// TODO: Add a test here that shows all columns can be horizontally scrolled

// Assert there are no leaked connections
assertNoLeakedConnections(t)
}
Expand Down
19 changes: 15 additions & 4 deletions client/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,11 @@ func (m *Model) MaxHScroll() int {
maxWidth := 0
index := m.ColIndex(m.hcol)
for _, row := range m.rows {
if len(row) > index {
maxWidth = max(len(row[index]), maxWidth)
for _, value := range row {
maxWidth = max(runewidth.StringWidth(value), maxWidth)
}
}
return max(maxWidth-m.cols[index].Width+1, 0)
return max(maxWidth-m.cols[index].Width+2, 0)
}

// SetWidth sets the width of the viewport of the table.
Expand Down Expand Up @@ -478,6 +478,17 @@ func (m Model) headersView() string {
return lipgloss.JoinHorizontal(lipgloss.Left, s...)
}

func (m *Model) columnNeedsScrolling(columnIdxToCheck int) bool {
for rowIdx := m.start; rowIdx < m.end; rowIdx++ {
for columnIdx, value := range m.rows[rowIdx] {
if columnIdx == columnIdxToCheck && runewidth.StringWidth(value) > m.cols[columnIdx].Width {
return true
}
}
}
return false
}

func (m *Model) renderRow(rowID int) string {
isRowSelected := rowID == m.cursor
var s = make([]string, 0, len(m.cols))
Expand All @@ -491,7 +502,7 @@ func (m *Model) renderRow(rowID int) string {
}

var renderedCell string
if i == m.ColIndex(m.hcol) && m.hcursor > 0 {
if m.columnNeedsScrolling(i) && m.hcursor > 0 {
renderedCell = style.Render(runewidth.Truncate(runewidth.TruncateLeft(value, m.hcursor, "…"), m.cols[i].Width, "…"))
} else {
renderedCell = style.Render(runewidth.Truncate(value, m.cols[i].Width, "…"))
Expand Down

0 comments on commit 8f4aff3

Please sign in to comment.