Skip to content

Commit

Permalink
Dynamically decide number of lines of each chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
WitherredAway committed Nov 1, 2024
1 parent b3d02b7 commit bca15a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cogs/poketwo_administration.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from helpers.converters import ActivityArgs
from helpers.outline.decorators import with_typing
from helpers.poketwo import format_pokemon_details
from helpers.utils import FetchUserConverter
from helpers.utils import FetchUserConverter, as_line_chunks_by_len

REFUND_CHANNEL_ID = 973239955784614008

Expand Down Expand Up @@ -554,7 +554,6 @@ async def activity(
)

table = tabulate([cols, *data])
NL = "\n"
msgs = [
dedent(
f"""
Expand All @@ -566,7 +565,10 @@ async def activity(
> **Formula**: `(bot-logs * {bnet} + tickets * {tnet}) * 100`
> **Max Amount**: {max_amount}"""
),
*[f"""{"`"*3}py\n{NL.join(lines)}\n{"`"*3}""" for lines in discord.utils.as_chunks(table.split("\n"), 25)],
*[
f"""{"`"*3}py\n{chunk}\n{"`"*3}"""
for chunk in as_line_chunks_by_len(table, constants.CONTENT_CHAR_LIMIT - 10)
], # - 10 because of the code block
]

if len("\n".join(msgs)) < constants.CONTENT_CHAR_LIMIT:
Expand Down
18 changes: 18 additions & 0 deletions helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,21 @@ def get_substring_matches(substring: str, strings: List[str]) -> List[str]:

matches.sort(key=lambda c: c.index(substring))
return matches


def as_line_chunks_by_len(text: str, char_limit: int):
"""Split a string into chunks by line based on the character limit"""

chunks = []

chunk = []
for line in text.splitlines():
if len("\n".join(chunk + [line])) > char_limit:
chunks.append("\n".join(chunk))
chunk.clear()
chunk.append(line)

if chunk:
chunks.append("\n".join(chunk))

return chunks

0 comments on commit bca15a1

Please sign in to comment.