diff --git a/__main__.py b/__main__.py index 0148a6e9a..4070ab1a8 100644 --- a/__main__.py +++ b/__main__.py @@ -31,26 +31,9 @@ def runpy(args: List[str]) -> None: def hold(args: List[str]) -> None: import subprocess - import termios - from contextlib import suppress - from kittens.tui.operations import init_state, set_cursor_visible ret = subprocess.Popen(args[1:]).wait() - with suppress(BaseException): - print( - '\n\x1b[1;32mPress Enter to exit', - end=init_state(alternate_screen=False, kitty_keyboard_mode=False) + set_cursor_visible(False), - flush=True) - with suppress(BaseException): - fd = sys.stdin.fileno() - old = termios.tcgetattr(fd) - new = old[:] - new[3] &= ~termios.ECHO # 3 == 'lflags' - tcsetattr_flags = termios.TCSAFLUSH - if hasattr(termios, 'TCSASOFT'): - tcsetattr_flags |= getattr(termios, 'TCSASOFT') - termios.tcsetattr(fd, tcsetattr_flags, new) - with suppress(KeyboardInterrupt): - input() + from kitty.utils import hold_till_enter + hold_till_enter() raise SystemExit(ret) diff --git a/kitty/utils.py b/kitty/utils.py index 7e815c394..1e999a51a 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -852,3 +852,24 @@ def control_codes_pat() -> 'Pattern[str]': def sanitize_control_codes(text: str, replace_with: str = '') -> str: return control_codes_pat().sub(replace_with, text) + + +def hold_till_enter() -> None: + import termios + from kittens.tui.operations import init_state, set_cursor_visible + with suppress(BaseException): + print( + '\n\x1b[1;32mPress Enter to exit', + end=init_state(alternate_screen=False, kitty_keyboard_mode=False) + set_cursor_visible(False), + flush=True) + with suppress(BaseException): + fd = sys.stdin.fileno() + old = termios.tcgetattr(fd) + new = old[:] + new[3] &= ~termios.ECHO # 3 == 'lflags' + tcsetattr_flags = termios.TCSAFLUSH + if hasattr(termios, 'TCSASOFT'): + tcsetattr_flags |= getattr(termios, 'TCSASOFT') + termios.tcsetattr(fd, tcsetattr_flags, new) + with suppress(KeyboardInterrupt): + input()