Skip to content

Commit

Permalink
chore: fold get_footer functionality into update_states
Browse files Browse the repository at this point in the history
  • Loading branch information
onerandomusername committed Nov 19, 2021
1 parent 691e31b commit 256011e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 43 deletions.
27 changes: 12 additions & 15 deletions modmail/utils/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,6 @@ async def interaction_check(self, interaction: Interaction) -> bool:
)
return False

def get_footer(self) -> Optional[str]:
"""Returns the footer text."""
if self.embed is None:
self.content = self._pages[self.index]
return None
self.embed.description = self._pages[self.index]
page_indicator = f"Page {self.index+1}/{len(self._pages)}"
footer_txt = (
f"{self.footer_text} ({page_indicator})" if self.footer_text is not None else page_indicator
)
return footer_txt

def update_states(self) -> None:
"""
Disable specific components depending on paginator page and length.
Expand All @@ -251,9 +239,18 @@ def update_states(self) -> None:
if the paginator is on the last page, the jump last/move forward buttons will be disabled.
"""
# update the footer
text = self.get_footer()
if self.embed:
self.embed.set_footer(text=text)
if self.embed is None:
self.content = self._pages[self.index]
else:
self.embed.description = self._pages[self.index]
page_indicator = f"Page {self.index+1}/{len(self._pages)}"
self.embed.set_footer(
text=(
f"{self.footer_text} ({page_indicator})"
if self.footer_text is not None
else page_indicator
)
)

# determine if the jump buttons should be enabled
more_than_two_pages = len(self._pages) > 2
Expand Down
28 changes: 0 additions & 28 deletions tests/modmail/utils/test_pagination.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List, Union

import pytest

from modmail.utils.pagination import ButtonPaginator
Expand All @@ -11,29 +9,3 @@ async def test_paginator_init() -> None:
content = ["content"]
paginator = ButtonPaginator(content, prefix="", suffix="", linesep="")
assert paginator.pages == content


@pytest.mark.asyncio
@pytest.mark.parametrize(
"content, footer_text",
[
(["5"], "Snap, crackle, pop"),
(["Earthly"], "world"),
("There are no plugins installed.", None),
],
)
async def test_paginator_footer(content: Union[str, List[str]], footer_text: str) -> None:
"""Test the paginator footer matches what is passed."""
pag = ButtonPaginator(content, footer_text=footer_text)
print("index:", pag.index)
print("page len: ", len(pag.pages))
assert footer_text == pag.footer_text
if isinstance(content, str):
content = [content]

if footer_text is not None:
assert pag.get_footer().endswith(f"{len(content)})")
assert pag.get_footer().startswith(footer_text)

else:
assert pag.get_footer().endswith(f"{len(content)}")

0 comments on commit 256011e

Please sign in to comment.