From 29534ae3416d3e5cda659f0f34d3220e7ca78682 Mon Sep 17 00:00:00 2001 From: Gregory Date: Fri, 12 Jun 2020 14:56:30 -0400 Subject: [PATCH] Better handle cancellation --- src/config.rs | 6 +++++- src/error.rs | 2 ++ src/main.rs | 15 ++++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 859eb03..161b4e7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -57,6 +57,10 @@ impl Config { output.trim_end().to_owned() }; - Ok(output) + if output.is_empty() { + Err(Error::Cancelled) + } else { + Ok(output) + } } } diff --git a/src/error.rs b/src/error.rs index 9eea3c6..2b86b76 100644 --- a/src/error.rs +++ b/src/error.rs @@ -22,6 +22,8 @@ pub enum Error { MimeDetect(#[from] mime_detective::DetectiveError), #[error("error spawning selector process '{0}'")] Selector(String), + #[error("selection cancelled")] + Cancelled, } pub type Result = std::result::Result; diff --git a/src/main.rs b/src/main.rs index 9a182ee..7285dd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,14 +54,19 @@ fn main() -> Result<()> { }(); match (res, atty::is(atty::Stream::Stdout)) { - (Err(e), true) => eprintln!("{}", e), + (Err(e), _) if matches!(e, Error::Cancelled) => { + std::process::exit(1); + } + (Err(e), true) => { + eprintln!("{}", e); + std::process::exit(1); + } (Err(e), false) => { std::process::Command::new("notify-send") .args(&["handlr error", &e.to_string()]) .spawn()?; + std::process::exit(1); } - _ => {} - }; - - Ok(()) + _ => Ok(()), + } }