fix: out-of-range page wrap returns a per_page slice; validate cache length
get_page() out-of-range/negative wrap reset to page 0 but returned pages[0] (a single item) even with per_page>1, mis-shaping the page downstream; it now reroutes through get_page(0) so the normal slice logic applies. cache shorter than max_pages now raises ValueError at construction instead of an IndexError when the cache button is clicked on a later page. Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
parent
4d52ef7f50
commit
6dae35001e
@ -9,13 +9,13 @@ buttons) behind previous / jump / next navigation, with an optional cache button
|
|||||||
`requirements.txt`:
|
`requirements.txt`:
|
||||||
|
|
||||||
```
|
```
|
||||||
dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.0
|
dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.1
|
||||||
```
|
```
|
||||||
|
|
||||||
Direct:
|
Direct:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install "dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.0"
|
pip install "dpy_paginator @ git+ssh://git@git.rethinkstudios.io/rethink-public/dpy_paginator.git@v0.1.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
Requires `discord.py` (pulled transitively).
|
Requires `discord.py` (pulled transitively).
|
||||||
@ -116,7 +116,8 @@ await ButtonPaginator(pages, cache=cache, cache_sleep=1.0).start(ctx)
|
|||||||
```
|
```
|
||||||
|
|
||||||
Omit `cache` or pass `None`/`[]` and the button never appears. When set, `cache`
|
Omit `cache` or pass `None`/`[]` and the button never appears. When set, `cache`
|
||||||
must have one entry per rendered page.
|
must have one entry per rendered page — a shorter `cache` raises `ValueError` at
|
||||||
|
construction rather than failing with an `IndexError` mid-navigation.
|
||||||
|
|
||||||
## Constructor options
|
## Constructor options
|
||||||
|
|
||||||
|
|||||||
@ -176,6 +176,11 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|||||||
|
|
||||||
total_pages, left_over = divmod(len(self.pages), self.per_page)
|
total_pages, left_over = divmod(len(self.pages), self.per_page)
|
||||||
self.max_pages: int = total_pages + (1 if left_over else 0)
|
self.max_pages: int = total_pages + (1 if left_over else 0)
|
||||||
|
if cache is not None and len(cache) < self.max_pages:
|
||||||
|
raise ValueError(
|
||||||
|
f"cache has {len(cache)} entries but there are {self.max_pages} pages; "
|
||||||
|
"cache needs one entry per page"
|
||||||
|
)
|
||||||
self._page_kwargs: Dict[str, Any] = self._fresh_kwargs()
|
self._page_kwargs: Dict[str, Any] = self._fresh_kwargs()
|
||||||
|
|
||||||
def _fresh_kwargs(self) -> Dict[str, Any]:
|
def _fresh_kwargs(self) -> Dict[str, Any]:
|
||||||
@ -204,7 +209,7 @@ class ButtonPaginator(Generic[PageT_co], discord.ui.View):
|
|||||||
"""return the content for a page index, wrapping out-of-range to 0"""
|
"""return the content for a page index, wrapping out-of-range to 0"""
|
||||||
if page_number < 0 or page_number >= self.max_pages:
|
if page_number < 0 or page_number >= self.max_pages:
|
||||||
self.current_page = 0
|
self.current_page = 0
|
||||||
return self.pages[self.current_page]
|
return self.get_page(0)
|
||||||
if self.per_page == 1:
|
if self.per_page == 1:
|
||||||
return self.pages[page_number]
|
return self.pages[page_number]
|
||||||
base = page_number * self.per_page
|
base = page_number * self.per_page
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user