From d1bcfd38fc2df31f08cb265ee932859b03fe4931 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 3 Sep 2020 09:13:33 -0400 Subject: Allow navigate to be activated with DELTA_NAVIGATE env var --- src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index 6c29f9dc..587bb3c0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -228,7 +228,7 @@ pub struct Opt { /// Emulate diff-so-fancy (https://github.com/so-fancy/diff-so-fancy) pub diff_so_fancy: bool, - #[structopt(long = "navigate")] + #[structopt(long = "navigate", env = "DELTA_NAVIGATE")] /// Activate diff navigation: use n to jump forwards and N to jump backwards. To change the /// file labels used see --file-modified-label, --file-removed-label, --file-added-label, /// --file-renamed-label. -- cgit v1.2.3 From ba6535ea4169128f35c478bf30b409bd3f9d0673 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 3 Sep 2020 09:35:33 -0400 Subject: Revert "Allow navigate to be activated with DELTA_NAVIGATE env var" This reverts commit d718a3fc75f1796a24b4b9286308876b6e4a92c7. Using env vars for boolean flags like this should perhaps wait for changes in clap. See https://github.com/clap-rs/clap/issues/1476 https://github.com/TeXitoi/structopt/issues/372 --- src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index 587bb3c0..6c29f9dc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -228,7 +228,7 @@ pub struct Opt { /// Emulate diff-so-fancy (https://github.com/so-fancy/diff-so-fancy) pub diff_so_fancy: bool, - #[structopt(long = "navigate", env = "DELTA_NAVIGATE")] + #[structopt(long = "navigate")] /// Activate diff navigation: use n to jump forwards and N to jump backwards. To change the /// file labels used see --file-modified-label, --file-removed-label, --file-added-label, /// --file-renamed-label. -- cgit v1.2.3 From 8f88b5b1eb085cd87f27cc1679b8be17a6719912 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 3 Sep 2020 09:51:32 -0400 Subject: Hack: env var for boolean flag The proper way would be using env = "DELTA_NAVIGATE", but see https://github.com/clap-rs/clap/issues/1476 https://github.com/TeXitoi/structopt/issues/372 Doing it as in this commit probably has drawbacks, e.g. related to whether the argument is recorded as having been supplied. --- src/env.rs | 9 +++++++++ src/options/set.rs | 1 + 2 files changed, 10 insertions(+) diff --git a/src/env.rs b/src/env.rs index 70811353..dc30ecca 100644 --- a/src/env.rs +++ b/src/env.rs @@ -8,3 +8,12 @@ pub fn get_env_var(name: &str) -> Option { non_empty_string => Some(non_empty_string.to_string()), } } + +pub fn get_boolean_env_var(name: &str) -> bool { + let val = get_env_var(name).map(|s| s.to_lowercase()); + match val.as_deref() { + None => false, + Some("false") => false, + Some(_) => true, + } +} diff --git a/src/options/set.rs b/src/options/set.rs index b8ea10a7..a6b4f151 100644 --- a/src/options/set.rs +++ b/src/options/set.rs @@ -74,6 +74,7 @@ pub fn set_options( } set_git_config_entries(opt, git_config); } + opt.navigate = opt.navigate || env::get_boolean_env_var("DELTA_NAVIGATE"); let option_names = cli::Opt::get_option_names(); -- cgit v1.2.3 From 2efb9d5c11d29a1ea25c7b8c74f3598dfeffa7a0 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 3 Sep 2020 09:53:42 -0400 Subject: Interpret an env var as boolean false iff it is unset If it is set to any value, including "" (and of course "false") then it is true. This matches the semantics which clap seems likely to adopt: https://github.com/clap-rs/clap/issues/1476#issuecomment-652344026 --- src/env.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/env.rs b/src/env.rs index dc30ecca..0a988f9a 100644 --- a/src/env.rs +++ b/src/env.rs @@ -9,11 +9,7 @@ pub fn get_env_var(name: &str) -> Option { } } +/// If `name` is set to any value at all (including "") then return true; else false. pub fn get_boolean_env_var(name: &str) -> bool { - let val = get_env_var(name).map(|s| s.to_lowercase()); - match val.as_deref() { - None => false, - Some("false") => false, - Some(_) => true, - } + env::var(name).ok().is_some() } -- cgit v1.2.3