From fc17528337c12f4c88dfbc37e8caa52a81e30d06 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 7 Feb 2022 15:39:21 +0530 Subject: [PATCH] Get --hold to work even when stdin is redirected --- kitty/utils.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/kitty/utils.py b/kitty/utils.py index 5d4cd13e9..5b21b468d 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -860,13 +860,15 @@ def sanitize_control_codes(text: str, replace_with: str = '') -> str: 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() + term = os.ctermid() or '/dev/tty' + with open(term, 'w') as tty: + 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, file=tty) + with suppress(BaseException), open(term, 'rb') as inp: + fd = inp.fileno() old = termios.tcgetattr(fd) new = old[:] new[3] &= ~termios.ECHO # 3 == 'lflags' @@ -875,4 +877,5 @@ def hold_till_enter() -> None: tcsetattr_flags |= getattr(termios, 'TCSASOFT') termios.tcsetattr(fd, tcsetattr_flags, new) with suppress(KeyboardInterrupt): - input() + while inp.read(1) not in b'\n\r': + pass