Skip to content

Commit

Permalink
add timeout to clipboard init
Browse files Browse the repository at this point in the history
  • Loading branch information
jonZlotnik committed Mar 6, 2023
1 parent 10e167c commit 90529c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
**/*.rs.bk

.idea/
.vscode/
32 changes: 26 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod util;

use std::{
ops::Deref,
time::{Duration, Instant},
time::{Duration, Instant}, thread, sync::mpsc,
};

use async_std::{fs::OpenOptions, sync::Arc};
Expand Down Expand Up @@ -283,11 +283,31 @@ async fn main() -> eyre::Result<()> {
.try_init()?;
}

let mut clipboard = ClipboardContext::new()
.map_err(|err| {
log::warn!("Failed to initialize clipboard support: {}", err);
})
.ok();
// cli_clipboard can hang if unable to connect to x11 socket in linux
// so we try for up to 1 second
let (sender, receiver) = mpsc::channel();
let _clipboard_init_thread = thread::spawn(move || {
match sender.send(
ClipboardContext::new().map_err(|err| {
log::warn!("Failed to initialize clipboard support: {}", err);
})
.ok()
){
Ok(()) => {}, // everything good
Err(_) => {}, // we have been released, don't panic
}
});
let mut clipboard = match receiver.recv_timeout(Duration::from_millis(1000)) {
Ok(initialized_clipboard) => {
log::debug!("Clipboard initialized successfully");
initialized_clipboard
},
Err(err) => {
log::warn!("Clipboard initialization timed out: {}", err);
None
},
};


let concat_file_name = |file_path: &Path, file_name: Option<_>| {
// TODO this has gotten out of hand (it ugly)
Expand Down

0 comments on commit 90529c1

Please sign in to comment.