feat: Preserve scroll position percentage when switching commits

This commit is contained in:
n loewen (aider) 2025-06-07 20:18:52 +01:00
parent 4a780cf193
commit 2df25ac089
1 changed files with 13 additions and 0 deletions

13
gtm
View File

@ -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]