Make hold_till_enter re-useable

This commit is contained in:
Kovid Goyal 2022-01-07 12:13:20 +05:30
parent 04807453ec
commit a155b081b7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 23 additions and 19 deletions

View File

@ -31,26 +31,9 @@ def runpy(args: List[str]) -> None:
def hold(args: List[str]) -> None: def hold(args: List[str]) -> None:
import subprocess import subprocess
import termios
from contextlib import suppress
from kittens.tui.operations import init_state, set_cursor_visible
ret = subprocess.Popen(args[1:]).wait() ret = subprocess.Popen(args[1:]).wait()
with suppress(BaseException): from kitty.utils import hold_till_enter
print( hold_till_enter()
'\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()
raise SystemExit(ret) raise SystemExit(ret)

View File

@ -852,3 +852,24 @@ def control_codes_pat() -> 'Pattern[str]':
def sanitize_control_codes(text: str, replace_with: str = '') -> str: def sanitize_control_codes(text: str, replace_with: str = '') -> str:
return control_codes_pat().sub(replace_with, text) 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()