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

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