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

@ -23,6 +23,9 @@ def main(stdscr, filename):
# Initialize colors if terminal supports them
if curses.has_colors():
curses.use_default_colors() # Use terminal's default colors
# Initialize key variable
key = 0
commits = get_commits(filename)
selected_commit = 0
@ -32,13 +35,22 @@ def main(stdscr, filename):
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:
stdscr.clear()
stdscr.erase() # Use erase instead of clear for less flickering
height, width = stdscr.getmaxyx()
# Fetch file content for selected commit
commit_hash = commits[selected_commit].split()[0]
file_lines = get_file_at_commit(commit_hash, filename)
# 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]
file_lines = get_file_at_commit(commit_hash, filename)
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]
@ -57,9 +69,12 @@ def main(stdscr, filename):
for y in range(height):
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):
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
visible_height = height - 1 # Reserve 1 line for the status bar
@ -75,7 +90,13 @@ def main(stdscr, filename):
x = width - len(status) - 1
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()
# If no key was pressed, continue the loop
if key == -1:
continue
# Mouse interaction
if key == curses.KEY_MOUSE: