summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Peter <sharkdp@users.noreply.github.com>2018-09-12 21:48:34 +0200
committerGitHub <noreply@github.com>2018-09-12 21:48:34 +0200
commitea369ee17f287309f746503233ebc6fcfbdeba2e (patch)
tree93aa27f5cbac2223f96b9671f5ecf8a1ededa34d
parent97129ab9d8b249611b31740ad597452079458afc (diff)
parent63d32bc818b8a8375ddf70d069e7ee903b63d55c (diff)
Merge pull request #298 from ms2300/bat_style
#208 Added BAT_STYLE env variable functionality
-rw-r--r--src/app.rs19
-rw-r--r--src/style.rs3
2 files changed, 18 insertions, 4 deletions
diff --git a/src/app.rs b/src/app.rs
index 5b00658c..d2438ba4 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};
@@ -178,14 +179,15 @@ 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 \
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")
@@ -462,7 +464,18 @@ impl App {
} else if matches.is_present("plain") {
[OutputComponent::Plain].iter().cloned().collect()
} else {
- values_t!(matches.values_of("style"), OutputComponent)?
+ let env_style_components: Option<Vec<OutputComponent>> =
+ transpose(env::var("BAT_STYLE").ok().map(|style_str| {
+ style_str
+ .split(",")
+ .map(|x| OutputComponent::from_str(&x))
+ .collect::<Result<Vec<OutputComponent>>>()
+ }))?;
+
+ 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| {
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()),
}
}
}