Rapptz / discord-ext-menus

MIT License
234 stars 87 forks source link

Allow menu pages to loop on themselves #14

Open Drapersniper opened 4 years ago

Drapersniper commented 4 years ago

Current implementation of MenuPages.show_checked_page() is the following:

    async def show_checked_page(self, page_number):
        max_pages = self._source.get_max_pages()
        try:
            if max_pages is None:
                # If it doesn't give maximum pages, it cannot be checked
                await self.show_page(page_number)
            elif max_pages > page_number >= 0:
                await self.show_page(page_number)
        except IndexError:
            # An error happened that can be handled, so ignore it.
            pass

Before making a PR I wanted to check if the following implementation is something you would like to see implemented Danny.

    async def show_checked_page(self, page_number: int) -> None:
        max_pages = self._source.get_max_pages()
        try:
            if max_pages is None:
                # If it doesn't give maximum pages, it cannot be checked
                await self.show_page(page_number)
            elif page_number >= max_pages:
                await self.show_page(0)
            elif page_number < 0:
                await self.show_page(max_pages - 1)
            elif max_pages > page_number >= 0:
                await self.show_page(page_number)
        except IndexError:
            # An error happened that can be handled, so ignore it.
            pass

This implementation would allow the menu to go to the last page when pressing back on Page 0, and go to first page when pressing Next on Page -1

Rapptz commented 4 years ago

This is by design at the moment since I support asynchronous iterators which can generally only go forward until they can't anymore.