From ae674964734b013db4b2cf5eca94a3e62db79cc9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 11 Nov 2021 11:34:20 -0600 Subject: [PATCH 1/2] feat(app): Expose `App::get_color` This will be important for testing color support. No idea how much users will care. --- src/build/app/mod.rs | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index 44ac39534b5..f40b1abe8f1 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -315,6 +315,28 @@ impl<'help> App<'help> { self.settings.is_set(s) || self.g_settings.is_set(s) } + /// Should we color the output? + #[inline] + pub fn get_color(&self) -> ColorChoice { + debug!("App::color: Color setting..."); + + if cfg!(feature = "color") { + #[allow(deprecated)] + if self.is_set(AppSettings::ColorNever) { + debug!("Never"); + ColorChoice::Never + } else if self.is_set(AppSettings::ColorAlways) { + debug!("Always"); + ColorChoice::Always + } else { + debug!("Auto"); + ColorChoice::Auto + } + } else { + ColorChoice::Never + } + } + /// Returns `true` if this `App` has subcommands. #[inline] pub fn has_subcommands(&self) -> bool { @@ -2781,24 +2803,6 @@ impl<'help> App<'help> { self.args.args().find(|a| a.id == *arg_id) } - #[inline] - // Should we color the output? - pub(crate) fn get_color(&self) -> ColorChoice { - debug!("App::color: Color setting..."); - - #[allow(deprecated)] - if self.is_set(AppSettings::ColorNever) { - debug!("Never"); - ColorChoice::Never - } else if self.is_set(AppSettings::ColorAlways) { - debug!("Always"); - ColorChoice::Always - } else { - debug!("Auto"); - ColorChoice::Auto - } - } - #[inline] pub(crate) fn contains_short(&self, s: char) -> bool { assert!( From a2c3b14bb0b7701ecc289836d978b7010b721b70 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 11 Nov 2021 11:36:33 -0600 Subject: [PATCH 2/2] fix(app): Propogate color In #2851, we moved color from an AppSetting to function (with some tweaks in #2907). When doing this, we documented `App::color` to be equivalent of `App::global_settings(Color...)` but never actually propagated it. We are now propagating it. A test is added to ensure that no matter how we store the color choice, we continue to propagate it. This required exposing `App::get_color`. --- src/build/app/mod.rs | 6 +++--- tests/app_settings.rs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index f40b1abe8f1..d3498e2e503 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -1045,9 +1045,9 @@ impl<'help> App<'help> { pub fn color(self, color: ColorChoice) -> Self { #[allow(deprecated)] match color { - ColorChoice::Auto => self.setting(AppSettings::ColorAuto), - ColorChoice::Always => self.setting(AppSettings::ColorAlways), - ColorChoice::Never => self.setting(AppSettings::ColorNever), + ColorChoice::Auto => self.global_setting(AppSettings::ColorAuto), + ColorChoice::Always => self.global_setting(AppSettings::ColorAlways), + ColorChoice::Never => self.global_setting(AppSettings::ColorNever), } } diff --git a/tests/app_settings.rs b/tests/app_settings.rs index 95c90640317..1caea5ca935 100644 --- a/tests/app_settings.rs +++ b/tests/app_settings.rs @@ -1216,3 +1216,16 @@ fn no_auto_version_mut_arg() { assert!(result.is_ok()); assert!(result.unwrap().is_present("version")); } + +#[test] +#[cfg(feature = "color")] +fn color_is_global() { + let mut app = App::new("myprog") + .color(clap::ColorChoice::Never) + .subcommand(App::new("foo")); + app._build_all(); + assert_eq!(app.get_color(), clap::ColorChoice::Never); + + let sub = app.get_subcommands().collect::>()[0]; + assert_eq!(sub.get_color(), clap::ColorChoice::Never); +}