def run_cmd(global_opts: RCOptions, cmd: str, func: RemoteCommand, opts: Any, items: List[str]) -> None: from .remote_control import create_basic_command, do_io print(end=set_window_title(cmd) + output_prefix, flush=True) payload = func.message_to_kitty(global_opts, opts, items) no_response = func.no_response if hasattr(opts, 'no_response'): no_response = opts.no_response send = create_basic_command(cmd, payload=payload, is_asynchronous=func.is_asynchronous, no_response=no_response) response_timeout = func.response_timeout if hasattr(opts, 'response_timeout'): response_timeout = opts.response_timeout try: response = do_io(global_opts.to, send, no_response, response_timeout) except TimeoutError: send.pop('payload', None) send['cancel_async'] = True do_io(global_opts.to, send, True, 10) print_err( f'Timed out after {response_timeout} seconds waiting for response from kitty' ) return if not response.get('ok'): if response.get('tb'): print_err(response['tb']) print_err(response['error']) return if 'data' in response: print(response['data'])
def init_terminal_state(self): self.write(set_line_wrapping(False)) self.write(set_window_title(_('Search')))
def real_main(global_opts: RCOptions) -> None: init_readline() print_help_for_seq.allow_pager = False print('Welcome to the kitty shell!') print('Use {} for assistance or {} to quit'.format(green('help'), green('exit'))) awid = os.environ.pop('KITTY_SHELL_ACTIVE_WINDOW_ID', None) if awid is not None: print(f'The ID of the previously active window is: {awid}') pre_prompt = set_window_title('The kitty shell') + set_cursor_shape('bar') pre_prompt += f'\x1b]133;A;redraw={0 if is_libedit else 1}\x1b\\' while True: try: print(end=pre_prompt) try: scmdline = input(f'{kitty_face} ') except UnicodeEncodeError: scmdline = input('kitty> ') except EOFError: break except KeyboardInterrupt: print() continue print(end=set_cursor_shape(), flush=True) if not scmdline: continue try: cmdline = shlex.split(scmdline) except Exception: print_err( f'"{emph(scmdline)}" is invalid. Use "help" to see a list of commands.' ) continue cmd = cmdline[0].lower() try: func = command_for_name(cmd) except KeyError: if cmd in ('exit', 'quit'): break print(end=output_prefix, flush=True) if cmd == 'help': print_help(cmdline[1] if len(cmdline) > 1 else None) continue print_err( f'"{emph(cmd)}" is an unknown command. Use "help" to see a list of commands.' ) continue try: opts, items = parse_subcommand_cli(func, cmdline) except SystemExit as e: if e.code != 0: print(end=output_prefix, flush=True) print_err(e) print_err('Use "{}" to see how to use this command.'.format( emph('help ' + cmd))) continue except Exception: print(end=output_prefix, flush=True) print_err('Unhandled error:') traceback.print_exc() continue else: try: run_cmd(global_opts, cmd, func, opts, items) except (SystemExit, ParsingOfArgsFailed) as e: print(end=output_prefix, flush=True) print_err(e) continue except KeyboardInterrupt: print(end=output_prefix, flush=True) print() continue except Exception: print(end=output_prefix, flush=True) print_err('Unhandled error:') traceback.print_exc() continue