feat: Optimize scrolling performance and reduce screen flickering

This commit is contained in:
n loewen (aider) 2025-05-05 09:13:00 +01:00
parent df1a0d4ad3
commit b6ec425d91
1 changed files with 27 additions and 6 deletions

View File

@ -24,6 +24,9 @@ def main(stdscr, filename):
if curses.has_colors(): if curses.has_colors():
curses.use_default_colors() # Use terminal's default colors curses.use_default_colors() # Use terminal's default colors
# Initialize key variable
key = 0
commits = get_commits(filename) commits = get_commits(filename)
selected_commit = 0 selected_commit = 0
divider_col = 40 divider_col = 40
@ -32,13 +35,22 @@ def main(stdscr, filename):
dragging_divider = False dragging_divider = False
# Initialize file content
commit_hash = commits[selected_commit].split()[0]
file_lines = get_file_at_commit(commit_hash, filename)
# Enable nodelay for smoother scrolling
stdscr.nodelay(True)
while True: while True:
stdscr.clear() stdscr.erase() # Use erase instead of clear for less flickering
height, width = stdscr.getmaxyx() height, width = stdscr.getmaxyx()
# Fetch file content for selected commit # Only fetch file content when commit changes
commit_hash = commits[selected_commit].split()[0] if key in [curses.KEY_DOWN, curses.KEY_UP, ord('j'), ord('k')] and focus == "left":
file_lines = get_file_at_commit(commit_hash, filename) commit_hash = commits[selected_commit].split()[0]
file_lines = get_file_at_commit(commit_hash, filename)
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]
@ -57,9 +69,12 @@ def main(stdscr, filename):
for y in range(height): for y in range(height):
stdscr.addch(y, divider_col, divider_char) stdscr.addch(y, divider_col, divider_char)
# Draw file content (right pane) # Draw file content (right pane) - more efficiently
right_width = width - divider_col - 3
for i, line in enumerate(visible_lines): for i, line in enumerate(visible_lines):
stdscr.addnstr(i, divider_col + 2, line, width - divider_col - 3) # Only draw what fits in the window
if i < height - 1:
stdscr.addnstr(i, divider_col + 2, line, right_width)
# Status bar for right pane # Status bar for right pane
visible_height = height - 1 # Reserve 1 line for the status bar visible_height = height - 1 # Reserve 1 line for the status bar
@ -75,8 +90,14 @@ def main(stdscr, filename):
x = width - len(status) - 1 x = width - len(status) - 1
stdscr.addnstr(height - 1, x, status, len(status), curses.A_REVERSE) stdscr.addnstr(height - 1, x, status, len(status), curses.A_REVERSE)
# Get input with a small timeout for smoother scrolling
stdscr.timeout(50) # 50ms timeout
key = stdscr.getch() key = stdscr.getch()
# If no key was pressed, continue the loop
if key == -1:
continue
# Mouse interaction # Mouse interaction
if key == curses.KEY_MOUSE: if key == curses.KEY_MOUSE:
try: try: