From db1a530531cecfd32a3fc59a882c994fd2271218 Mon Sep 17 00:00:00 2001 From: actionless Date: Thu, 12 Sep 2024 18:22:13 +0200 Subject: [PATCH] refactor(pikaprint: sidejoin_multiline_paragraphs): change function signature to be more clear and extendable --- pikaur/info_cli.py | 11 +++--- pikaur/pikaprint.py | 3 +- pikaur/pikasay.py | 81 ++++++++++++++++++++------------------ pikaur/print_department.py | 7 ++-- 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/pikaur/info_cli.py b/pikaur/info_cli.py index c7e19b2e8..e04686d56 100644 --- a/pikaur/info_cli.py +++ b/pikaur/info_cli.py @@ -112,11 +112,12 @@ def cli_info_packages() -> None: # noqa: PLR0914 ) value_height = len(value_display.splitlines()) line = sidejoin_multiline_paragraphs( - "", - key_display + ( - "\n" + " " * (longest_field_length + 2 + 1) - ) * (value_height - 1), - value_display, + "", ( + key_display + ( + "\n" + " " * (longest_field_length + 2 + 1) + ) * (value_height - 1), + value_display, + ), ) pkg_info_lines.append(line) print_stdout( diff --git a/pikaur/pikaprint.py b/pikaur/pikaprint.py index 9b4f52f99..33f44aa01 100644 --- a/pikaur/pikaprint.py +++ b/pikaur/pikaprint.py @@ -4,6 +4,7 @@ import shutil import sys import termios +from collections.abc import Sequence from itertools import zip_longest from typing import TYPE_CHECKING @@ -347,7 +348,7 @@ def make_equal_right_padding(multiline_string: str, length: int | None = None) - ) -def sidejoin_multiline_paragraphs(join_separator: str, *multiline_strings: str) -> str: +def sidejoin_multiline_paragraphs(join_separator: str, multiline_strings: Sequence[str]) -> str: return "\n".join( join_separator.join(line or "" for line in lines) for lines in zip_longest( diff --git a/pikaur/pikasay.py b/pikaur/pikasay.py index f8b5b9576..e18c27b66 100644 --- a/pikaur/pikasay.py +++ b/pikaur/pikasay.py @@ -120,42 +120,42 @@ def bubble_right( f"{' ' * (bubble_width + margin * 2 + bubble_handle_width)}\n" * height_compensating_margin ) + sidejoin_multiline_paragraphs( - "", - "\n".join( - [" " * margin] * (bubble_height + vert_margin - 1 + 2), - ), - "\n".join(( - *([" "] * (vert_margin + 1)), - "/", - *(["|"] * (bubble_height - 2)), - "\\", - )), - "\n".join(( - *([" " * (bubble_width - 2)] * vert_margin), - "_" * (bubble_width - 2), - *([" " * (bubble_width - 2)] * (vert_padding + 1)), - formatted_paragraph, - *([" " * (bubble_width - 2)] * (vert_padding)), - "_" * (bubble_width - 2), - *([" " * (bubble_width - 2)] * vert_margin), - )), - "\n".join(( - *([" "] * (vert_margin + 1)), - "\\", - *(["|"] * (bubble_handle_position)), - " ", - *(["|"] * (bubble_height - 2 - bubble_handle_position - 1)), - "/", - )), - "\n".join(( - *([" "] * (vert_margin + 1)), - *([" "] * (bubble_handle_position)), - bubble_handle, - *([" "] * (bubble_height - 2 - bubble_handle_position)), - )), - "\n".join( - [" " * margin] * (bubble_height + vert_margin - 1 + 2), - ), + "", [ + "\n".join(lines) + for lines in ( + [" " * margin] * (bubble_height + vert_margin - 1 + 2), + ( + *([" "] * (vert_margin + 1)), + "/", + *(["|"] * (bubble_height - 2)), + "\\", + ), + ( + *([" " * (bubble_width - 2)] * vert_margin), + "_" * (bubble_width - 2), + *([" " * (bubble_width - 2)] * (vert_padding + 1)), + formatted_paragraph, + *([" " * (bubble_width - 2)] * (vert_padding)), + "_" * (bubble_width - 2), + *([" " * (bubble_width - 2)] * vert_margin), + ), + ( + *([" "] * (vert_margin + 1)), + "\\", + *(["|"] * (bubble_handle_position)), + " ", + *(["|"] * (bubble_height - 2 - bubble_handle_position - 1)), + "/", + ), + ( + *([" "] * (vert_margin + 1)), + *([" "] * (bubble_handle_position)), + bubble_handle, + *([" "] * (bubble_height - 2 - bubble_handle_position)), + ), + [" " * margin] * (bubble_height + vert_margin - 1 + 2), + ) + ], ) @@ -169,9 +169,12 @@ def pikasay( # noqa: PLR0917 )) elif orientation == "vertical": message = sidejoin_multiline_paragraphs( - "", - bubble_right(text=text, margin=margin, padding=padding, width=width, mascot=mascot_pic), - mascot_pic, + "", ( + bubble_right( + text=text, margin=margin, padding=padding, width=width, mascot=mascot_pic, + ), + mascot_pic, + ), ) else: raise ValueError(orientation) diff --git a/pikaur/print_department.py b/pikaur/print_department.py index f26287a55..0b636c404 100644 --- a/pikaur/print_department.py +++ b/pikaur/print_department.py @@ -62,9 +62,9 @@ def print_version(pacman_version: str, pyalpm_version: str, *, quiet: bool = Fal year = str(datetime.now(tz=DEFAULT_TIMEZONE).year) sys.stdout.write( sidejoin_multiline_paragraphs( - " ", - make_equal_right_padding(PIKAPIC), - r""" + " ", ( + make_equal_right_padding(PIKAPIC), + r""" @@ -75,6 +75,7 @@ def print_version(pacman_version: str, pyalpm_version: str, *, quiet: bool = Fal """ + pacman_version + r""" pyalpm v""" + pyalpm_version + r""" """, + ), ) + "\n", )