From 529028c961613e4c91a4960c31e5d46124a10379 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 18 Oct 2021 08:52:59 -0500 Subject: [PATCH] fix: Allow using deprecated color AppSettings This partiall reverts commit efeb02cd342af78d2eb8446ecce8aed05643594b, bringing back the `AppSettins::Color*` and making them the backing store for `App::color`, to help ease the transition from clap2->3. Once we remove these deprecated settings, we might want to keep this backing store to save on memory. This is a part of #2617 --- src/build/app/mod.rs | 26 +++++++++++++++++++++----- src/build/app/settings.rs | 23 ++++++++++++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index 8f1e48d8bbc6..b32d0180ad2e 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -90,7 +90,6 @@ pub struct App<'help> { pub(crate) template: Option<&'help str>, pub(crate) settings: AppFlags, pub(crate) g_settings: AppFlags, - pub(crate) color: ColorChoice, pub(crate) args: MKeyMap<'help>, pub(crate) subcommands: Vec>, pub(crate) replacers: HashMap<&'help str, &'help [&'help str]>, @@ -1021,9 +1020,13 @@ impl<'help> App<'help> { /// [`ColorChoice::Auto`]: crate::ColorChoice::Auto #[cfg(feature = "color")] #[inline] - pub fn color(mut self, color: ColorChoice) -> Self { - self.color = color; - self + 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), + } } /// Sets the terminal width at which to wrap help messages. Defaults to @@ -2810,8 +2813,21 @@ impl<'help> App<'help> { } #[inline] + // Should we color the output? pub(crate) fn get_color(&self) -> ColorChoice { - self.color + 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] diff --git a/src/build/app/settings.rs b/src/build/app/settings.rs index e0b6f7c96ad9..0c8bd41fcdbb 100644 --- a/src/build/app/settings.rs +++ b/src/build/app/settings.rs @@ -25,6 +25,9 @@ bitflags! { const NO_POS_VALUES = 1 << 17; const NEXT_LINE_HELP = 1 << 18; const DERIVE_DISP_ORDER = 1 << 19; + const COLOR_ALWAYS = 1 << 21; + const COLOR_AUTO = 1 << 22; + const COLOR_NEVER = 1 << 23; const DONT_DELIM_TRAIL = 1 << 24; const ALLOW_NEG_NUMS = 1 << 25; const DISABLE_HELP_SC = 1 << 27; @@ -56,7 +59,7 @@ pub struct AppFlags(Flags); impl Default for AppFlags { fn default() -> Self { - Self::empty() + AppFlags(Flags::COLOR_AUTO) } } @@ -77,6 +80,12 @@ impl_settings! { AppSettings, AppFlags, => Flags::ALLOW_NEG_NUMS, AllowMissingPositional("allowmissingpositional") => Flags::ALLOW_MISSING_POS, + ColorAlways("coloralways") + => Flags::COLOR_ALWAYS, + ColorAuto("colorauto") + => Flags::COLOR_AUTO, + ColorNever("colornever") + => Flags::COLOR_NEVER, DontDelimitTrailingValues("dontdelimittrailingvalues") => Flags::DONT_DELIM_TRAIL, DontCollapseArgsInUsage("dontcollapseargsinusage") @@ -489,6 +498,18 @@ pub enum AppSettings { /// ``` SubcommandPrecedenceOverArg, + /// Deprecated, see [`App::color`] + #[deprecated(since = "3.0.0", note = "Replaced with `App::color`")] + ColorAuto, + + /// Deprecated, see [`App::color`] + #[deprecated(since = "3.0.0", note = "Replaced with `App::color`")] + ColorAlways, + + /// Deprecated, see [`App::color`] + #[deprecated(since = "3.0.0", note = "Replaced with `App::color`")] + ColorNever, + /// Disables the automatic collapsing of positional args into `[ARGS]` inside the usage string /// /// # Examples