Skip to content

Commit

Permalink
fix: Allow using deprecated color AppSettings
Browse files Browse the repository at this point in the history
This partiall reverts commit efeb02c,
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 clap-rs#2617
  • Loading branch information
epage committed Oct 27, 2021
1 parent 88b8931 commit 9905801
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
26 changes: 21 additions & 5 deletions src/build/app/mod.rs
Expand Up @@ -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<App<'help>>,
pub(crate) replacers: HashMap<&'help str, &'help [&'help str]>,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2818,8 +2821,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]
Expand Down
23 changes: 22 additions & 1 deletion src/build/app/settings.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -56,7 +59,7 @@ pub struct AppFlags(Flags);

impl Default for AppFlags {
fn default() -> Self {
Self::empty()
AppFlags(Flags::COLOR_AUTO)
}
}

Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9905801

Please sign in to comment.