From dcf892a8ea687e140d60ca08505b28800e2d1753 Mon Sep 17 00:00:00 2001 From: "n loewen (aider)" Date: Sun, 8 Jun 2025 09:26:55 +0100 Subject: [PATCH] fix: Add bounds checking and error handling for curses drawing --- gtm | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/gtm b/gtm index 6f02b88..6484d4d 100755 --- a/gtm +++ b/gtm @@ -941,15 +941,31 @@ def draw_commit_details(stdscr, state: AppState, layout: StatusBarLayout): # Draw the author and branch on the right author_branch_info = f" {state.commit_author} [{state.commit_branch}] " right_x = layout.screen_width - len(author_branch_info) - if right_x >= 0: - stdscr.addstr(second_line, right_x, author_branch_info, curses.A_REVERSE) + if right_x >= len(commit_info) and right_x >= 0: + try: + stdscr.addstr(second_line, right_x, author_branch_info, curses.A_REVERSE) + except curses.error: + # If it still fails, truncate the author info to fit + max_author_width = layout.screen_width - len(commit_info) - 4 # Leave some margin + if max_author_width > 10: # Only show if we have reasonable space + truncated_info = f" {state.commit_author[:max_author_width-10]}... " + try: + stdscr.addstr(second_line, layout.screen_width - len(truncated_info), truncated_info, curses.A_REVERSE) + except curses.error: + pass # Give up if we still can't fit it # Draw the first line of the commit message in between - available_width = layout.screen_width - len(commit_info) - len(author_branch_info) - 1 - first_line = message_lines[0] if message_lines else "" - if len(first_line) > available_width: - first_line = first_line[:available_width-3] + "..." - stdscr.addstr(second_line, len(commit_info), first_line, curses.A_REVERSE) + # Only draw if we have space and the author info was successfully placed + if right_x >= len(commit_info): + available_width = right_x - len(commit_info) - 1 + first_line = message_lines[0] if message_lines else "" + if available_width > 0: + if len(first_line) > available_width: + first_line = first_line[:available_width-3] + "..." + try: + stdscr.addstr(second_line, len(commit_info), first_line, curses.A_REVERSE) + except curses.error: + pass # Silently fail if we can't draw the message # Draw additional lines of the commit message if we have more space for i, line in enumerate(message_lines[1:], 1):