From 11d52ca426e5c594f7c6c10766935a7f30a83225 Mon Sep 17 00:00:00 2001 From: s0ders <39492740+s0ders@users.noreply.github.com> Date: Tue, 22 Apr 2025 13:41:52 +0200 Subject: [PATCH] fix(table): preventing cursor from being out-of-bounds. The following brings: - Preventing table's cursor from being out-of-bound when the table's rows shrink. ref: #782 --- table/table.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/table/table.go b/table/table.go index ed29d8c..4dd1203 100644 --- a/table/table.go +++ b/table/table.go @@ -4,12 +4,13 @@ package table import ( "strings" - "github.com/charmbracelet/bubbles/help" - "github.com/charmbracelet/bubbles/key" - "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "github.com/mattn/go-runewidth" + + "github.com/charmbracelet/bubbles/help" + "github.com/charmbracelet/bubbles/key" + "github.com/charmbracelet/bubbles/viewport" ) // Model defines a state for the table widget. @@ -306,6 +307,11 @@ func (m Model) Columns() []Column { // SetRows sets a new rows state. func (m *Model) SetRows(r []Row) { m.rows = r + + if m.cursor > len(m.rows)-1 { + m.cursor = len(m.rows) - 1 + } + m.UpdateViewport() }