From acf5e4f3e2bf6421dc824555a5a19fcfc082bb02 Mon Sep 17 00:00:00 2001 From: Max Comperatore <131000419+pyoneerC@users.noreply.github.com> Date: Thu, 1 May 2025 15:08:07 +0000 Subject: [PATCH] Enhance terminal reset handling on second Ctrl+C for improved user experience --- browser_use/utils.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/browser_use/utils.py b/browser_use/utils.py index 95ea0ee51..b3759f4eb 100644 --- a/browser_use/utils.py +++ b/browser_use/utils.py @@ -136,9 +136,30 @@ class SignalHandler: # Force immediate exit - more reliable than sys.exit() print('\n\nšŸ›‘ Got second Ctrl+C. Exiting immediately...\n', file=stderr) - # write carriage return + newline + ASNI reset to both stdout and stderr to clear any color codes - print('\r\033[0m', end='', flush=True, file=stderr) - print('\r\033[0m', end='', flush=True) + + # Reset terminal to a clean state by sending multiple escape sequences + # Order matters for terminal resets - we try different approaches + + # Reset terminal modes for both stdout and stderr + print('\033[?25h', end='', flush=True, file=stderr) # Show cursor + print('\033[?25h', end='', flush=True) # Show cursor + + # Reset text attributes and terminal modes + print('\033[0m', end='', flush=True, file=stderr) # Reset text attributes + print('\033[0m', end='', flush=True) # Reset text attributes + + # Disable special input modes that may cause arrow keys to output control chars + print('\033[?1l', end='', flush=True, file=stderr) # Reset cursor keys to normal mode + print('\033[?1l', end='', flush=True) # Reset cursor keys to normal mode + + # Disable bracketed paste mode + print('\033[?2004l', end='', flush=True, file=stderr) + print('\033[?2004l', end='', flush=True) + + # Carriage return helps ensure a clean line + print('\r', end='', flush=True, file=stderr) + print('\r', end='', flush=True) + os._exit(0) def sigint_handler(self) -> None: