Skip to content
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

Add support for horizontal scrolling of all columns for #188 #195

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
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
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
Loading