From dccf8d82212e24e9015e4346a3d7f9330051a6ff Mon Sep 17 00:00:00 2001 From: sharkdp Date: Wed, 12 Sep 2018 21:35:23 +0200 Subject: [PATCH] Use a more streamlined version of style-component collection --- src/app.rs | 29 ++++++++++++++--------------- src/assets.rs | 1 - src/style.rs | 3 ++- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index 7a69285c..24de5b5b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,7 +12,7 @@ use console::Term; #[cfg(windows)] use ansi_term; -use assets::{BAT_STYLE_DEFAULT, BAT_THEME_DEFAULT}; +use assets::BAT_THEME_DEFAULT; use errors::*; use line_range::LineRange; use style::{OutputComponent, OutputComponents, OutputWrap}; @@ -430,26 +430,25 @@ impl App { [OutputComponent::Numbers].iter().cloned().collect() } else if matches.is_present("plain") { [OutputComponent::Plain].iter().cloned().collect() - } else if matches.is_present("style") { - values_t!(matches.values_of("style"), OutputComponent)? + } else { + let env_style_components: Option> = + transpose(env::var("BAT_STYLE").ok().map(|style_str| { + style_str + .split(",") + .map(|x| OutputComponent::from_str(&x)) + .collect::>>() + }))?; + + values_t!(matches.values_of("style"), OutputComponent) + .ok() + .or(env_style_components) + .unwrap_or(vec![OutputComponent::Full]) .into_iter() .map(|style| style.components(self.interactive_output)) .fold(HashSet::new(), |mut acc, components| { acc.extend(components.iter().cloned()); acc }) - } else { - let style_str = env::var("BAT_STYLE").unwrap_or(String::from(BAT_STYLE_DEFAULT)); - style_str - .split(",") - .map(|x| OutputComponent::from_str(&x)) - .map(|s| match s { - Ok(style) => style.components(self.interactive_output), - Err(_) => &[], - }).fold(HashSet::new(), |mut acc, components| { - acc.extend(components.iter().cloned()); - acc - }) }, )) } diff --git a/src/assets.rs b/src/assets.rs index 296bbfac..3e3211b3 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -18,7 +18,6 @@ lazy_static! { ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory"); } -pub const BAT_STYLE_DEFAULT: &str = "auto"; pub const BAT_THEME_DEFAULT: &str = "Monokai Extended"; pub struct HighlightingAssets { diff --git a/src/style.rs b/src/style.rs index efd83697..56b04ee1 100644 --- a/src/style.rs +++ b/src/style.rs @@ -54,7 +54,8 @@ impl FromStr for OutputComponent { "header" => Ok(OutputComponent::Header), "numbers" => Ok(OutputComponent::Numbers), "full" => Ok(OutputComponent::Full), - "plain" | _ => Ok(OutputComponent::Plain), + "plain" => Ok(OutputComponent::Plain), + _ => Err(format!("Unknown style '{}'", s).into()), } } }