From 0047d3d052da3603dd79b7d4c4143c7816398161 Mon Sep 17 00:00:00 2001 From: Yorick Downe Date: Tue, 17 Sep 2024 05:46:24 -0400 Subject: [PATCH] Clear scrollback --- .../cli/generate_bls_to_execution_change.py | 3 ++- ethstaker_deposit/cli/generate_keys.py | 3 ++- ethstaker_deposit/cli/new_mnemonic.py | 7 +++--- ethstaker_deposit/utils/terminal.py | 23 +++++++++++++++++++ 4 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 ethstaker_deposit/utils/terminal.py diff --git a/ethstaker_deposit/cli/generate_bls_to_execution_change.py b/ethstaker_deposit/cli/generate_bls_to_execution_change.py index 69f7e28a..445bca3d 100644 --- a/ethstaker_deposit/cli/generate_bls_to_execution_change.py +++ b/ethstaker_deposit/cli/generate_bls_to_execution_change.py @@ -38,6 +38,7 @@ closest_match, load_text, ) +from ethstaker_deposit.utils.terminal import clear_terminal from ethstaker_deposit.settings import ( MAINNET, ALL_CHAIN_KEYS, @@ -208,7 +209,7 @@ def generate_bls_to_execution_change( if not json_file_validation_result: raise ValidationError(load_text(['err_verify_btec'])) - click.clear() + clear_terminal() click.echo(OWL_0) click.echo(load_text(['msg_creation_success']) + str(bls_to_execution_changes_folder)) diff --git a/ethstaker_deposit/cli/generate_keys.py b/ethstaker_deposit/cli/generate_keys.py index f14db47d..5054cfa6 100644 --- a/ethstaker_deposit/cli/generate_keys.py +++ b/ethstaker_deposit/cli/generate_keys.py @@ -33,6 +33,7 @@ closest_match, load_text, ) +from ethstaker_deposit.utils.terminal import clear_terminal from ethstaker_deposit.settings import ( MAINNET, ALL_CHAIN_KEYS, @@ -140,7 +141,7 @@ def generate_keys(ctx: click.Context, validator_start_index: int, if not os.path.exists(folder): os.mkdir(folder) - click.clear() + clear_terminal() click.echo(RHINO_0) click.echo(load_text(['msg_key_creation'])) credentials = CredentialList.from_mnemonic( diff --git a/ethstaker_deposit/cli/new_mnemonic.py b/ethstaker_deposit/cli/new_mnemonic.py index 45dcbf61..5d9753d6 100644 --- a/ethstaker_deposit/cli/new_mnemonic.py +++ b/ethstaker_deposit/cli/new_mnemonic.py @@ -21,6 +21,7 @@ load_text, get_first_options, ) +from ethstaker_deposit.utils.terminal import clear_terminal from .generate_keys import ( generate_keys, @@ -50,14 +51,14 @@ def new_mnemonic(ctx: click.Context, mnemonic_language: str, **kwargs: Any) -> N mnemonic = get_mnemonic(language=mnemonic_language, words_path=WORD_LISTS_PATH) test_mnemonic = '' while mnemonic != reconstruct_mnemonic(test_mnemonic, WORD_LISTS_PATH): - click.clear() + clear_terminal() click.echo(load_text(['msg_mnemonic_presentation'])) click.echo('\n\n%s\n\n' % mnemonic) click.pause(load_text(['msg_press_any_key'])) - click.clear() + clear_terminal() test_mnemonic = click.prompt(load_text(['msg_mnemonic_retype_prompt']) + '\n\n') - click.clear() + clear_terminal() # Do NOT use mnemonic_password. ctx.obj = {'mnemonic': mnemonic, 'mnemonic_password': ''} ctx.params['validator_start_index'] = 0 diff --git a/ethstaker_deposit/utils/terminal.py b/ethstaker_deposit/utils/terminal.py new file mode 100644 index 00000000..f64febd0 --- /dev/null +++ b/ethstaker_deposit/utils/terminal.py @@ -0,0 +1,23 @@ +import subprocess +import sys +import shutil +import click + + +def clear_terminal() -> None: + if sys.platform == 'win32': + try: + subprocess.call('cls', shell=True) + except Exception: + click.clear() + elif sys.platform == 'linux' or sys.platform == 'darwin': + if shutil.which('tput'): + subprocess.call(['tput', 'reset']) + elif shutil.which('reset'): + subprocess.call(['reset']) + elif shutil.which('clear'): + subprocess.call(['clear']) + else: + click.clear() + else: + click.clear()