feat: Preserve scroll position percentage when switching commits
This commit is contained in:
parent
4a780cf193
commit
2df25ac089
13
gtm
13
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
|
stdscr.erase() # Use erase instead of clear for less flickering
|
||||||
height, width = stdscr.getmaxyx()
|
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
|
# Only fetch file content when commit changes
|
||||||
if key in [curses.KEY_DOWN, curses.KEY_UP, ord('j'), ord('k')] and focus == "left":
|
if key in [curses.KEY_DOWN, curses.KEY_UP, ord('j'), ord('k')] and focus == "left":
|
||||||
commit_hash = commits[selected_commit].split()[0]
|
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:
|
if show_whole_diff or show_additions or show_deletions:
|
||||||
added_lines, deleted_lines = get_diff_info(commit_hash, prev_commit_hash, filename)
|
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))
|
max_scroll = max(0, len(file_lines) - (height - 1))
|
||||||
scroll_offset = min(scroll_offset, max_scroll)
|
scroll_offset = min(scroll_offset, max_scroll)
|
||||||
visible_lines = file_lines[scroll_offset:scroll_offset + height - 1]
|
visible_lines = file_lines[scroll_offset:scroll_offset + height - 1]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue