From 702250c329dedb9ca72306325d1894453182307f Mon Sep 17 00:00:00 2001 From: Russell Schmidt Date: Tue, 10 Jun 2025 17:42:14 -0500 Subject: [PATCH] Fix crash with newlines, message spacing --- contact/ui/contact_ui.py | 2 +- contact/ui/nav_utils.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/contact/ui/contact_ui.py b/contact/ui/contact_ui.py index db482fb..a10d289 100644 --- a/contact/ui/contact_ui.py +++ b/contact/ui/contact_ui.py @@ -550,7 +550,7 @@ def draw_messages_window(scroll_to_bottom: bool = False) -> None: row = 0 for prefix, message in messages: full_message = f"{prefix}{message}" - wrapped_lines = wrap_text(full_message, messages_win.getmaxyx()[1]) + wrapped_lines = wrap_text(full_message, messages_win.getmaxyx()[1] - 2) msg_line_count += len(wrapped_lines) messages_pad.resize(msg_line_count, messages_win.getmaxyx()[1]) diff --git a/contact/ui/nav_utils.py b/contact/ui/nav_utils.py index fb48048..57f2e6d 100644 --- a/contact/ui/nav_utils.py +++ b/contact/ui/nav_utils.py @@ -300,6 +300,11 @@ def text_width(text: str) -> int: def wrap_text(text: str, wrap_width: int) -> List[str]: """Wraps text while preserving spaces and breaking long words.""" + + whitespace = '\t\n\x0b\x0c\r ' + whitespace_trans = dict.fromkeys(map(ord, whitespace), ord(' ')) + text = text.translate(whitespace_trans) + words = re.findall(r"\S+|\s+", text) # Capture words and spaces separately wrapped_lines = [] line_buffer = "" @@ -312,7 +317,7 @@ def wrap_text(text: str, wrap_width: int) -> List[str]: if word_length > wrap_width: # Break long words if line_buffer: - wrapped_lines.append(line_buffer) + wrapped_lines.append(line_buffer.strip()) line_buffer = "" line_length = 0 for i in range(0, word_length, wrap_width): @@ -320,7 +325,7 @@ def wrap_text(text: str, wrap_width: int) -> List[str]: continue if line_length + word_length > wrap_width and word.strip(): - wrapped_lines.append(line_buffer) + wrapped_lines.append(line_buffer.strip()) line_buffer = "" line_length = 0 @@ -328,7 +333,7 @@ def wrap_text(text: str, wrap_width: int) -> List[str]: line_length += word_length if line_buffer: - wrapped_lines.append(line_buffer) + wrapped_lines.append(line_buffer.strip()) return wrapped_lines