Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Add support for hyperlinks in kitty #165

Closed
wants to merge 1 commit into from
Closed

Add support for hyperlinks in kitty #165

wants to merge 1 commit into from

Conversation

wladh
Copy link

@wladh wladh commented Oct 6, 2020

Kitty supports OSC8 (hyperlinks) in versions 0.19.0 and later.

Kitty supports OSC8 (hyperlinks) in versions above 0.19.0.
@swsnr
Copy link
Owner

swsnr commented Oct 6, 2020

@wladh Woa, nice 🤩 Been waiting for this, guess it's about time I switch my terminal emulator!

I'll give this change a review soon'ish 🙂

@swsnr swsnr self-assigned this Oct 6, 2020
Copy link
Owner

@swsnr swsnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wladh Thanks a lot for this PR. Aside from a few stylistic remarks concerning the capabilities constructor my major concern is how this PR checks for the Kitty version.

I think shelling out to kitty --version is not really reliable, and it's only half way through, because there's the allow_hyperlinks configuration setting to account for.

.ok()
.filter(|value| value == "xterm-kitty")?;

let output = Command::new("kitty").arg("--version").output().ok()?;
Copy link
Owner

@swsnr swsnr Oct 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if kitty is not in $PATH or doesn't exist at all (e.g. inside a chroot or systemd container). And worse, it won't fail gracefully in this case and detect kitty without hyperlinks; instead it'll make mdcat believe that it's not running in kitty at all, even if $TERM says it's kitty.

Can we somehow get the version in a more reliable way? Perhaps by means of some escape sequence?

And is the version actually sufficient? What about the allow_hyperlinks setting?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If kitty is not in $PATH image display would also fail. You're right about allow_hyperlinks, but looking at the issue and at the commits in kitty I haven't seen anything implemented for feature detection.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware that image display uses kitty. In this case I agree that it's a good idea to fail completely if kitty isn't found! But please document this in the docstring of the method, so that I don't forget and think it's a bug in six months 😉

@@ -128,11 +128,15 @@ impl TerminalCapabilities {
}

/// Terminal capabilities of Kitty.
pub fn kitty() -> TerminalCapabilities {
pub fn kitty(version: (u8, u8, u8)) -> TerminalCapabilities {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like for these constructors to always return the same set of capabilities.

Would you mind to leave pub fn kitty() as is, and add a separate and independent pub fn kitty_with_links()? And the move the condition either into a separate pub fn kitty_autodetect(version) or even just to detect()?

@kovidgoyal
Copy link

Why do you need to detect at all? Simply always output hyperlinks if on kitty, earlier versions of kitty will ignore them and display text, newer versions will hyperlink the text. Unless you are changing the output of mdcat based on detecting hyperlink support. In which case I am happy to discuss modalities for detection, although they would be kitty specific, since the spec author refused to add any detection facilities to the spec.

@swsnr
Copy link
Owner

swsnr commented Oct 7, 2020

Unless you are changing the output of mdcat based on detecting hyperlink support.

In fact we do @kovidgoyal. If mdcat "detects" hyperlink support it only prints the link text. If the terminal swallows the URL the user will have no chance to even see it. We do have an (albeit private) flag to emit only plain ANSI format which would let the user work around this, but I would prefer if mdcat would only use hyperlinks if it's reasonably sure the underlying terminal supports it.

But "detection" is more "guesswork" as far as mdcat's concerned. For VTE terminals or iTerm we also just rely on some environment variables; if kitty could export the state of hyperlink support wrt to the flag as environment variable I'd be happy.

@kovidgoyal
Copy link

I've already added escape code based detection, see the hyperlink ticket
in the kitty bug tracker for details. Environment variables are
a non-starter, they dont work over SSH or sudo, for instance.

@swsnr
Copy link
Owner

swsnr commented Oct 7, 2020

I've already added escape code based detection, see the hyperlink ticket in the kitty bug tracker for details.

@wladh Can you check whether that's feasible for mdcat?

Environment variables are a non-starter, they dont work over SSH or sudo, for instance.

@kovidgoyal I don't see things so rigorous 🤷 Environment variables are also simple to use, and fail safely. At worst you'll get Ansi formatting out of mdcat, but it won't crash or mess up the terminal with some weird escape stuff.

And what good's a nice escape sequence if we can't use it because $TERM didn't make it across whatever connection we have? What good's an escape sequence if printing images requires kitty icat which doesn't make it across SSH either? 🙂

Sure environment variables aren't perfect, but from my point of view this is a "a good guess is good enough" business 😎

@kovidgoyal
Copy link

kovidgoyal commented Oct 7, 2020 via email

@kovidgoyal
Copy link

kovidgoyal commented Oct 7, 2020 via email

@swsnr
Copy link
Owner

swsnr commented Oct 7, 2020

There is nothing weird about this escape code.

That was proverbial, there's no need to lecture me 🙂

You can use the escape code regardless of TERM.

We need to be reasonably sure the underlying terminal understands the escape sequence, in particular if we expect to receive a response.

@swsnr
Copy link
Owner

swsnr commented Oct 7, 2020

And you dont need kitty icat to display images. You can output the escape codes for doing so yurself.

But do, but we still need kitty icat --print-window-size to get the window size. Perhaps there's also an escape sequence for that, but it's how it is currently, and I don't have the time to change it.

@kovidgoyal
Copy link

kovidgoyal commented Oct 7, 2020 via email

@kovidgoyal
Copy link

kovidgoyal commented Oct 7, 2020 via email

@swsnr
Copy link
Owner

swsnr commented Oct 7, 2020

No, you dont. You cant assume you will get a response in any case since this can potentially be over a arbitrarily slow network link.

Obviously 👍 but that's not my point 🙂

I just wouldn't print the escape sequence in the first place if I'm not reasonably sure that the terminal understands it and returns a good response in return if it gets the chance. I wouldn't trust a terminal to handle anything it doesn't understand well.

I do concede though that XTGETTCAP is a fairly standard sequence that all terminals should at least ignore safely. But I'd still check $TERM first 🙂

Delay in catting is not really desirable

I guess we can afford some 100ms to check for hyperlinks, for I presume no one's going to use mdcat through SSH over a slow link; even if it might survive feature detection it's gonna stall anway as soon as it tries to transmit an entire uncompressed image embedded in escape sequences 🙈

I care more about having a good default, and to me this implies we automagically enable hyperlinks if the underlying terminal supports. I don't want to think about this myself, or mess with my shell aliases 🤷

This tool very much follows my own personal preferences, because my own use is still what this is mostly written for 😊

As far as kitty is concerned, there is not going to be an env var just for hyperlink detection.

That's fine for me, and I didn't intend ask for any different. I'm sorry if you understood me different.

I just suggested an env var in the first place because I thought it'd be the simplest way, and I wasn't aware that kitty had other means to detect hyperlink support. I'm fine with environment variables but I'm also fine with any other means of making a good guess whether Kitty supports links.

I applaud you for doing things the right way(TM) in Kitty, but as far as mdcat's concerned we're using env vars all over the place anyway since no other terminal goes this far to make things "right". 🙈 If everyone would properly respond to XTGETTCAP I'd perhaps be a happier person, but then again none yet complained that we did the wrong thing in iTerm or Gnome Terminal 😉, and I have to admit that I have many more pressing things to worry about 🙂

But there's still no need to lecture me, so I'd appreciate if did a little less "No, you don't" and spoke a little less condescending, and just a tad more friendly.

No, you dont. Getting the window size is a simple piece of synchronous five line code

The person who contributed image support for Kitty did. Perhaps they weren't aware of this ioctl, perhaps they considered shelling out easier and safer than ioctls, I don't know. But things are as they are and as I said I don't have time to change it.

Will you open a pull request? I don't think so, and I don't expect you to, but I would like to ask you to speak less condescending about work people contribute in their own free time for the benefit of others. I mean no offence but they do deserve better 🙂

@kovidgoyal
Copy link

kovidgoyal commented Oct 7, 2020 via email

@swsnr
Copy link
Owner

swsnr commented Oct 7, 2020

I'm suggesting you always output hyperlinks, the vast majority of terminals will ignore them safely, and leave it up to users to configure if they dont want hyperlinks.

As said, mdcat doesn't print the URL if it uses hyperlinks, so this would leave people on terminals without hyperlink support without any way to actually see the URL. Sure, they can configure it, but it still makes a poor default in my opinion, in particular since some popular terminals do not yet support hyperlinks (e.g. xterm.js which is what VSCode uses as far as I know, or KDE Konsole).

I do very much hope that this makes a safe default at some point but I don't think we're there yet, even if you just consider "modern" terminal emulators.

"No, you dont", isn't condescending. Sorry you feel that way.

I guess we'll have to agree to disagree here, but thank you for your apology 🙏

Anyway, I am bowing out now. Good luck.

To you as well. Thank you for the work you do on Kitty 👍 It is truly an awesome tool, and easily one of the best terminal emulators out there 👏

@swsnr swsnr closed this in 676cba9 Oct 16, 2020
@swsnr
Copy link
Owner

swsnr commented Oct 16, 2020

@wladh I've followed @kovidgoyal suggestions, and added it without any kind of feature detection. mdcat now prints hyperlinks on Kitty, unconditionally. Those who can update mdcat can likely also just update kitty, and anyone who cares to disable allow_hyperlinks perhaps wouldn't use mdcat in the first place 🤷

If anyone needs feature detection they can open another pull request 🙂

@kovidgoyal While I'm at this I'm sorry to bother you with another question: Kitty doesn't happen to support an escape sequence to set a mark on a certain line number, like iTerm does? In Iterm2 mdcat places such marks on every Markdown header to allow jumping back and forth between sections.

@kovidgoyal
Copy link

kovidgoyal commented Oct 16, 2020 via email

@swsnr
Copy link
Owner

swsnr commented Oct 16, 2020

@kovidgoyal Sorry to hear this. Thanks for your fast answer 🙏

@wladh
Copy link
Author

wladh commented Oct 19, 2020

Sorry, I didn't have time to get back to this. Thanks for implementing it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
4 participants