2

I have a ruby terminal script that I would like to print out hyperlinks. I achieve that like so:

puts("\e]8;;https://example.com\aThis is a link\e]8;;\a")

This works perfectly fine in a "normal" terminal (gnome-terminal btw) window.

But I need to run this script within GNU screen, where the escape-sequence simply has no effect. Other sequences (like colors for example) work fine, the hyperlink one (which according to the source might be a gnome-terminal-only thing) doesn't. (screen is running inside gnome-terminal)

How can I get screen to acknowledge my link sequence and display it properly?

confetti
  • 1,882
  • 3
  • 23
  • 40

1 Answers1

4

You can pass through some text to the terminal screen itself runs in by putting it inside an ESC P, ESC \ pair (\033P%s\033\\ in printf format).

So you should bracket inside \eP..\e\\ pairs all the parts of the sequence, except for the text which will appear on the screen ("This is a link"):

printf '\eP\e]8;;https://example.com\a\e\\This is a link\eP\e]8;;\a\e\\\n'
printf '\eP\e]8;;%s\a\e\\%s\eP\e]8;;\a\e\\\n' https://example.com 'This is a link'

Or, from C:

puts("\eP\e]8;;https://example.com\a\e\\This is a link\eP\e]8;;\a\e\\");
printf("\eP\e]8;;%s\a\e\\%s\eP\e]8;;\a\e\\\n", url, title);

Putting the replacement text too inside \eP..\e\\ may result in screen losing track of the cursor position.


This is documented in the GNU screen manual:

ESC P  (A)     Device Control String
               Outputs a string directly to the host
               terminal without interpretation

The "string" should should be terminated with a ST ("string terminator") escape, ie. \e\\ -- thence \eP..\e\\.

  • I might be doing something wrong, but that doesn’t work for me... – Stephen Kitt Mar 27 '20 at 15:43
  • This does not work for me either, neither with `printf` nor with `echo -e` nor from the ruby script. It prints `This is a link` but it's not clickable. Gnome-terminal 3.36.0.1 GNU Screen 4.08.00 – confetti Mar 27 '20 at 15:45
  • @mosvy I know it *should* work, and I had no doubt you’d tested it. I tried it on Fedora 31 running GNU screen 4.06.02 inside GNOME Terminal 3.34.2, and a remote Debian 10 running GNU screen 4.06.02 inside the same GNOME Terminal. – Stephen Kitt Mar 27 '20 at 15:48
  • With the remote connection, I get `]8;;https://example.comThis is a link`; in Fedora, I get an undecorated `This is a link`. – Stephen Kitt Mar 27 '20 at 15:50
  • `STY= screen` did not work. I run this all on a local arch linux machine. I've tried to run `script /dev/null` inside `screen` first aswell which fixes some other screen related issues, but not this one. – confetti Mar 27 '20 at 15:58
  • @confetti it may have to do with `~/.screenrc` config options, I will try to look into what may break it. –  Mar 27 '20 at 15:59
  • Contents of my `~/.screenrc`: `defscrollback 150000` – confetti Mar 27 '20 at 15:59
  • @confetti try setting `vbell off` in your `~/.screenrc` and see if it fixes it. –  Mar 27 '20 at 16:12
  • I don't understand how (assuming `vbell off` turns the terminal BEL off?), but that did the trick! It's working now! – confetti Mar 27 '20 at 16:13
  • 1
    That's because `screen` will "eat up" the `\a` which terminates that escape. I will update the answer with a solution which doesn't need that `~/.screenrc` mod. –  Mar 27 '20 at 16:22
  • @confetti please see the updated answer, especially the note about screen losing track of the cursor position. –  Mar 27 '20 at 17:17
  • Could what you said last have to do with the issue I just figured out? When I enter screen's copy mode (CTRL-A + ESC), all the links disappear (including the text) completely. Even after exiting copy-mode again, it's all just gone. Edit: Okay, yes, after updating my code with your update the text doesn't disappear anymore. The link however does and even after exiting copy-mode it's sitll not clickable anymore. That's not really a big deal for me though. – confetti Mar 27 '20 at 18:42
  • No, it's not related. It has rather to do with the fact that screen re-displaying the saved buffer, without any escapes which were passed through, of course. It will also happen when you switch between screen windows with Ctrl-A Ctrl-N, etc. I don't think there's any fix for that. –  Mar 27 '20 at 18:51