From 7052a80afe1cea9078c21514c6cc0e7c029b20c4 Mon Sep 17 00:00:00 2001 From: Ramforth Date: Thu, 30 Oct 2025 19:46:23 +0100 Subject: [PATCH] Fix multi-line background fill using Text.stylize --- pytchat_listener.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/pytchat_listener.py b/pytchat_listener.py index dc069e9..a94c893 100644 --- a/pytchat_listener.py +++ b/pytchat_listener.py @@ -14,15 +14,15 @@ console = Console() # Define the emoji pattern and coloring function emoji_pattern = re.compile( r'(' - r'\U0001F600-\U0001F64F' # emoticons - r'\U0001F300-\U0001F5FF' # symbols & pictographs - r'\U0001F680-\U0001F6FF' # transport & map symbols - r'\U0001F1E0-\U0001F1FF' # flags (iOS) - r'\u2702-\u27B0' # Dingbats - r'\u24C2-\u2B55' # Enclosed characters - r'\U0001F900-\U0001F9FF' # Supplemental Symbols & Pictographs - r'\u200D' # ZWJ - r'\uFE0F' # VS-16 + r' \U0001F600-\U0001F64F' # emoticons + r' \U0001F300-\U0001F5FF' # symbols & pictographs + r' \U0001F680-\U0001F6FF' # transport & map symbols + r' \U0001F1E0-\U0001F1FF' # flags (iOS) + r' \u2702-\u27B0' # Dingbats + r' \u24C2-\u2B55' # Enclosed characters + r' \U0001F900-\U0001F9FF' # Supplemental Symbols & Pictographs + r' \u200D' # ZWJ + r' \uFE0F' # VS-16 r')+', flags=re.UNICODE) def colour_emoji(txt): @@ -109,8 +109,7 @@ def main(): user_color = get_user_color(author_channel_id) username_style = Style(color=user_color, bold=True) - # Format message for terminal - # Use rich.text.Text to build the message with styles + # Create a Text object for the entire message full_message_text = Text() full_message_text.append(f"[{username_style}]{author_display_name}[/]: ") full_message_text.append(message_text) @@ -118,12 +117,12 @@ def main(): # Alternate background styles background_style = Style(bgcolor="#2B2B2B") if message_count % 2 == 0 else Style(bgcolor="#3A3A3A") - # Create a Text object for the entire line with the background style - # Then pad it to the full console width - line_to_print = Text.assemble(full_message_text, style=background_style) - line_to_print.pad_to_width(console.width, style=background_style) + # Apply the background style to the entire Text object + full_message_text.stylize(background_style) - console.print(line_to_print, overflow="crop") + # Print the message, letting rich handle wrapping and filling the width + # rich will automatically apply the background to wrapped lines if styled on the Text object + console.print(full_message_text, width=console.width, overflow="crop") log_file.write(f"{datetime.now().strftime("%H:%M:%S")} {full_message_text.plain}\n") message_count += 1 @@ -139,4 +138,4 @@ def main(): console.print(f"[green]Chat log saved to {log_filename}[/green]") if __name__ == '__main__': - main() \ No newline at end of file + main()