From 708e95b640495c90d6d4474f35994ba48f536126 Mon Sep 17 00:00:00 2001 From: chris west Date: Sat, 18 Jan 2020 20:24:01 -0800 Subject: [PATCH] slightly nicer clipboard/open errors --- README.md | 2 -- src/utils.rs | 21 +++++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 45af135..5dc6627 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,6 @@ To enable just TLS support, or just Tor support, use `--features`: - [ ] don't create new strings for every Line - [ ] catch SIGWINCH - [ ] disable ctrl-c outside of raw mode (telnet) -- [ ] xdg-open -- [ ] nicer error msg when there's no xclip/pbcopy ## bugs diff --git a/src/utils.rs b/src/utils.rs index 41d84bf..dba34c3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -62,10 +62,13 @@ pub fn copy_to_clipboard(data: &str) -> Result<()> { #[cfg(not(target_os = "macos"))] let mut cmd = process::Command::new("xclip").args(&["-sel", "clip"]); - cmd.stdin(Stdio::piped()).spawn().and_then(|mut child| { - let child_stdin = child.stdin.as_mut().unwrap(); - child_stdin.write_all(data.as_bytes()) - }) + cmd.stdin(Stdio::piped()) + .spawn() + .and_then(|mut child| { + let child_stdin = child.stdin.as_mut().unwrap(); + child_stdin.write_all(data.as_bytes()) + }) + .map_err(|e| error!("Clipboard error: {}", e)) } /// Used to open non-Gopher URLs. @@ -76,15 +79,17 @@ pub fn open_external(url: &str) -> Result<()> { #[cfg(not(target_os = "macos"))] let cmd = "xdg-open"; - let output = process::Command::new(cmd).arg(url).output()?; + let output = process::Command::new(cmd) + .arg(url) + .output() + .map_err(|e| error!("`open` error: {}", e))?; + if output.stderr.is_empty() { Ok(()) } else { Err(error!( "`open` error: {}", - String::from_utf8(output.stderr) - .unwrap_or_else(|_| "?".into()) - .trim_end() + String::from_utf8_lossy(&output.stderr).trim_end() )) } }