diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..14a1c05 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,12 @@ +use std::fmt::{self, Display}; + +#[derive(Debug)] +pub struct SilentExit { + pub code: i32, +} + +impl Display for SilentExit { + fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 012f85c..daf7d0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,17 @@ mod config; mod db; mod dir; +mod error; mod subcommand; mod util; +use crate::error::SilentExit; + use anyhow::Result; use structopt::StructOpt; +use std::process; + #[derive(Debug, StructOpt)] #[structopt(about = "A cd command that learns your habits", version = env!("ZOXIDE_VERSION"))] enum Zoxide { @@ -20,13 +25,16 @@ enum Zoxide { pub fn main() -> Result<()> { let opt = Zoxide::from_args(); - match opt { - Zoxide::Add(add) => add.run()?, - Zoxide::Import(import) => import.run()?, - Zoxide::Init(init) => init.run()?, - Zoxide::Query(query) => query.run()?, - Zoxide::Remove(remove) => remove.run()?, + let res = match opt { + Zoxide::Add(add) => add.run(), + Zoxide::Import(import) => import.run(), + Zoxide::Init(init) => init.run(), + Zoxide::Query(query) => query.run(), + Zoxide::Remove(remove) => remove.run(), }; - Ok(()) + res.map_err(|e| match e.downcast::() { + Ok(SilentExit { code }) => process::exit(code), + Err(e) => e, + }) } diff --git a/src/util.rs b/src/util.rs index ade9f43..b05fd67 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,7 @@ use crate::config; use crate::db::DB; use crate::dir::{Dir, Epoch}; +use crate::error::SilentExit; use anyhow::{anyhow, bail, Context, Result}; @@ -109,6 +110,7 @@ where Some(2) => bail!("fzf returned an error"), // terminated by a signal + Some(code @ 130) => bail!(SilentExit { code }), Some(128..=254) | None => bail!("fzf was terminated"), // unknown