From 2df25ac089173bb7733126b1203f4dfc80c6f9bb Mon Sep 17 00:00:00 2001 From: "n loewen (aider)" Date: Sat, 7 Jun 2025 20:18:52 +0100 Subject: [PATCH] feat: Preserve scroll position percentage when switching commits --- gtm | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gtm b/gtm index 007e933..971c64c 100755 --- a/gtm +++ b/gtm @@ -127,6 +127,13 @@ def main(stdscr, filename, show_whole_diff=False, show_additions=False, show_del stdscr.erase() # Use erase instead of clear for less flickering height, width = stdscr.getmaxyx() + # Calculate current scroll position as percentage before changing commits + scroll_percentage = 0 + if len(file_lines) > 0 and focus == "left" and key in [curses.KEY_DOWN, curses.KEY_UP, ord('j'), ord('k')]: + # Calculate current position as percentage before loading new file + if max_scroll > 0: + scroll_percentage = scroll_offset / max_scroll + # Only fetch file content when commit changes if key in [curses.KEY_DOWN, curses.KEY_UP, ord('j'), ord('k')] and focus == "left": commit_hash = commits[selected_commit].split()[0] @@ -140,6 +147,12 @@ def main(stdscr, filename, show_whole_diff=False, show_additions=False, show_del if show_whole_diff or show_additions or show_deletions: added_lines, deleted_lines = get_diff_info(commit_hash, prev_commit_hash, filename) + # Apply the saved scroll percentage to the new file + max_scroll = max(0, len(file_lines) - (height - 1)) + if max_scroll > 0: + scroll_offset = int(scroll_percentage * max_scroll) + + # Recalculate max_scroll and ensure scroll_offset is within bounds max_scroll = max(0, len(file_lines) - (height - 1)) scroll_offset = min(scroll_offset, max_scroll) visible_lines = file_lines[scroll_offset:scroll_offset + height - 1]