Open zachriggle opened 2 years ago
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 :)
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.
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.
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 :)
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:
https://github.com/user-attachments/assets/a4a01cb8-ea75-4ae9-9480-61a19f2df93d
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 tofile://
, which informs Bat as to which URI to printbat
options, none of the above have any effectFor 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 clickablefile://
linkTechnical Details
I'm not a Rustacean yet, but here's a function that creates OCS8 links in Python.
Sponsorship Not that this grants me any special treatment, but I'm a sponsor for all of the maintainers of
bat
❤️💵