From 9d6bde48fa69adcc14633adc46025d9d68239159 Mon Sep 17 00:00:00 2001 From: ms2300 Date: Sun, 9 Sep 2018 18:28:04 -0600 Subject: [PATCH 1/5] Added BAT_STYLE env variable functionality --- src/app.rs | 18 +++++++++++++++--- src/assets.rs | 1 + 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index a9ac691e..5fd71c6b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; use std::env; use std::path::Path; +use std::str::FromStr; use atty::{self, Stream}; @@ -11,7 +12,7 @@ use console::Term; #[cfg(windows)] use ansi_term; -use assets::BAT_THEME_DEFAULT; +use assets::{BAT_THEME_DEFAULT, BAT_STYLE_DEFAULT}; use errors::*; use line_range::LineRange; use style::{OutputComponent, OutputComponents, OutputWrap}; @@ -174,7 +175,7 @@ impl App { .takes_value(true) .possible_values(&[ "auto", "full", "plain", "changes", "header", "grid", "numbers", - ]).default_value("auto") + ]) .help("Comma-separated list of style elements to display.") .long_help( "Configure which elements (line numbers, file headers, grid \ @@ -428,7 +429,7 @@ impl App { [OutputComponent::Numbers].iter().cloned().collect() } else if matches.is_present("plain") { [OutputComponent::Plain].iter().cloned().collect() - } else { + } else if matches.is_present("style") { values_t!(matches.values_of("style"), OutputComponent)? .into_iter() .map(|style| style.components(self.interactive_output)) @@ -436,6 +437,17 @@ impl App { acc.extend(components.iter().cloned()); acc }) + } else { + let style = env::var("BAT_STYLE").unwrap_or(String::from(BAT_STYLE_DEFAULT)); + match OutputComponent::from_str(&style) { + Ok(s) => [s].iter() + .map(|style| style.components(self.interactive_output)) + .fold(HashSet::new(), |mut acc, components| { + acc.extend(components.iter().cloned()); + acc + }), + Err(_) => HashSet::new(), + } }, )) } diff --git a/src/assets.rs b/src/assets.rs index 3e3211b3..296bbfac 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -18,6 +18,7 @@ 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 { From b9c556a0b606d07313787a8039604501afc50d36 Mon Sep 17 00:00:00 2001 From: ms2300 Date: Sun, 9 Sep 2018 18:35:40 -0600 Subject: [PATCH 2/5] Updated long help to reflect BAT_STYLE changes --- src/app.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index 5fd71c6b..83ffc4f2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -182,7 +182,8 @@ impl App { borders, Git modifications, ..) to display in addition to the \ file contents. The argument is a comma-separated list of \ components to display (e.g. 'numbers,changes,grid') or a \ - pre-defined style ('full')", + pre-defined style ('full'). To set a default theme, export the \ + BAT_STYLE environment variable (e.g.: export BAT_STYLE=\"numbers\").", ), ).arg( Arg::with_name("plain") From cf24986edb0647fe3539f71aedc24b3dd3b9503f Mon Sep 17 00:00:00 2001 From: ms2300 Date: Mon, 10 Sep 2018 00:07:46 -0600 Subject: [PATCH 3/5] Run formatting for bat_style changes --- src/app.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/app.rs b/src/app.rs index 83ffc4f2..8264914a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,7 +12,7 @@ use console::Term; #[cfg(windows)] use ansi_term; -use assets::{BAT_THEME_DEFAULT, BAT_STYLE_DEFAULT}; +use assets::{BAT_STYLE_DEFAULT, BAT_THEME_DEFAULT}; use errors::*; use line_range::LineRange; use style::{OutputComponent, OutputComponents, OutputWrap}; @@ -441,12 +441,13 @@ impl App { } else { let style = env::var("BAT_STYLE").unwrap_or(String::from(BAT_STYLE_DEFAULT)); match OutputComponent::from_str(&style) { - Ok(s) => [s].iter() - .map(|style| style.components(self.interactive_output)) - .fold(HashSet::new(), |mut acc, components| { - acc.extend(components.iter().cloned()); - acc - }), + Ok(s) => [s] + .iter() + .map(|style| style.components(self.interactive_output)) + .fold(HashSet::new(), |mut acc, components| { + acc.extend(components.iter().cloned()); + acc + }), Err(_) => HashSet::new(), } }, From 7897260bf00fc017da383a783eede2583206acc5 Mon Sep 17 00:00:00 2001 From: ms2300 Date: Mon, 10 Sep 2018 21:58:19 -0600 Subject: [PATCH 4/5] BAT_STYLE accounts for multiple styles (ie numbers,header) --- src/app.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/app.rs b/src/app.rs index 8264914a..7a69285c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -439,17 +439,17 @@ impl App { acc }) } else { - let style = env::var("BAT_STYLE").unwrap_or(String::from(BAT_STYLE_DEFAULT)); - match OutputComponent::from_str(&style) { - Ok(s) => [s] - .iter() - .map(|style| style.components(self.interactive_output)) - .fold(HashSet::new(), |mut acc, components| { - acc.extend(components.iter().cloned()); - acc - }), - Err(_) => HashSet::new(), - } + 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 + }) }, )) } From dccf8d82212e24e9015e4346a3d7f9330051a6ff Mon Sep 17 00:00:00 2001 From: sharkdp Date: Wed, 12 Sep 2018 21:35:23 +0200 Subject: [PATCH 5/5] 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()), } } }