Avoid unnecessary CPU churn when reading from stdin for @ send-text

This commit is contained in:
Kovid Goyal
2021-10-31 14:04:07 +05:30
parent 6310517ff2
commit b1c0398bba
2 changed files with 14 additions and 2 deletions

View File

@@ -69,9 +69,13 @@ Do not send text to the active window, even if it is one of the matched windows.
ret['exclude_active'] = True
keep_going = True
from kitty.utils import TTYIO
with TTYIO() as tty:
with TTYIO(read_with_timeout=False) as tty:
while keep_going:
if not tty.wait_till_read_available():
break
data = os.read(tty.tty_fd, limit)
if not data:
break
decoded_data = data.decode('utf-8')
if '\x04' in decoded_data:
decoded_data = decoded_data[:decoded_data.index('\x04')]

View File

@@ -442,15 +442,23 @@ def write_all(fd: int, data: Union[str, bytes]) -> None:
class TTYIO:
def __init__(self, read_with_timeout: bool = True):
self.read_with_timeout = read_with_timeout
def __enter__(self) -> 'TTYIO':
from .fast_data_types import open_tty
self.tty_fd, self.original_termios = open_tty(True)
self.tty_fd, self.original_termios = open_tty(self.read_with_timeout)
return self
def __exit__(self, *a: Any) -> None:
from .fast_data_types import close_tty
close_tty(self.tty_fd, self.original_termios)
def wait_till_read_available(self) -> bool:
import select
rd = select.select([self.tty_fd], [], [])[0]
return bool(rd)
def send(self, data: Union[str, bytes, Iterable[Union[str, bytes]]]) -> None:
if isinstance(data, (str, bytes)):
write_all(self.tty_fd, data)