Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.copy is not working on Linux X11 #160

Closed
gauravkumar37 opened this issue Oct 27, 2023 · 9 comments · Fixed by #161
Closed

.copy is not working on Linux X11 #160

gauravkumar37 opened this issue Oct 27, 2023 · 9 comments · Fixed by #161

Comments

@gauravkumar37
Copy link
Contributor

gauravkumar37 commented Oct 27, 2023

  • Using the latest commit on Linux with X11 display server, .copy is not working and nothing happens, not even after quitting the app.
  • Using Linux Majaro with 6.5.5-1 kernel on X11 with KDE as the desktop environment
  • echo ok | xclip -selection clipboard -i this works
@gauravkumar37 gauravkumar37 changed the title .copy is nor working on Linux X11 .copy is not working on Linux X11 Oct 27, 2023
@sigoden
Copy link
Owner

sigoden commented Oct 27, 2023

This feature has been around for a long time, and no one has reported bugs. I've tried it myself and found no problem.
Is there something wrong with your operation? Can you do it again based on the picture?

image

@gauravkumar37
Copy link
Contributor Author

Yes, tried again like in the picture.
I think this may be because of 1Password/arboard#114

@gauravkumar37
Copy link
Contributor Author

Ok, tried with the library arboard on local. Here's my debugging steps, not super familiar with rust but I think this has something to do with text reference vs actual data?

use std::time::Duration;

use arboard::Clipboard;

fn main() {
    copy("this does not work");

    let mut clipboard = Clipboard::new().unwrap();
    clipboard.set_text("this works").unwrap();

    // give it a second to sync with clipboard before exiting the program
    std::thread::sleep(Duration::from_secs(1));
}

pub fn copy(src: &str) {
    let mut clipboard = Clipboard::new().unwrap();
    clipboard.set_text(src).unwrap();
}

@gauravkumar37
Copy link
Contributor Author

gauravkumar37 commented Oct 27, 2023

Aah, got it, you need some delay in the function copy. This fixes it:

use std::time::Duration;

use arboard::Clipboard;

fn main() {
    copy("this was not working earlier but now works");

    let mut clipboard = Clipboard::new().unwrap();
    clipboard.set_text("this works").unwrap();

    // give it a second to sync with clipboard before exiting the program
    // std::thread::sleep(Duration::from_secs(1));
}

pub fn copy(src: &str) {
    let mut clipboard = Clipboard::new().unwrap();
    clipboard.set_text(src).unwrap();

    std::thread::sleep(Duration::from_secs(1)); // <-- this is the fix
}

Used aichat to debug aichat :-)

@sigoden
Copy link
Owner

sigoden commented Oct 27, 2023

Is 1 second is enough? or 100 milliseconds is enough?
Is delay related to the length of the content?
Moreover, aichat supports config.auto_copy. Long delay will affect the user experience.

It is better to wait for arboard to fix related problems.

@gauravkumar37
Copy link
Contributor Author

auto copy is also not working and I think this is why: clipboard is being initialized inside the copy function every time. So before it has a chance to actually copy (asynchronously) data to OS clipboard, the clipboard is dereferenced because copy function returns.
The timeout was just a band-aid fix. Actual fix is to extract out the clipboard out of the copy function and only initialize it once.

use std::time::Duration;

use arboard::Clipboard;

fn main() {
    let mut clipboard = Clipboard::new().unwrap();

    copy(&mut clipboard, "hello");

    // give it a second to sync with clipboard before exiting the program
    // only needed in this test cli, not in aichat
    std::thread::sleep(Duration::from_secs(1));
}

pub fn copy(clipboard: &mut Clipboard, src: &str) {
    clipboard.set_text(src).unwrap();
}

@sigoden
Copy link
Owner

sigoden commented Oct 27, 2023

cloud you test #161 for me?

@gauravkumar37
Copy link
Contributor Author

Works like a charm. Also tested the auto_copy feature, that is also working. Thanks for such a wonderful tool and also for providing a fix so promptly 🚀

@sigoden
Copy link
Owner

sigoden commented Oct 27, 2023

You're welcome and thank you for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants