feat(bot): 💄 Align differences in p.stats.

This commit is contained in:
Nyeki.py
2024-10-18 01:00:59 -03:00
parent 8cd1613e3c
commit 5213515387
2 changed files with 28 additions and 8 deletions
+3 -2
View File
@@ -369,8 +369,9 @@ class Mods(base.Cog):
diff = value - prev_value diff = value - prev_value
rows_with_diff[stat_i].append( rows_with_diff[stat_i].append(
str(value) value
+ (f" ({"+" if diff > 0 else ""}{diff})" if diff != 0 else "") if diff == 0
else (value, f"({"+" if diff > 0 else ""}{diff})")
) )
table = fmt.monospace_table( table = fmt.monospace_table(
+25 -6
View File
@@ -57,7 +57,7 @@ def monospace_grid(values: list[str], space: int = 14) -> str:
def monospace_table( def monospace_table(
rows: list[tuple], rows: list[list[str | tuple[str, str]]],
headers: list | None = None, headers: list | None = None,
column_separator: str = "|", column_separator: str = "|",
header_separator: str = "-", header_separator: str = "-",
@@ -66,12 +66,17 @@ def monospace_table(
) -> str: ) -> str:
"""Creates a monospace table. """Creates a monospace table.
Creates a monospace table. Row columns can be either a string with the column's content, or a
tuple with two items, being the first one aligned to the left and the second one aligned to the
right. Non-string values will be converted to string using `str(value)`.
Args: Args:
rows (list[tuple]): The rows. rows (list[list[str | tuple[str, str]]]): The rows.
headers (list | None, optional): The headers. headers (list | None, optional): The headers.
column_separator (str, optional): The column separator. Defaults to "|". column_separator (str, optional): The column separator. Defaults to "|".
header_separator (str, optional): The header separator. Defaults to "-". header_separator (str, optional): The header separator. Defaults to "-".
intersection (str, optional): The intersection character where column and row separators meet. intersection (str, optional): The intersection character where column and row separators
meet.
padding (int, optional): The padding that each column has at its sides. padding (int, optional): The padding that each column has at its sides.
Returns: Returns:
@@ -81,7 +86,14 @@ def monospace_table(
max_sizes = [ max_sizes = [
max( max(
*( *(
[len(str(row[i])) for row in rows] [
(
len(str(row[i]))
if not isinstance(row[i], tuple)
else (sum(len(str(item)) for item in row[i]) + 1)
)
for row in rows
]
+ ([len(headers[i])] if headers is not None else []) + ([len(headers[i])] if headers is not None else [])
) )
) )
@@ -92,8 +104,15 @@ def monospace_table(
return column_separator.join( return column_separator.join(
[ [
(" " * padding) (" " * padding)
+ str(col) + (
+ (" " * (max_sizes[i] - len(str(col)))) (str(col) + (" " * (max_sizes[i] - len(str(col)))))
if not isinstance(col, tuple)
else (
str(col[0])
+ (" " * (max_sizes[i] - len(str(col[0])) - len(str(col[1]))))
+ str(col[1])
)
)
+ (" " * padding) + (" " * padding)
for i, col in enumerate(row) for i, col in enumerate(row)
] ]