From 3c23ee2ce9bb83f2f5dcbe3b922966000424eb44 Mon Sep 17 00:00:00 2001 From: kinoshitakenta Date: Wed, 28 May 2025 15:57:25 +0800 Subject: [PATCH] feat: improves screen clearing functionality Updates the screen clearing function to provide a more robust cross-platform solution. It now attempts to reset text attributes on Windows and uses `shutil.which` to locate the `clear` command on Unix-like systems, falling back to ANSI escape codes if necessary. This change improves the screen-clearing function, ensuring its effectiveness regardless of the operating system or available commands. --- main.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 15b1165..7ceff18 100644 --- a/main.py +++ b/main.py @@ -25,12 +25,30 @@ def get_usage() -> list[tuple]: return cmd_list -def display_usage(): +def clear_screen(): if os.name == 'nt': + # Windows system + try: + # try to reset the text attributes + kernel32 = ctypes.windll.kernel32 + handle = kernel32.GetStdHandle(-11) # STD_OUTPUT_HANDLE + kernel32.SetConsoleTextAttribute(handle, 7) + except Exception: + pass + os.system('cls') else: - os.system('clear') + # Unix-like system + clear_cmd = shutil.which('clear') + if clear_cmd: + os.system(clear_cmd) + else: + # backup plan: Use ANSI escape codes (to clear and reset the screen). + print('\033c', end='') + +def display_usage(): + clear_screen() for cmd, msg in get_usage(): print(f"{cmd}: {msg}")