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