#1053·bubbles

table (v2): moving up from the last row scrolls the whole window instead of the cursor

Author: cabbagekobeCreated Sep 20, 2026Updated Sep 20, 2026

Describe the bug

In charm.land/bubbles/v2 (v2.2.1 and current main), with a table taller than its viewport, pressing k / up once while the cursor is on the last row scrolls the whole visible window up by one row and leaves the cursor on the bottom line. Only the cursor should move (to the second-to-last line). From the second k onward the cursor moves normally within the window.

This does not happen in bubbles v1 (v0.21.x).

Setup

  • OS: macOS 26 (Darwin 25.6.0)
  • Shell: zsh
  • Terminal Emulator: not relevant (reproduces in a unit test on Model.View())
  • Locale: en_US.UTF-8
  • bubbles v2.2.1, bubbletea v2.0.9, lipgloss v2.0.6, Go 1.25

To Reproduce

  1. Create a table with 30 rows and a viewport of 5 body rows (WithHeight(6) with the default single-line header).
  2. GotoBottom() – the view shows rows 25..29 with the cursor on row 29.
  3. MoveUp(1) (what k / up dispatches to).
  4. The view now shows rows 24..28 with the cursor on row 28: the window moved, not the cursor.

Source Code

go
func TestMoveUpFromBottomKeepsViewport(t *testing.T) {
	rows := make([]table.Row, 30)
	for i := range rows {
		rows[i] = table.Row{fmt.Sprintf("r%02d", i)}
	}
	m := table.New(
		table.WithColumns([]table.Column{{Title: "c", Width: 4}}),
		table.WithRows(rows),
		table.WithWidth(10),
		table.WithHeight(6), // 5 body rows below the header
	)
	m.GotoBottom()
	before := m.View()
	m.MoveUp(1)
	after := m.View()

	firstRow := func(view string) string {
		return ansi.Strip(strings.Split(view, "\n")[1])
	}
	if got, want := firstRow(after), firstRow(before); got != want {
		t.Errorf("window scrolled: first visible row %q, want %q", got, want)
	}
}

Output on v2.2.1:

window scrolled: first visible row " r24      ", want " r25      "

Expected behavior

The visible window stays on rows 25..29 and the cursor moves to row 28, the same as v1 and the same as what MoveDown does at the top of the table.

Additional context

Cause: MoveUp computes the new y-offset and calls viewport.SetYOffset() before UpdateViewport() re-renders the row window (table/table.go, MoveUp). In v2 viewport.SetYOffset clamps to the current content length, and at that point the content still ends at the previous cursor. From the last row that content is only one row taller than the viewport, so the new offset is clamped back to 1, and after the re-render the window is shifted by one row.

  • v1 assigned viewport.YOffset directly (no clamp), so the bug did not surface there.
  • MoveDown already calls UpdateViewport() first, which is why j is unaffected.

Calling UpdateViewport() before SetYOffset() in MoveUp (mirroring MoveDown) fixes it with the offset computation unchanged; the existing table tests still pass. I have this change ready and can open a PR referencing this issue.