Allow pressing esc to exit --hold as well

This commit is contained in:
Kovid Goyal 2022-02-07 18:14:54 +05:30
parent fc17528337
commit c54bdd921a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -21,10 +21,10 @@ from .constants import (
appname, config_dir, is_macos, is_wayland, read_kitty_resource, shell_path, appname, config_dir, is_macos, is_wayland, read_kitty_resource, shell_path,
supports_primary_selection supports_primary_selection
) )
from .fast_data_types import Color, open_tty
from .rgb import to_color from .rgb import to_color
from .types import run_once from .types import run_once
from .typing import AddressFamily, PopenType, Socket, StartupCtx from .typing import AddressFamily, PopenType, Socket, StartupCtx
from .fast_data_types import Color
if TYPE_CHECKING: if TYPE_CHECKING:
from .fast_data_types import OSWindowSize from .fast_data_types import OSWindowSize
@ -459,7 +459,6 @@ class TTYIO:
self.read_with_timeout = read_with_timeout self.read_with_timeout = read_with_timeout
def __enter__(self) -> 'TTYIO': def __enter__(self) -> 'TTYIO':
from .fast_data_types import open_tty
self.tty_fd, self.original_termios = open_tty(self.read_with_timeout) self.tty_fd, self.original_termios = open_tty(self.read_with_timeout)
return self return self
@ -654,6 +653,7 @@ def which(name: str, only_system: bool = False) -> Optional[str]:
if os.sep in name: if os.sep in name:
return name return name
import shutil import shutil
from .fast_data_types import get_options from .fast_data_types import get_options
opts: Optional[Options] = None opts: Optional[Options] = None
with suppress(RuntimeError): with suppress(RuntimeError):
@ -837,6 +837,7 @@ def is_kitty_gui_cmdline(*cmd: str) -> bool:
def reload_conf_in_all_kitties() -> None: def reload_conf_in_all_kitties() -> None:
import signal import signal
from kitty.child import cmdline_of_process from kitty.child import cmdline_of_process
for pid in get_all_processes(): for pid in get_all_processes():
@ -858,24 +859,25 @@ def sanitize_control_codes(text: str, replace_with: str = '') -> str:
def hold_till_enter() -> None: def hold_till_enter() -> None:
import select
import termios import termios
from kittens.tui.operations import init_state, set_cursor_visible from kittens.tui.operations import init_state, set_cursor_visible
term = os.ctermid() or '/dev/tty' fd, original_termios = open_tty()
with open(term, 'w') as tty: write_all(fd, '\n\x1b[1;32mPress Enter or Esc to exit')
with suppress(BaseException): write_all(fd, init_state(alternate_screen=False, kitty_keyboard_mode=False) + set_cursor_visible(False))
print( old = termios.tcgetattr(fd)
'\n\x1b[1;32mPress Enter to exit', new = old[:]
end=init_state(alternate_screen=False, kitty_keyboard_mode=False) + set_cursor_visible(False), new[3] &= ~termios.ECHO # 3 == 'lflags'
flush=True, file=tty) tcsetattr_flags = termios.TCSAFLUSH
with suppress(BaseException), open(term, 'rb') as inp: if hasattr(termios, 'TCSASOFT'):
fd = inp.fileno() tcsetattr_flags |= getattr(termios, 'TCSASOFT')
old = termios.tcgetattr(fd) termios.tcsetattr(fd, tcsetattr_flags, new)
new = old[:] termios.tcdrain(fd)
new[3] &= ~termios.ECHO # 3 == 'lflags' while True:
tcsetattr_flags = termios.TCSAFLUSH rd = select.select([fd], [], [])[0]
if hasattr(termios, 'TCSASOFT'): if not rd:
tcsetattr_flags |= getattr(termios, 'TCSASOFT') break
termios.tcsetattr(fd, tcsetattr_flags, new) q = os.read(fd, 1)
with suppress(KeyboardInterrupt): if q in b'\n\r\x1b\x03':
while inp.read(1) not in b'\n\r': break
pass