diff --git a/docs/changelog.rst b/docs/changelog.rst index faae56280..46fc69296 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -35,6 +35,12 @@ mouse anywhere in the current command to move the cursor there. See Detailed list of changes ------------------------------------- +0.26.2 [2022-08-30] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- hyperlinked_grep kitten: Allow control which parts of rg output are hyperlinked (:pull:`5428`) + +- ssh kitten: Fix executable permission missing from kitty bootstrap script (:iss:`5438`) 0.26.1 [2022-08-30] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/kittens/hyperlinked_grep.rst b/docs/kittens/hyperlinked_grep.rst index 8287de93d..cf79e398e 100644 --- a/docs/kittens/hyperlinked_grep.rst +++ b/docs/kittens/hyperlinked_grep.rst @@ -74,13 +74,15 @@ to :program:`rg`. How to do that varies based on the shell: To learn more about kitty's powerful framework for customizing URL click actions, see :doc:`here `. -By default, this adds hyperlinks for several parts of ripgrep output: the -per-file header, match context lines, and match lines. You can control which -items are linked with a :command:`--kitten hyperlink` flag. For example, +By default, this kitten adds hyperlinks for several parts of ripgrep output: +the per-file header, match context lines, and match lines. You can control +which items are linked with a :command:`--kitten hyperlink` flag. For example, :command:`--kitten hyperlink=matching_lines` will only add hyperlinks to the -match lines. :command:`--kitten hyperlink=file_headers,context_lines` will -link file headers and context lines but not match lines. -:command:`--kitten hyperlink` may be specified multiple times. +match lines. :command:`--kitten hyperlink=file_headers,context_lines` will link +file headers and context lines but not match lines. :command:`--kitten +hyperlink=none` will cause the command line to be passed to directly to +:command:`rg` so no hyperlinking will be performed. :command:`--kitten +hyperlink` may be specified multiple times. Hopefully, someday this functionality will make it into some `upstream grep `__ program directly removing diff --git a/kittens/hyperlinked_grep/main.py b/kittens/hyperlinked_grep/main.py index dc787cae2..fb28fafd0 100755 --- a/kittens/hyperlinked_grep/main.py +++ b/kittens/hyperlinked_grep/main.py @@ -21,29 +21,48 @@ def write_hyperlink(write: Callable[[bytes], None], url: bytes, line: bytes, fra def main() -> None: i = 1 - all_link_options = ['matching_lines', 'context_lines', 'file_headers'] + all_link_options = {'matching_lines', 'context_lines', 'file_headers'} link_options = set() + delegate_to_rg = False + + def parse_link_options(raw: str) -> None: + nonlocal delegate_to_rg + if not raw: + raise SystemExit('Must specify an argument for --kitten option') + p, _, s = raw.partition('=') + if p != 'hyperlink': + raise SystemExit(f'Unknown argument for --kitten: {raw}') + for option in s.split(','): + if option == 'all': + link_options.update(all_link_options) + delegate_to_rg = False + elif option == 'none': + delegate_to_rg = True + link_options.clear() + elif option not in all_link_options: + a = ', '.join(sorted(all_link_options)) + raise SystemExit(f"hyperlink option must be one of all, none, {a}, not '{option}'") + else: + link_options.add(option) + delegate_to_rg = False + while i < len(sys.argv): if sys.argv[i] == '--kitten': - if len(sys.argv) < i + 2 or not sys.argv[i + 1].startswith("hyperlink="): - raise SystemExit("--kitten argument must be followed by hyperlink=(all|matching_lines|context_lines|file_headers)") - for option in sys.argv[i + 1].split('=')[1].split(','): - if option == 'all': - link_options.update(all_link_options) - elif option not in all_link_options: - raise SystemExit(f"hyperlink option must be one of all, matching_lines, context_lines, or file_headers, not '{option}'") - else: - link_options.add(option) + next_item = '' if i + 1 >= len(sys.argv) else sys.argv[i + 1] + parse_link_options(next_item) del sys.argv[i:i+2] + elif sys.argv[i].startswith('--kitten='): + parse_link_options(sys.argv[i][len('--kitten='):]) + del sys.argv[i] else: i += 1 - if len(link_options) == 0: # Default to linking everything if no options given + if not link_options: # Default to linking everything if no options given link_options.update(all_link_options) link_file_headers = 'file_headers' in link_options link_context_lines = 'context_lines' in link_options link_matching_lines = 'matching_lines' in link_options - if not sys.stdout.isatty() and '--pretty' not in sys.argv and '-p' not in sys.argv: + if delegate_to_rg or (not sys.stdout.isatty() and '--pretty' not in sys.argv and '-p' not in sys.argv): os.execlp('rg', 'rg', *sys.argv[1:]) cmdline = ['rg', '--pretty', '--with-filename'] + sys.argv[1:] try: