From 2109a7830b8f8630ead4313656b86bfd6c05bc81 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Wed, 17 Oct 2018 20:21:58 +0200 Subject: [PATCH] Add error handling for parsing errors --- src/controller.rs | 2 +- src/output.rs | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/controller.rs b/src/controller.rs index e5c6ed53..d9a53786 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -19,7 +19,7 @@ impl<'b> Controller<'b> { } pub fn run(&self) -> Result { - let mut output_type = OutputType::from_mode(self.config.paging_mode); + let mut output_type = OutputType::from_mode(self.config.paging_mode)?; let writer = output_type.handle()?; let mut no_errors: bool = true; diff --git a/src/output.rs b/src/output.rs index 3c1838ce..797e6bba 100644 --- a/src/output.rs +++ b/src/output.rs @@ -15,22 +15,23 @@ pub enum OutputType { } impl OutputType { - pub fn from_mode(mode: PagingMode) -> Self { + pub fn from_mode(mode: PagingMode) -> Result { use self::PagingMode::*; - match mode { - Always => OutputType::try_pager(false), - QuitIfOneScreen => OutputType::try_pager(true), + Ok(match mode { + Always => OutputType::try_pager(false)?, + QuitIfOneScreen => OutputType::try_pager(true)?, _ => OutputType::stdout(), - } + }) } /// Try to launch the pager. Fall back to stdout in case of errors. - fn try_pager(quit_if_one_screen: bool) -> Self { + fn try_pager(quit_if_one_screen: bool) -> Result { let pager = env::var("BAT_PAGER") .or_else(|_| env::var("PAGER")) .unwrap_or(String::from("less")); - let pagerflags = shell_words::split(&pager).unwrap_or(vec![pager]); + let pagerflags = shell_words::split(&pager) + .chain_err(|| "Could not parse (BAT_)PAGER environment variable.")?; let less_path = PathBuf::from(&pagerflags[0]); let is_less = less_path.file_stem() == Some(&OsString::from("less")); @@ -49,12 +50,12 @@ impl OutputType { Command::new(&less_path) }; - process + Ok(process .args(&pagerflags[1..]) .stdin(Stdio::piped()) .spawn() .map(OutputType::Pager) - .unwrap_or_else(|_| OutputType::stdout()) + .unwrap_or_else(|_| OutputType::stdout())) } fn stdout() -> Self {