Fix multi-line background fill using Text.stylize

This commit is contained in:
2025-10-30 19:46:23 +01:00
parent 93986e0681
commit 7052a80afe

View File

@@ -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