bat icon indicating copy to clipboard operation
bat copied to clipboard

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

Open zachriggle opened this issue 3 years ago • 5 comments

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 avatar Apr 16 '22 02:04 zachriggle

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 https://github.com/sharkdp/bat/issues/2149#issuecomment-1091044320)

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

keith-hall avatar Apr 16 '22 20:04 keith-hall

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.

zachriggle avatar Apr 16 '22 21:04 zachriggle

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.

lsoksane avatar Feb 05 '23 13:02 lsoksane

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

eth-p avatar Apr 16 '23 21:04 eth-p