From d76e5bdc176c5e0f65479cf681f2e089ea6869f3 Mon Sep 17 00:00:00 2001 From: "n loewen (aider)" Date: Sat, 7 Jun 2025 19:57:14 +0100 Subject: [PATCH] fix: Improve display of added and deleted lines with proper formatting and positioning --- gtm | 82 ++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/gtm b/gtm index 9342d00..7766389 100755 --- a/gtm +++ b/gtm @@ -166,36 +166,62 @@ def main(stdscr, filename, show_additions=False, show_deletions=False): # Draw file content (right pane) - more efficiently right_width = width - divider_col - 3 + + # First, collect all lines to display (regular and deleted) + display_lines = [] + for i, line in enumerate(visible_lines): - # Only draw what fits in the window - if i < height - 1: - line_num = i + scroll_offset + 1 # 1-based line number + line_num = i + scroll_offset + 1 # 1-based line number + + # Check if this is an added line + is_added = False + if show_additions: + for added_line_num, _ in added_lines: + if added_line_num == line_num: + is_added = True + break + + # Add the regular line to our display list + display_lines.append({ + 'type': 'added' if is_added else 'regular', + 'content': line, + 'line_num': line_num + }) + + # If showing deletions, check if there are deleted lines at this position + if show_deletions: + for del_line_num, del_line_content in deleted_lines: + if del_line_num == line_num: + # Add the deleted line after the current line + display_lines.append({ + 'type': 'deleted', + 'content': del_line_content, + 'line_num': line_num + }) + + # Now display all lines + display_row = 0 + for line_info in display_lines: + # Stop if we've reached the bottom of the screen + if display_row >= height - 1: + break - # Check if this is an added line - is_added = False - if show_additions: - for added_line_num, _ in added_lines: - if added_line_num == line_num: - is_added = True - break - - # Display the line with appropriate formatting - if is_added: - stdscr.addstr(i, divider_col + 2, "+ ", curses.color_pair(3)) - stdscr.addnstr(i, divider_col + 4, line, right_width - 2, curses.color_pair(3)) - else: - stdscr.addnstr(i, divider_col + 2, line, right_width) - - # Display deleted lines if enabled - if show_deletions: - # Check if there are deleted lines after the current line - for del_line_num, del_line_content in deleted_lines: - if del_line_num == line_num: - # Only show if we have space - if i + 1 < height - 1: - i += 1 # Move to next line for displaying the deleted content - stdscr.addstr(i, divider_col + 2, "- ", curses.color_pair(4)) - stdscr.addnstr(i, divider_col + 4, del_line_content, right_width - 2, curses.color_pair(4)) + line_type = line_info['type'] + content = line_info['content'] + + if line_type == 'added': + # Green with + prefix for added lines + stdscr.addstr(display_row, divider_col + 2, "+ ", curses.color_pair(3)) + stdscr.addnstr(display_row, divider_col + 4, content, right_width - 2, curses.color_pair(3)) + elif line_type == 'deleted': + # Red with - prefix for deleted lines + stdscr.addstr(display_row, divider_col + 2, "- ", curses.color_pair(4)) + stdscr.addnstr(display_row, divider_col + 4, content, right_width - 2, curses.color_pair(4)) + else: + # Regular line + stdscr.addnstr(display_row, divider_col + 2, content, right_width) + + display_row += 1 # Status bars for both panes visible_height = height - 1 # Reserve 1 line for the status bar