From a66b06daab90a9736df9e183809fdaaf97e83ecf Mon Sep 17 00:00:00 2001 From: "n loewen (aider)" Date: Sun, 8 Jun 2025 02:52:59 +0100 Subject: [PATCH] refactor: Remove show_whole_diff flag and simplify diff mode handling --- gtm | 53 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/gtm b/gtm index 205e318..9a70157 100755 --- a/gtm +++ b/gtm @@ -101,7 +101,6 @@ class AppState: filename: str width: int height: int - show_whole_diff: bool show_additions: bool show_deletions: bool enable_mouse: bool @@ -216,7 +215,7 @@ def load_commit_content(state: AppState) -> AppState: if state.selected_commit_idx < len(state.commits) - 1: prev_commit_hash = state.commits[state.selected_commit_idx + 1].split()[0] - if state.show_whole_diff or state.show_additions or state.show_deletions: + if state.show_additions or state.show_deletions: added_lines, deleted_lines = get_diff_info(commit_hash, prev_commit_hash, state.filename) else: added_lines, deleted_lines = [], [] @@ -437,7 +436,7 @@ def draw_right_pane(stdscr, state): display_lines = [] deleted_line_map = {} - if state.show_whole_diff or state.show_deletions: + if state.show_deletions: for del_line_num, del_content in state.deleted_lines: if del_line_num not in deleted_line_map: deleted_line_map[del_line_num] = [] @@ -445,13 +444,13 @@ def draw_right_pane(stdscr, state): for i, line in enumerate(visible_lines): line_num = i + state.right_scroll_offset + 1 - if (state.show_whole_diff or state.show_deletions) and line_num in deleted_line_map: + if state.show_deletions and line_num in deleted_line_map: for del_content in deleted_line_map[line_num]: # For deleted lines, use the same line number display_lines.append({'type': 'deleted', 'content': del_content, 'line_num': line_num}) is_added = False - if state.show_whole_diff or state.show_additions: + if state.show_additions: for added_line_num, _ in state.added_lines: if added_line_num == line_num: is_added = True @@ -498,7 +497,7 @@ def draw_right_pane(stdscr, state): prefix = "- " attr = curses.color_pair(4) else: - if state.show_whole_diff or state.show_additions or state.show_deletions: + if state.show_additions or state.show_deletions: prefix = " " attr = curses.A_NORMAL @@ -934,7 +933,7 @@ def copy_selection_to_clipboard(stdscr, state): visible_lines = [] deleted_line_map = {} - if state.show_whole_diff or state.show_deletions: + if state.show_deletions: for del_line_num, del_content in state.deleted_lines: if del_line_num not in deleted_line_map: deleted_line_map[del_line_num] = [] @@ -975,11 +974,11 @@ def copy_selection_to_clipboard(stdscr, state): # Add prefix based on line type if in diff mode prefix = "" - if line_type == 'added' and (state.show_whole_diff or state.show_additions): + if line_type == 'added' and state.show_additions: prefix = "+ " - elif line_type == 'deleted' and (state.show_whole_diff or state.show_deletions): + elif line_type == 'deleted' and state.show_deletions: prefix = "- " - elif state.show_whole_diff or state.show_additions or state.show_deletions: + elif state.show_additions or state.show_deletions: prefix = " " # Calculate character positions, accounting for the prefix @@ -1118,23 +1117,18 @@ def handle_keyboard_input(key, state: AppState) -> AppState: elif key == ord('s'): return toggle_sidebar(state) elif key == ord('d'): - new_state = replace(state, show_whole_diff=not state.show_whole_diff) + # 'd' toggles both additions and deletions together + new_state = replace(state, + show_additions=not (state.show_additions and state.show_deletions), + show_deletions=not (state.show_additions and state.show_deletions)) return load_commit_content(new_state) elif key == ord('a'): - # If diff mode is on, pressing 'a' should toggle off additions - # If diff mode is off, pressing 'a' should toggle on additions - if state.show_whole_diff: - new_state = replace(state, show_additions=False, show_whole_diff=False) - else: - new_state = replace(state, show_additions=not state.show_additions) + # 'a' toggles just additions + new_state = replace(state, show_additions=not state.show_additions) return load_commit_content(new_state) elif key == ord('x'): - # If diff mode is on, pressing 'x' should toggle off deletions - # If diff mode is off, pressing 'x' should toggle on deletions - if state.show_whole_diff: - new_state = replace(state, show_deletions=False, show_whole_diff=False) - else: - new_state = replace(state, show_deletions=not state.show_deletions) + # 'x' toggles just deletions + new_state = replace(state, show_deletions=not state.show_deletions) return load_commit_content(new_state) elif key == ord('w'): return replace(state, wrap_lines=not state.wrap_lines) @@ -1149,13 +1143,13 @@ def handle_keyboard_input(key, state: AppState) -> AppState: if state.search_matches: return jump_to_prev_match(state) elif key in [99, ord('c')]: # ASCII code for 'c' - if state.show_whole_diff or state.show_additions or state.show_deletions: + if state.show_additions or state.show_deletions: return jump_to_next_change(state) elif key in [67, ord('C')]: # ASCII code for 'C' (uppercase) - if state.show_whole_diff or state.show_additions or state.show_deletions: + if state.show_additions or state.show_deletions: return jump_to_prev_change(state) elif key in [112, ord('p')]: # ASCII code for 'p' - if state.show_whole_diff or state.show_additions or state.show_deletions: + if state.show_additions or state.show_deletions: return jump_to_prev_change(state) elif key in [curses.KEY_LEFT, ord('h')]: if state.show_sidebar: @@ -1207,9 +1201,14 @@ def main(stdscr, filename, show_diff, show_add, show_del, mouse, wrap_lines=True curses.init_pair(8, curses.COLOR_BLACK, curses.COLOR_YELLOW) # Search matches height, width = stdscr.getmaxyx() + # If show_diff is true, enable both additions and deletions + if show_diff: + show_add = True + show_del = True + state = AppState( filename=filename, width=width, height=height, - show_whole_diff=show_diff, show_additions=show_add, + show_additions=show_add, show_deletions=show_del, enable_mouse=mouse, commits=get_commits(filename), wrap_lines=wrap_lines