diff --git a/docs/kittens/hyperlinked_grep.rst b/docs/kittens/hyperlinked_grep.rst index e6fdb2cca..ed305e30e 100644 --- a/docs/kittens/hyperlinked_grep.rst +++ b/docs/kittens/hyperlinked_grep.rst @@ -67,7 +67,7 @@ the need for this kitten. output formatting as the kitten works by parsing the output from ripgrep. The unsupported options are: :code:`--context-separator`, :code:`--field-context-separator`, :code:`--field-match-separator`, - :code:`--json`, :code:`-I --no-filename`, :code:`--no-heading`, - :code:`-0 --null`, :code:`--null-data`, :code:`--path-separator`. - If you specify options via configuration file, then any changes to the - default output format will not be supported, not just the ones listed above. + :code:`--json`, :code:`-I --no-filename`, :code:`-0 --null`, + :code:`--null-data`, :code:`--path-separator`. If you specify options via + configuration file, then any changes to the default output format will not be + supported, not just the ones listed. diff --git a/kittens/hyperlinked_grep/main.py b/kittens/hyperlinked_grep/main.py index 21cd5b9cd..439a71008 100755 --- a/kittens/hyperlinked_grep/main.py +++ b/kittens/hyperlinked_grep/main.py @@ -116,9 +116,6 @@ def main() -> None: raise SystemExit('Could not find the rg executable in your PATH. Is ripgrep installed?') assert p.stdout is not None - def get_quoted_path(x: bytes) -> bytes: - return quote_from_bytes(os.path.abspath(x)).encode('utf-8') - write: Callable[[bytes], None] = cast(Callable[[bytes], None], sys.stdout.buffer.write) sgr_pat = re.compile(br'\x1b\[.*?m') osc_pat = re.compile(b'\x1b\\].*?\x1b\\\\') @@ -132,6 +129,9 @@ def main() -> None: in_result: bytes = b'' hostname = get_hostname().encode('utf-8') + def get_quoted_url(file_path: bytes) -> bytes: + return b'file://' + hostname + quote_from_bytes(os.path.abspath(file_path)).encode('utf-8') + try: for line in p.stdout: line = osc_pat.sub(b'', line) # remove any existing hyperlinks @@ -158,20 +158,20 @@ def main() -> None: elif args.count or args.count_matches: m = path_with_count_pat.match(clean_line) if m is not None and link_file_headers: - write_hyperlink(write, b'file://' + hostname + get_quoted_path(m.group(1)), line) + write_hyperlink(write, get_quoted_url(m.group(1)), line) continue elif args.files or args.files_with_matches or args.files_without_match: if link_file_headers: - write_hyperlink(write, get_quoted_path(clean_line), line) + write_hyperlink(write, get_quoted_url(clean_line), line) continue elif args.vimgrep or args.no_heading: # When the vimgrep option is present, it will take precedence. m = vimgrep_pat.match(clean_line) if args.vimgrep else path_with_linenum_pat.match(clean_line) if m is not None and (link_file_headers or link_matching_lines): - write_hyperlink(write, b'file://' + hostname + get_quoted_path(m.group(1)), line, frag=m.group(2)) + write_hyperlink(write, get_quoted_url(m.group(1)), line, frag=m.group(2)) continue else: - in_result = b'file://' + hostname + get_quoted_path(clean_line) + in_result = get_quoted_url(clean_line) if link_file_headers: write_hyperlink(write, in_result, line) continue