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 7, 2023
1 parent 32369ac commit 79ebb44
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod util;

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

Expand Down Expand Up @@ -283,11 +285,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 79ebb44

Please sign in to comment.