Don't show error if fzf exits gracefully

This commit is contained in:
Ajeet D'Souza 2020-04-05 22:31:32 +05:30
parent 2f73465d8d
commit b21dbefa22
3 changed files with 29 additions and 7 deletions

12
src/error.rs Normal file
View File

@ -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(())
}
}

View File

@ -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::<SilentExit>() {
Ok(SilentExit { code }) => process::exit(code),
Err(e) => e,
})
}

View File

@ -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