Fix multi-line background fill for rich display

This commit is contained in:
2025-10-30 19:34:04 +01:00
parent ffe51afd37
commit 6c68edb9fe

View File

@@ -7,24 +7,23 @@ import json
from datetime import datetime
from rich.console import Console
from rich.style import Style
from rich.text import Text
console = Console()
# Define the emoji pattern and coloring function
emoji_pattern = re.compile(
r"""
(
U0001F600-U0001F64F # emoticons
U0001F300-U0001F5FF # symbols & pictographs
U0001F680-U0001F6FF # transport & map symbols
U0001F1E0-U0001F1FF # flags (iOS)
U2702-U27B0 # Dingbats
U24C2-U2B55 # Enclosed characters
U0001F900-U0001F9FF # Supplemental Symbols & Pictographs
U200D # ZWJ
UFE0F # VS-16
)+"""
, re.UNICODE | re.VERBOSE)
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')+', flags=re.UNICODE)
def colour_emoji(txt):
return emoji_pattern.sub(r'[magenta]\1[/magenta]', txt)
@@ -107,18 +106,18 @@ def main():
username_style = Style(color=user_color, bold=True)
# Format message for terminal
formatted_message = f"[{username_style}]{author_display_name}[/]: {message_text}"
# Calculate padding to fill terminal width
rendered_length = console.measure(formatted_message).maximum
padding = max(0, console.width - rendered_length)
padded_message = f"{formatted_message}{' ' * padding}"
# Use rich.text.Text to build the message with styles
full_message_text = Text()
full_message_text.append(f"{author_display_name}: ", style=username_style)
full_message_text.append(message_text)
# Alternate background styles
background_style = Style(bgcolor="#2B2B2B") if message_count % 2 == 0 else Style(bgcolor="#3A3A3A")
console.print(padded_message, style=background_style, overflow="crop")
log_file.write(f"{datetime.now().strftime("%H:%M:%S")} {formatted_message}\n")
# Print the message, letting rich handle wrapping and filling the width
# The 'width' parameter ensures the background fills the terminal width
console.print(full_message_text, style=background_style, width=console.width, overflow="crop")
log_file.write(f"{datetime.now().strftime("%H:%M:%S")} {full_message_text.plain}\n")
message_count += 1
except Exception as e: