Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Enable OSC8 hyperlinks for line numbers or highlighted line numbers #2158

Open
zachriggle opened this issue Apr 16, 2022 · 5 comments
Labels
feature-request New feature or request

Comments

@zachriggle
Copy link

zachriggle commented Apr 16, 2022

While iTerm may have been the first, lots of terminals now support OSC8 hyperlinks. This is a custom escape handler that lets text be clickable hypertext, effectively.

Supporting this would only mean adding some escape characters and a URI to each line number that's printed, to allow them to be ⌘-Clicked to open in the editor of choice.

Most of this is irrelevant if using a non-GUI editor (vim, emacs, etc) but relevant on at least macOS and likely other OSes if they support custom URI handlers.

Proposal

I propose the following additional flags for bat:

  • --osc8 will cause every line number to be a clickable link
  • --osc8-highlight will be the same, but only for highlighted lines
  • --osc8-scheme is an optional flag, defaults to file://, which informs Bat as to which URI to print
    • For example, one may have a custom URI handler per-file
  • If line numbers are not displayed with the current bat options, none of the above have any effect

For completeness, I do not need this as a feature (iTerm2 lets you ⌘-Click paths to open them automagically) but I may as well suggest it.

  • --osc8 will cause every file name to be a clickable file:// link

Technical Details

I'm not a Rustacean yet, but here's a function that creates OCS8 links in Python.

def osc8_link(url, text=None):
    """Emits an OSC8-compatible hyperlink"""
    if not text:
        text=url

    prefix='\x1b]8;;'
    separator='\x07'
    suffix='\x1b]8;;\x07'
    return f'{prefix}{url}{separator}{text}{suffix}'

Sponsorship
Not that this grants me any special treatment, but I'm a sponsor for all of the maintainers of bat ❤️💵

@zachriggle zachriggle added the feature-request New feature or request label Apr 16, 2022
@keith-hall
Copy link
Collaborator

Thanks for the suggestion, it sounds interesting :)

I tried your Python script on Linux Mint with xfce4-terminal v0.8.10 out of curiosity to see if it worked, and it just displayed the text without any clickable hyperlink. So I hope it's safe to say that, if we do implement this, there shouldn't be any problems with it on terminal emulators which don't support this feature. (Note, for reference, we know that xfce4-terminal supports clickable links in general, see #2149 (comment))

I have a few questions, if I may. For a clickable line number, what should the URI look like (assuming the default file:/// scheme) to be able to open the file at that specific line? I know that for Sublime Text, using subl file:line on the command line would open the file at the given line number. What are the chances that all GUI text editors would work that way? And how should it be customizable if the user's preferred GUI text editor uses a custom URI scheme?

We would also need to consider how it would interact with when bat reads from stdin. I guess if --file-name is not provided, the --osc8 args would be ignored. If it is provided, would it be correct for bat to create hyperlinks for it?

P.S. Thanks for the sponsorship, glad you find our work on bat useful :)

@zachriggle
Copy link
Author

Unfortunately the OSC8 URIs do not support launching arbitrary commands.

macOS does allow easy and arbitrary registration of URI scheme handlers, e.g.:

bundle id:                  Terminal (0x127ac)
claimed schemes:            ssh:, telnet:, x-man-page:

So you can x-man-page://memcpy. I believe people have already written scheme handlers for Sublime Text, thus the request for --osc8-scheme instead of defaulting to file://.

Unfortunately, file:///path/to/file.c does not generally permit line numbers.

Regarding stdin, I think it's fine to require --file-name for the OSC8 flags to matter.

@lsoksane
Copy link

lsoksane commented Feb 5, 2023

I write to support this request. I would be happy with the file name in the header being an OSC8 hyperlink. This would also serve as a test case for the suggested line number feature, and the usual file:/// scheme would do the job.

@eth-p
Copy link
Collaborator

eth-p commented Apr 16, 2023

Huh, interesting. This seems like a pretty useful feature! I might take a crack at it once my finals are over for the semester if I have the time :)

@llimllib
Copy link

Some terminals and editors do support file: URLs with line numbers, but unfortunately there's no standard. ripgrep's support is currently top-class; here's how it defines options for file URLs

I'd also note that file: URLs are useful for editors in the terminal if you use a terminal that supports it! Here's what it looks like in kitty for me, using rg and opening up a match in vim to a particular line by clicking on the line number in the result:

Screen.Recording.2024-09-26.at.10.54.32.AM.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants