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: