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.
This commit is contained in:
parent
0671d49497
commit
3c23ee2ce9
22
main.py
22
main.py
|
@ -25,12 +25,30 @@ def get_usage() -> list[tuple]:
|
||||||
return cmd_list
|
return cmd_list
|
||||||
|
|
||||||
|
|
||||||
def display_usage():
|
def clear_screen():
|
||||||
if os.name == 'nt':
|
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')
|
os.system('cls')
|
||||||
else:
|
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():
|
for cmd, msg in get_usage():
|
||||||
print(f"{cmd}: {msg}")
|
print(f"{cmd}: {msg}")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue