def main(args: List[str]) -> Response: # For some reason importing readline in a key handler in the main kitty process # causes a crash of the python interpreter, probably because of some global # lock global readline msg = 'Ask the user for input' try: cli_opts, items = parse_args(args[1:], option_text, '', msg, 'kitty ask', result_class=AskCLIOptions) except SystemExit as e: if e.code != 0: print(e.args[0]) input('Press enter to quit...') raise SystemExit(e.code) if cli_opts.type in ('yesno', 'choices'): loop = Loop() handler = Choose(cli_opts) loop.loop(handler) return {'items': items, 'response': handler.response} import readline as rl readline = rl from kitty.shell import init_readline init_readline() response = None with alternate_screen(), HistoryCompleter(cli_opts.name): if cli_opts.message: print(styled(cli_opts.message, bold=True)) prompt = '> ' with suppress(KeyboardInterrupt, EOFError): response = input(prompt) return {'items': items, 'response': response}
def parse_unicode_input_args(args): return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten unicode_input', result_class=UnicodeCLIOptions)
def parse_hints_args(args): return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten hints', result_class=HintsCLIOptions)
def main(args): warnings.showwarning = showwarning msg = 'Show a side-by-side diff of the specified files/directories' args, items = parse_args(args[1:], OPTIONS, 'file_or_directory file_or_directory', msg, 'kitty +kitten diff') if len(items) != 2: raise SystemExit( 'You must specify exactly two files/directories to compare') left, right = items if os.path.isdir(left) != os.path.isdir(right): raise SystemExit( 'The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.' ) opts = init_config(args) set_diff_command(opts.diff_cmd) loop = Loop() handler = DiffHandler(args, opts, left, right) loop.loop(handler) for message in showwarning.warnings: from kitty.utils import safe_print safe_print(message, file=sys.stderr) if loop.return_code != 0: if handler.report_traceback_on_exit: print(handler.report_traceback_on_exit, file=sys.stderr) raise SystemExit(loop.return_code)
def main(args: List[str]) -> Result: msg = 'Ask the user what to do with the remote file' try: cli_opts, items = parse_args(args[1:], option_text, '', msg, 'kitty remote_file', result_class=RemoteFileCLIOptions) except SystemExit as e: if e.code != 0: print(e.args[0]) input('Press enter to quit...') raise SystemExit(e.code) try: action = ask_action(cli_opts) finally: print(reset_terminal(), end='', flush=True) try: return handle_action(action, cli_opts) except Exception: print(reset_terminal(), end='', flush=True) import traceback traceback.print_exc() show_error('Failed with unhandled exception') return None
def main(args): warnings.showwarning = showwarning args, items = parse_args(args[1:], OPTIONS, usage, help_text, 'kitty +kitten diff') if len(items) != 2: raise SystemExit( 'You must specify exactly two files/directories to compare') left, right = items main.title = _('{} vs. {}').format(left, right) if os.path.isdir(left) != os.path.isdir(right): raise SystemExit( 'The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.' ) opts = init_config(args) set_diff_command(opts.diff_cmd) lines_for_path.replace_tab_by = opts.replace_tab_by loop = Loop() handler = DiffHandler(args, opts, left, right) loop.loop(handler) for message in showwarning.warnings: from kitty.utils import safe_print safe_print(message, file=sys.stderr) if loop.return_code != 0: if handler.report_traceback_on_exit: print(handler.report_traceback_on_exit, file=sys.stderr) input('Press Enter to quit.') raise SystemExit(loop.return_code)
def parse_hints_args(args: List[str]) -> Tuple[HintsCLIOptions, List[str]]: return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten hints', result_class=HintsCLIOptions)
def main(args: List[str]) -> Optional['ResultDict']: def ospec(): return ''' --cursor-x dest=x type=int (Internal) Starting cursor column, 0-based. --cursor-y dest=y type=int (Internal) Starting cursor line, 0-based. --top-line dest=top_line type=int (Internal) Window scroll offset, 1-based. --title (Internal)''' args, _rest = parse_args(args[1:], ospec) tty = open(os.ctermid()) lines = (sys.stdin.buffer.read().decode('utf-8').split('\n')[:-1] ) # last line ends with \n, too sys.stdin = tty opts = parse_opts() handler = GrabHandler(args, opts, lines) loop = Loop() loop.loop(handler) return handler.result
def parse_panel_args(args: List[str]) -> Tuple[PanelCLIOptions, List[str]]: return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten panel', result_class=PanelCLIOptions)
def main(args: List[str]) -> None: warnings.showwarning = showwarning cli_opts, items = parse_args(args[1:], OPTIONS, usage, help_text, 'kitty +kitten diff', result_class=DiffCLIOptions) if len(items) != 2: raise SystemExit('You must specify exactly two files/directories to compare') left, right = items global_data.title = _('{} vs. {}').format(left, right) if os.path.isdir(left) != os.path.isdir(right): raise SystemExit('The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.') opts = init_config(cli_opts) set_diff_command(opts.diff_cmd) lines_for_path.replace_tab_by = opts.replace_tab_by left, right = map(get_remote_file, (left, right)) for f in left, right: if not os.path.exists(f): raise SystemExit('{} does not exist'.format(f)) loop = Loop() handler = DiffHandler(cli_opts, opts, left, right) loop.loop(handler) for message in showwarning.warnings: from kitty.utils import safe_print safe_print(message, file=sys.stderr) highlight_processes = getattr(highlight_collection, 'processes', ()) terminate_processes(tuple(highlight_processes)) terminate_processes(tuple(worker_processes)) if loop.return_code != 0: if handler.report_traceback_on_exit: print(handler.report_traceback_on_exit, file=sys.stderr) input('Press Enter to quit.') raise SystemExit(loop.return_code)
def parse_subcommand_cli(command: RemoteCommand, args: ArgsType) -> Tuple[Any, ArgsType]: opts, items = parse_args(args[1:], *cli_params_for(command), result_class=command.options_class) if command.args_count is not None and command.args_count != len(items): if command.args_count == 0: raise SystemExit('Unknown extra argument(s) supplied to {}'.format(command.name)) raise SystemExit('Must specify exactly {} argument(s) for {}'.format(command.args_count, command.name)) return opts, items
def parse_themes_args(args: List[str]) -> Tuple[ThemesCLIOptions, List[str]]: return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten themes', result_class=ThemesCLIOptions)
def parse_launch_args(args=None): args = list(args or ()) try: opts, args = parse_args(args=args, ospec=options_spec) except SystemExit as e: raise ValueError from e return opts, args
def main(args): msg = 'Select text from the screen using the keyboard. Defaults to searching for URLs.' text = '' if sys.stdin.isatty(): if '--help' not in args and '-h' not in args: print('You must pass the text to be hinted on STDIN', file=sys.stderr) input(_('Press Enter to quit')) return else: text = sys.stdin.buffer.read().decode('utf-8') sys.stdin = open('/dev/tty') try: args, items = parse_args(args[1:], OPTIONS, '', msg, 'hints') except SystemExit as e: if e.code != 0: print(e.args[0], file=sys.stderr) input(_('Press Enter to quit')) return if items: print('Extra command line arguments present: {}'.format( ' '.join(items)), file=sys.stderr) input(_('Press Enter to quit')) return return run(args, text)
def main(args): # For some reason importing readline in a key handler in the main kitty process # causes a crash of the python interpreter, probably because of some global # lock global readline import readline as rl readline = rl from kitty.shell import init_readline msg = 'Ask the user for input' try: args, items = parse_args(args[1:], option_text, '', msg, 'kitty ask', result_class=AskCLIOptions) except SystemExit as e: if e.code != 0: print(e.args[0]) input('Press enter to quit...') raise SystemExit(e.code) init_readline(readline) ans = {'items': items} with alternate_screen(), HistoryCompleter(args.name): if args.message: print(styled(args.message, bold=True)) prompt = '> ' with suppress(KeyboardInterrupt, EOFError): ans['response'] = input(prompt) return ans
def parse_panel_args(args): return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten panel', result_class=PanelCLIOptions)
def parse_broadcast_args( args: List[str]) -> Tuple[BroadcastCLIOptions, List[str]]: return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten broadcast', result_class=BroadcastCLIOptions)
def parse_transfer_args( args: List[str]) -> Tuple[TransferCLIOptions, List[str]]: return parse_args(args[1:], option_text, usage, help_text, 'kitty +kitten transfer', result_class=TransferCLIOptions)
def parse_unicode_input_args( args: List[str]) -> Tuple[UnicodeCLIOptions, List[str]]: return parse_args(args, OPTIONS, usage, help_text, 'kitty +kitten unicode_input', result_class=UnicodeCLIOptions)
def main(args=sys.argv): msg = 'Highlight URLs inside the specified text' try: args, items = parse_args(args[1:], OPTIONS, '[path to file or omit to use stdin]', msg, 'url_hints') except SystemExit as e: print(e.args[0], file=sys.stderr) input('Press enter to quit...') return 1 run(args, (items or [None])[0])
def real_main(args): error_message = sys.stdin.buffer.read().decode('utf-8') sys.stdin = open(os.ctermid()) msg = 'Show an error message' args, items = parse_args(args, OPTIONS, '', msg, 'hints') print(styled(args.title, fg_intense=True, fg='red', bold=True)) print() print(error_message) print() input('Press Enter to close.')
def parse_copy_args( args: Optional[Sequence[str]] = None ) -> Tuple[CopyCLIOptions, List[str]]: args = list(args or ()) try: opts, args = parse_args(result_class=CopyCLIOptions, args=args, ospec=option_text) except SystemExit as e: raise CopyCLIError from e return opts, args
def main(args: List[str]) -> None: cli_opts, items = parse_args(args[1:], OPTIONS, '', '', 'kitty +kitten clipboard', result_class=ShowKeyCLIOptions) if cli_opts.key_mode == 'kitty': from .kitty_mode import main as kitty_main return kitty_main() if cli_opts.key_mode != 'unchanged': print(end='\x1b[?1' + ('l' if cli_opts.key_mode == 'normal' else 'h'), flush=True) try: return legacy_main() finally: if cli_opts.key_mode != 'unchanged': print(end='\x1b[?1l', flush=True)
def main(args: List[str]) -> None: cli_opts, items = parse_args(args[1:], option_text, '', 'Transfer files over the TTY device', 'kitty transfer', result_class=TransferCLIOptions) if not items: raise SystemExit('Usage: kitty +kitten transfer file_or_directory ...') if cli_opts.direction == 'send': send_main(cli_opts, items) return
def main(args: List[str]) -> Response: # For some reason importing readline in a key handler in the main kitty process # causes a crash of the python interpreter, probably because of some global # lock global readline msg = 'Ask the user for input' try: cli_opts, items = parse_args(args[1:], option_text, '', msg, 'kitty ask', result_class=AskCLIOptions) except SystemExit as e: if e.code != 0: print(e.args[0]) input('Press Enter to quit') raise SystemExit(e.code) if cli_opts.type in ('yesno', 'choices'): loop = Loop() handler = Choose(cli_opts) loop.loop(handler) return {'items': items, 'response': handler.response} prompt = cli_opts.prompt if prompt[0] == prompt[-1] and prompt[0] in '\'"': prompt = prompt[1:-1] if cli_opts.type == 'password': loop = Loop() phandler = Password(cli_opts, prompt) loop.loop(phandler) return {'items': items, 'response': phandler.response} import readline as rl readline = rl from kitty.shell import init_readline init_readline() response = None with alternate_screen(), HistoryCompleter(cli_opts.name): if cli_opts.message: print(styled(cli_opts.message, bold=True)) with suppress(KeyboardInterrupt, EOFError): if cli_opts.default: def prefill_text() -> None: readline.insert_text(cli_opts.default or '') readline.redisplay() readline.set_pre_input_hook(prefill_text) response = input(prompt) readline.set_pre_input_hook() else: response = input(prompt) return {'items': items, 'response': response}
def main(args=sys.argv): msg = 'Highlight URLs inside the specified text' try: args, items = parse_args(args[1:], OPTIONS, '[path to file or omit to use stdin]', msg, 'url_hints') except SystemExit as e: print(e.args[0], file=sys.stderr) input(_('Press Enter to quit')) return 1 try: run(args, (items or [None])[0]) except Exception: import traceback traceback.print_exc() input(_('Press Enter to quit'))
def real_main(args: List[str]) -> None: msg = 'Show an error message' cli_opts, items = parse_args(args[1:], OPTIONS, '', msg, 'hints', result_class=ErrorCLIOptions) error_message = sys.stdin.buffer.read().decode('utf-8') sys.stdin = open(os.ctermid()) print(styled(cli_opts.title, fg_intense=True, fg='red', bold=True)) print() print(error_message) print() input('Press Enter to close.')
def main(args: List[str]) -> NoReturn: cli_opts, items = parse_args(args[1:], OPTIONS, usage, help_text, 'kitty +kitten clipboard', result_class=ClipboardCLIOptions) if items: raise SystemExit('Unrecognized extra command line arguments') data: Optional[bytes] = None if not sys.stdin.isatty(): data = sys.stdin.buffer.read() sys.stdin = open(os.ctermid()) loop = Loop() handler = Clipboard(data, cli_opts) loop.loop(handler) if loop.return_code == 0 and handler.clipboard_contents: sys.stdout.write(handler.clipboard_contents) sys.stdout.flush() raise SystemExit(loop.return_code)
def main(args): args, items = parse_args(args[1:], OPTIONS, usage, help_text, 'kitty +kitten clipboard') if items: raise SystemExit('Unrecognized extra command line arguments') data = None if not sys.stdin.isatty(): data = sys.stdin.buffer.read() sys.stdin = open(os.ctermid(), 'r') loop = Loop() handler = Clipboard(data, args) loop.loop(handler) if loop.return_code == 0 and handler.clipboard_contents: sys.stdout.write(handler.clipboard_contents) sys.stdout.flush() raise SystemExit(loop.return_code)
def main(args): msg = 'Resize the current window' try: args, items = parse_args(args[1:], OPTIONS, '', msg, 'resize_window') except SystemExit as e: if e.code != 0: print(e.args[0], file=sys.stderr) input('Press Enter to quit') return loop = Loop() handler = Resize(args) loop.loop(handler) if handler.print_on_fail: print(handler.print_on_fail, file=sys.stderr) input('Press Enter to quit') raise SystemExit(loop.return_code)