From 521351538735ff314fd47258a25ff16d4de1539c Mon Sep 17 00:00:00 2001 From: "Nyeki.py" <84998222+Nekidev@users.noreply.github.com> Date: Fri, 18 Oct 2024 01:00:59 -0300 Subject: [PATCH] feat(bot): :lipstick: Align differences in `p.stats`. --- payphone/cogs/mods/cog.py | 5 +++-- payphone/core/formatting.py | 31 +++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/payphone/cogs/mods/cog.py b/payphone/cogs/mods/cog.py index b2b0ad6..2cc5634 100644 --- a/payphone/cogs/mods/cog.py +++ b/payphone/cogs/mods/cog.py @@ -369,8 +369,9 @@ class Mods(base.Cog): diff = value - prev_value rows_with_diff[stat_i].append( - str(value) - + (f" ({"+" if diff > 0 else ""}{diff})" if diff != 0 else "") + value + if diff == 0 + else (value, f"({"+" if diff > 0 else ""}{diff})") ) table = fmt.monospace_table( diff --git a/payphone/core/formatting.py b/payphone/core/formatting.py index 3296dff..7a12dec 100644 --- a/payphone/core/formatting.py +++ b/payphone/core/formatting.py @@ -57,7 +57,7 @@ def monospace_grid(values: list[str], space: int = 14) -> str: def monospace_table( - rows: list[tuple], + rows: list[list[str | tuple[str, str]]], headers: list | None = None, column_separator: str = "|", header_separator: str = "-", @@ -66,12 +66,17 @@ def monospace_table( ) -> str: """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: - rows (list[tuple]): The rows. + rows (list[list[str | tuple[str, str]]]): The rows. headers (list | None, optional): The headers. column_separator (str, optional): The column 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. Returns: @@ -81,7 +86,14 @@ def monospace_table( max_sizes = [ 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 []) ) ) @@ -92,8 +104,15 @@ def monospace_table( return column_separator.join( [ (" " * 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) for i, col in enumerate(row) ]