def pipe() -> CmdGenerator: if sys.stdin.isatty(): ret['exclude_active'] = True keep_going = True from kitty.utils import TTYIO with TTYIO(read_with_timeout=False) as tty: while keep_going: if not tty.wait_till_read_available(): break data = tty.read(limit) if not data: break decoded_data = data.decode('utf-8') if '\x04' in decoded_data: decoded_data = decoded_data[:decoded_data. index('\x04')] keep_going = False ret['data'] = f'text:{decoded_data}' yield ret else: while True: data = sys.stdin.buffer.read(limit) if not data: break ret['data'] = f'base64:{base64.standard_b64encode(data).decode("ascii")}' yield ret
def detect_support(wait_for=10, silent=False): if not silent: print('Checking for graphics ({}s max. wait)...'.format(wait_for), end='\r') sys.stdout.flush() try: received = b'' responses = {} def parse_responses(): for m in re.finditer(b'\033_Gi=([1|2]);(.+?)\033\\\\', received): iid = m.group(1) if iid in (b'1', b'2'): iid = int(iid.decode('ascii')) if iid not in responses: responses[iid] = m.group(2) == b'OK' def more_needed(data): nonlocal received received += data parse_responses() return 1 not in responses or 2 not in responses with NamedTemporaryFile() as f: f.write(b'abcd'), f.flush() write_gr_cmd(dict(a='q', s=1, v=1, i=1), standard_b64encode(b'abcd')) write_gr_cmd(dict(a='q', s=1, v=1, i=2, t='f'), standard_b64encode(f.name.encode(fsenc))) with TTYIO() as io: io.recv(more_needed, timeout=float(wait_for)) finally: if not silent: sys.stdout.buffer.write(b'\033[J'), sys.stdout.flush() detect_support.has_files = bool(responses.get(2)) return responses.get(1, False)
def do_queries(queries: Iterable[str], cli_opts: QueryTerminalCLIOptions) -> Dict[str, str]: actions = tuple(all_queries[x]() for x in queries) qstring = ''.join(a.query_code() for a in actions) received = b'' pat = re.compile(rb'\x1b\[\?.+?c') def more_needed(data: bytes) -> bool: nonlocal received received += data has_da1_response = pat.search(received) is not None if has_da1_response: return False for a in actions: if a.more_needed(received): return True return has_da1_response with TTYIO() as ttyio: ttyio.send(qstring) ttyio.send( '\x1b[c') # DA1 query https://vt100.net/docs/vt510-rm/DA1.html ttyio.recv(more_needed, timeout=cli_opts.wait_for) return {a.name: a.output_line() for a in actions}
def do_queries(queries: Iterable, cli_opts: QueryTerminalCLIOptions) -> Dict[str, str]: actions = tuple(all_queries[x]() for x in queries) qstring = ''.join(a.query_code() for a in actions) received = b'' def more_needed(data: bytes) -> bool: nonlocal received received += data for a in actions: if a.more_needed(received): return True return False with TTYIO() as ttyio: ttyio.send(qstring) ttyio.recv(more_needed, timeout=cli_opts.wait_for) return {a.name: a.output_line() for a in actions}
def detect_support(wait_for: float = 10, silent: bool = False) -> bool: global can_transfer_with_files if not silent: print(f'Checking for graphics ({wait_for}s max. wait)...', end='\r') sys.stdout.flush() try: received = b'' responses: Dict[int, bool] = {} def parse_responses() -> None: for m in re.finditer(b'\033_Gi=([1|2]);(.+?)\033\\\\', received): iid = m.group(1) if iid in (b'1', b'2'): iid_ = int(iid.decode('ascii')) if iid_ not in responses: responses[iid_] = m.group(2) == b'OK' def more_needed(data: bytes) -> bool: nonlocal received received += data parse_responses() return 1 not in responses or 2 not in responses with NamedTemporaryFile() as f: f.write(b'abcd') f.flush() gc = GraphicsCommand() gc.a = 'q' gc.s = gc.v = gc.i = 1 write_gr_cmd(gc, standard_b64encode(b'abcd')) gc.t = 'f' gc.i = 2 write_gr_cmd(gc, standard_b64encode(f.name.encode(fsenc))) with TTYIO() as io: io.recv(more_needed, timeout=wait_for) finally: if not silent: sys.stdout.buffer.write(b'\033[J'), sys.stdout.flush() can_transfer_with_files = bool(responses.get(2)) return responses.get(1, False)