Skip to content

fix(paginator): clamp current page when item count shrinks - #1078

Open
Alifarid0011 wants to merge 1 commit into
charmbracelet:mainfrom
Alifarid0011:fix/paginator-page-after-shrink
Open

Alifarid0011 wants to merge 1 commit into
charmbracelet:mainfrom
Alifarid0011:fix/paginator-page-after-shrink

Conversation

@Alifarid0011

Copy link
Copy Markdown

What changed

Keep the current page within the new page count when SetTotalPages recalculates pagination for a smaller, positive number of items.

For example, start with six items and two items per page, navigate to page index 2, then replace the data with one item and call SetTotalPages(1). Previously the page stayed at 2, GetSliceBounds(1) returned (4, 1), and ItemsOnPage(1) returned -3. Slicing with those bounds, as shown in the method's documentation, panicked.

The current page now moves to the new last page. In the example it becomes 0, the bounds are (0, 1), and forward navigation stops there. Valid pages are preserved when the item count grows or stays unchanged. The existing no-op behavior for zero and negative item counts is unchanged.

The fix adds one clamp in SetTotalPages and documents that behavior; no dependencies or public signatures change.

Validation

  • New regression tests fail on unchanged main (e04077d) and pass with the fix.
  • Eight table cases cover shrinking to full/partial pages, a still-valid page, unchanged/growing counts, and zero/negative inputs. An additional regression exercises slice bounds, actual slicing, item counts, and forward navigation after shrinking.
  • go test -race -count=1 -covermode=atomic -coverprofile=coverage.out ./... passes across the complete repository. SetTotalPages has 100% statement coverage (overall repository coverage: 56.0%).
  • go test -shuffle=on ./... passes; the new paginator tests also pass across 100 repetitions.
  • go build ./... and go vet ./... pass.
  • golangci-lint run ./... and golangci-lint fmt --diff pass with the CI-pinned v2.13.1 and the existing repository configuration.

Local validation: Go 1.26.8, linux/amd64. Other platforms and Go versions are left to the CI matrix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant