Skip to content

Commit

Permalink
Merge a2c3b14 into 00a0c4e
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 11, 2021
2 parents 00a0c4e + a2c3b14 commit d5e4e48
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1023,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),
}
}

Expand Down Expand Up @@ -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!(
Expand Down
13 changes: 13 additions & 0 deletions tests/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()[0];
assert_eq!(sub.get_color(), clap::ColorChoice::Never);
}

0 comments on commit d5e4e48

Please sign in to comment.