From c1605e99cd9bf5f85a7ca8f6af95917b1c96925a Mon Sep 17 00:00:00 2001 From: "n loewen (aider)" Date: Sat, 7 Jun 2025 23:15:22 +0100 Subject: [PATCH] fix: Improve error handling and reduce timeout in main event loop --- gtm2.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/gtm2.py b/gtm2.py index e080036..9568d8c 100755 --- a/gtm2.py +++ b/gtm2.py @@ -544,6 +544,7 @@ def handle_keyboard_input(key, state): def main(stdscr, filename, show_diff, show_add, show_del, mouse): curses.curs_set(0) stdscr.keypad(True) + stdscr.timeout(50) # Less aggressive timeout (50ms instead of 10ms) if curses.has_colors(): curses.use_default_colors() @@ -566,20 +567,26 @@ def main(stdscr, filename, show_diff, show_add, show_del, mouse): draw_ui(stdscr, state) while not state.should_exit: - key = stdscr.getch() - - # Process input and update the application state - if key == curses.KEY_RESIZE: + try: + key = stdscr.getch() + + # Process input and update the application state + if key == -1: # No input available (timeout) + pass # Just redraw the UI and continue + elif key == curses.KEY_RESIZE: h, w = stdscr.getmaxyx() state.update_dimensions(h, w) - elif state.enable_mouse and key == curses.KEY_MOUSE: - handle_mouse_input(stdscr, state) - else: - handle_keyboard_input(key, state) - - # After every action, redraw the UI to reflect changes immediately. - # This is crucial for real-time feedback during mouse drags. - draw_ui(stdscr, state) + elif state.enable_mouse and key == curses.KEY_MOUSE: + handle_mouse_input(stdscr, state) + else: + handle_keyboard_input(key, state) + + # After every action, redraw the UI to reflect changes immediately. + # This is crucial for real-time feedback during mouse drags. + draw_ui(stdscr, state) + except Exception as e: + # Prevent crashes by catching exceptions + pass # Disable mouse motion events when exiting if state.enable_mouse: