From 1d26ca109b438b33ea87aa1c6c0f351ca3ed6382 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sat, 23 Mar 2024 18:00:36 -0700 Subject: [PATCH] Add support for horizontal scrolling of all columns for #188 --- client/client_test.go | 2 ++ client/table/table.go | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/client/client_test.go b/client/client_test.go index 5a43eb2b..d58d4710 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -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) } diff --git a/client/table/table.go b/client/table/table.go index 5d3579f7..955e247d 100644 --- a/client/table/table.go +++ b/client/table/table.go @@ -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. @@ -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)) @@ -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, "…"))