Cleanup previous PR

Allow disabling hyperlinking of rg output. Also parse
--kitten=hyperlink=... form of CLI args.
This commit is contained in:
Kovid Goyal 2022-08-30 07:41:14 +05:30
parent 49f8c0eae6
commit 89a2545855
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 45 additions and 18 deletions

View File

@ -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]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -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 </open_actions>`.
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
<https://github.com/BurntSushi/ripgrep/issues/665>`__ program directly removing

View File

@ -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: