on master help sign in
Merge pull request #298 from ms2300/bat_style

#208 Added BAT_STYLE env variable functionality

commitea369ee17f287309f746503233ebc6fcfbdeba2e authorDavid Peter <sharkdp@users.noreply.github.com> dateWed 12 Sep 2018, 21:48 (8 years ago) parent97129ab63d32bc
2 files changed+18−4
src/app.rs+16−3 src/style.rs+2−1
src/app.rs+16−3
@@ -1,6 +1,7 @@
11use std::collections::HashSet;
22use std::env;
33use std::path::Path;
4+use std::str::FromStr;
45
56use atty::{self, Stream};
67
@@ -178,14 +179,15 @@ impl App {
178179 .takes_value(true)
179180 .possible_values(&[
180181 "auto", "full", "plain", "changes", "header", "grid", "numbers",
181− ]).default_value("auto")
182+ ])
182183 .help("Comma-separated list of style elements to display.")
183184 .long_help(
184185 "Configure which elements (line numbers, file headers, grid \
185186 borders, Git modifications, ..) to display in addition to the \
186187 file contents. The argument is a comma-separated list of \
187188 components to display (e.g. 'numbers,changes,grid') or a \
188− pre-defined style ('full')",
189+ pre-defined style ('full'). To set a default theme, export the \
190+ BAT_STYLE environment variable (e.g.: export BAT_STYLE=\"numbers\").",
189191 ),
190192 ).arg(
191193 Arg::with_name("plain")
@@ -462,7 +464,18 @@ impl App {
462464 } else if matches.is_present("plain") {
463465 [OutputComponent::Plain].iter().cloned().collect()
464466 } else {
465− values_t!(matches.values_of("style"), OutputComponent)?
467+ let env_style_components: Option<Vec<OutputComponent>> =
468+ transpose(env::var("BAT_STYLE").ok().map(|style_str| {
469+ style_str
470+ .split(",")
471+ .map(|x| OutputComponent::from_str(&x))
472+ .collect::<Result<Vec<OutputComponent>>>()
473+ }))?;
474+
475+ values_t!(matches.values_of("style"), OutputComponent)
476+ .ok()
477+ .or(env_style_components)
478+ .unwrap_or(vec![OutputComponent::Full])
466479 .into_iter()
467480 .map(|style| style.components(self.interactive_output))
468481 .fold(HashSet::new(), |mut acc, components| {
src/style.rs+2−1
@@ -54,7 +54,8 @@ impl FromStr for OutputComponent {
5454 "header" => Ok(OutputComponent::Header),
5555 "numbers" => Ok(OutputComponent::Numbers),
5656 "full" => Ok(OutputComponent::Full),
57− "plain" | _ => Ok(OutputComponent::Plain),
57+ "plain" => Ok(OutputComponent::Plain),
58+ _ => Err(format!("Unknown style '{}'", s).into()),
5859 }
5960 }
6061}