xterm.js icon indicating copy to clipboard operation
xterm.js copied to clipboard

ANSI OSC 52 support?

Open olegkrutov opened this issue 3 years ago • 12 comments

ANSI OSC 52 is a sequence to transport some data from remote console app to client's clipboard. For example, tmux can use it on remote side, and xterm (terminal app), Windows terminal and a number of others -- on client side. It would be very convenient to have such a feature in xterm.js. Here is a simple test which sends a string from remote to local clipboard (taken here): printf "\033]52;c;$(printf "%s" "blabla" | base64)\a"

olegkrutov avatar Mar 22 '21 11:03 olegkrutov

This would need some thinking around how embedders can conditionally enable it, like how browsers ask if they can access the clipboard.

Tyriar avatar Mar 22 '21 13:03 Tyriar

Maybe it can be done as plugin so it can be totally disabled, otherwise raising a pop-up window when such a sequence arrives first time in session?

olegkrutov avatar Mar 22 '21 15:03 olegkrutov

Just tested this with Firefox and Chrome:

setTimeout(() => navigator.clipboard.writeText('Hello there!'), 1000);

which seems to work in both as long as something on the page has the focus. Not quite sure about the focus state, it also works if the browser window is in the background, but does not work, if the focus is in the console. No clue about Safari.

What would be possible:

  • make it optional via terminal settings or as addon
  • use async handler for OSC 52, with this we can stop terminal progression and show an "Allow clipboard access?" popup, if the attempt fails in the first place due to missing focus (clipboard.writeText returns a promise, easy to get that up with an async handler now :smile_cat:)

jerch avatar Mar 22 '21 15:03 jerch

which seems to work in both as long as something on the page has the focus.

I think browsers treat this differently and it may get blocked if it wasn't caused by a "user event" (ie. KeyboardEvent, MouseEvent).

make it optional via terminal settings or as addon

The way VS Code copies depends on the environment it's in, so this might need an event. If it was an event the embedder could just not listen to it if it doesn't want to support it then.

with this we can stop terminal progression and show an "Allow clipboard access?" popup,

We probably wouldn't want to pause the terminal, just request it and not await.

Tyriar avatar Mar 22 '21 16:03 Tyriar

Isn't it worth to assume that since we are selecting text here in xterm session, we have a focus right now? Focus will be lost in situations like some "mirroring" of terminal output but I don't think this is 1) majority of cases and 2) we should manipulate with clipboard in these situations...

olegkrutov avatar Mar 22 '21 18:03 olegkrutov

Isn't it worth to assume that since we are selecting text here in xterm session, we have a focus right now? Focus will be lost in situations like some "mirroring" of terminal output but I don't think this is 1) majority of cases and 2) we should manipulate with clipboard in these situations...

OSC 52 doesn't mean text is selected. It means the remote process has emitted this sequence. That could happen from a process that's running, such as sleep 30; printf "\033]52;c;$(printf "%s" "blabla" | base64)\a". No selection necessary, and the terminal could easily be out of focus by the time the sequence is reached.

jaraco avatar Dec 07 '21 22:12 jaraco

@jaraco Yes, part of the OSC 52 deal is that it works the way around for remote --> local clipboard mapping. For this it cannot be assumed, that the terminal still has focus. A partial workaround could be to implement a deferred clipboard write, which replays the write onFocus.

jerch avatar Dec 08 '21 12:12 jerch

We may want to expose a handler on the API similar to the to attachCustomKeyEventHandler to handle copying, that way the embedder can hook it up to its clipboard mechanism (eg. electron/native copy API, clipboard web API, fallback to execCommand).

Tyriar avatar Dec 08 '21 12:12 Tyriar

Is there any update on this? It would be really nice to have this feature in vscode's terminal.

YingboMa avatar Aug 11 '22 23:08 YingboMa

@YingboMa as with anything where the clipboard is touched we want to make sure there aren't any security concerns (if you log into a malicious server, it could read your clipboard which may have a password in it?), we would need some API to plug the clipboard detection code in anyway I think so that would probably be solved by a warning on the API. Other than that potential issue, it just needs someone to come and help implement it.

Tyriar avatar Aug 12 '22 00:08 Tyriar

Refs from Windows Terminal:

  • https://github.com/microsoft/terminal/issues/2946
  • https://github.com/microsoft/terminal/issues/9479

Tyriar avatar Aug 01 '23 21:08 Tyriar

really looking forward to copy text from tmux inside coder web terminal

MrPeacockNLB avatar Sep 14 '23 17:09 MrPeacockNLB