alacritty/copypasta

Clipboard is cleared when the process exits on Linux

xliiv opened this issue ยท 11 comments

xliiv commented

Any plans to address this inherited persistence issue?
aweinstock314/rust-clipboard#61

Below code to reproduce

extern crate copypasta;

use copypasta::ClipboardContext;
use copypasta::ClipboardProvider;

fn main() {
    let mut ctx = ClipboardContext::new().unwrap();

    ctx.set_contents("Test".to_owned()).unwrap();

    dbg!(ctx.get_contents().unwrap());  // CONTAINS "Test"
}
// dbg!(ctx.get_contents().unwrap(); CONTAINS "";

No, this is by design on X11 and if you do not like it, you should install a clipboard manager.

Solving this inside of this library is the incorrect approach.

Agree, it should be handled in application level (instead of library) - thanks!

Just to clarify, I'd recommend even against trying to handle this in your application. This should be handled by separate software on X11.

Just to clarify, I'd recommend even against trying to handle this in your application.

Unless, you write clipboard manager, right?

Anyway, thanks again.

Last thing, here's a good explanation of the clipboard topic which just found
https://www.uninformativ.de/blog/postings/2017-04-02/0/POSTING-en.html

Unless, you write clipboard manager, right?

Seems like it.

Not a solution for this issue, but this might be useful for people landing here looking for a solution.

For ffsend (and a few other crates) I do want to keep clipboard contents because these CLI applications immediately quit after invocation, making the current situation useless. Linking people to clipboard managers is not a solution in my case. I therefore extended this crate with copypasta-ext to provide clipboard contexts that do fork or invoke xclip/xsel to work around this issue.

For example, these will keep clipboard contents after exit:

use clipboard_ext::prelude::*;

// Fork and keep contents
clipboard_ext::x11_fork::ClipboardContext::new().unwrap()
    .set_contents("some string".into()).unwrap();

// Invoke xclip/xsel and keep contents
clipboard_ext::x11_bin::ClipboardContext::new().unwrap()
    .set_contents("some string".into()).unwrap();

See the README for more information.

Edit: link to extension crate for copypasta (this crate), instead of one for rust-clipboard

@timvisee Note that with CLI applications, you can also use the terminal's clipboard escape to write to the clipboard using the terminal emulator. That's a more 'proper' solution anyways in theory.

Unfortunately not all applications support that, which is why popular CLI applications like neovim use xclip for example. So that's probably what I'd recommend when you really need it, instead of forking to your own background process.

I wasn't aware that terminals have a clipboard escape, thanks for mentioning this!

I currently am using xclip through clipboard_ext::x11_bin::ClipboardContext in my applications, the alternative context that forks is just there for others that might find it useful. Thanks for the heads up though.

terminal's clipboard escape

@chrisduerr what do you mean? Could you provide a link on that?

You can write to the clipboard using printf "\e]52;c;$(printf 'test' | base64)\a.

See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html.