From 4dccd78686e1caa420eff17a34e97d3741405f66 Mon Sep 17 00:00:00 2001 From: crazy hugsy Date: Sun, 10 Nov 2024 10:52:34 -0800 Subject: [PATCH] Simplified `clear_screen` command (#1148) ## Description Simplify `clear_screen` API by sending directly the clear screen sequence to `sys.stdout` by default. Fixes #1147 --- gef.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/gef.py b/gef.py index c31ee9009..f660408b6 100644 --- a/gef.py +++ b/gef.py @@ -3898,19 +3898,12 @@ def get_memory_alignment(in_bits: bool = False) -> int: def clear_screen(tty: str = "") -> None: """Clear the screen.""" - global gef - if not tty: - gdb.execute("shell clear -x") - return + clean_sequence = "\x1b[H\x1b[J" + if tty: + pathlib.Path(tty).write_text(clean_sequence) + else: + sys.stdout.write(clean_sequence) - # Since the tty can be closed at any time, a PermissionError exception can - # occur when `clear_screen` is called. We handle this scenario properly - try: - with open(tty, "wt") as f: - f.write("\x1b[H\x1b[J") - except PermissionError: - gef.ui.redirect_fd = None - gef.config["context.redirect"] = "" return