Skip to content

Commit

Permalink
Capture env var configurations using clap
Browse files Browse the repository at this point in the history
Parses env configurations into matches (for those that can be configured
via matches) instead of relying on dedicated env checks everywhere.

Fixes sharkdp#1152
  • Loading branch information
jacobmischka committed Jan 2, 2022
1 parent 080c5f3 commit f661aa0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
13 changes: 0 additions & 13 deletions src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ impl App {
.matches
.value_of("tabs")
.map(String::from)
.or_else(|| env::var("BAT_TABS").ok())
.and_then(|t| t.parse().ok())
.unwrap_or(
if style_components.plain() && paging_mode == PagingMode::Never {
Expand All @@ -201,7 +200,6 @@ impl App {
.matches
.value_of("theme")
.map(String::from)
.or_else(|| env::var("BAT_THEME").ok())
.map(|s| {
if s == "default" {
String::from(HighlightingAssets::default_theme())
Expand Down Expand Up @@ -302,16 +300,6 @@ impl App {
} else if matches.is_present("plain") {
[StyleComponent::Plain].iter().cloned().collect()
} else {
let env_style_components: Option<Vec<StyleComponent>> = env::var("BAT_STYLE")
.ok()
.map(|style_str| {
style_str
.split(',')
.map(StyleComponent::from_str)
.collect::<Result<Vec<StyleComponent>>>()
})
.transpose()?;

matches
.value_of("style")
.map(|styles| {
Expand All @@ -321,7 +309,6 @@ impl App {
.filter_map(|style| style.ok())
.collect::<Vec<_>>()
})
.or(env_style_components)
.unwrap_or_else(|| vec![StyleComponent::Full])
.into_iter()
.map(|style| style.components(self.interactive_output))
Expand Down
4 changes: 4 additions & 0 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
app = app.arg(
Arg::with_name("tabs")
.long("tabs")
.env("BAT_TABS")
.overrides_with("tabs")
.takes_value(true)
.value_name("T")
Expand Down Expand Up @@ -319,6 +320,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.arg(
Arg::with_name("pager")
.long("pager")
.env("BAT_PAGER")
.overrides_with("pager")
.takes_value(true)
.value_name("command")
Expand Down Expand Up @@ -352,6 +354,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.arg(
Arg::with_name("theme")
.long("theme")
.env("BAT_THEME")
.overrides_with("theme")
.takes_value(true)
.help("Set the color theme for syntax highlighting.")
Expand All @@ -373,6 +376,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
Arg::with_name("style")
.long("style")
.value_name("components")
.env("BAT_STYLE")
// Need to turn this off for overrides_with to work as we want. See the bottom most
// example at https://docs.rs/clap/2.32.0/clap/struct.Arg.html#method.overrides_with
.use_delimiter(false)
Expand Down
24 changes: 24 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,17 @@ fn pager_basic() {
.stdout(predicate::eq("pager-output\n").normalize());
}

#[test]
fn pager_config() {
bat()
.arg("--pager=echo pager-output")
.arg("--paging=always")
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("pager-output\n").normalize());
}

#[test]
fn pager_overwrite() {
bat()
Expand All @@ -549,6 +560,19 @@ fn pager_overwrite() {
.stdout(predicate::eq("pager-output\n").normalize());
}

#[test]
fn pager_overwrite_overwrite() {
bat()
.env("PAGER", "echo other-pager")
.env("BAT_PAGER", "echo another-pager")
.arg("--pager=echo pager-output")
.arg("--paging=always")
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("pager-output\n").normalize());
}

#[test]
fn pager_disable() {
bat()
Expand Down

0 comments on commit f661aa0

Please sign in to comment.