Skip to content

Commit

Permalink
Merge #3014
Browse files Browse the repository at this point in the history
3014: fix(app): Propogate color  r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
  • Loading branch information
bors[bot] and epage committed Nov 13, 2021
2 parents 4b70a6e + a2c3b14 commit ca3e14c
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 @@ -314,6 +314,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 @@ -1001,9 +1023,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 @@ -2759,24 +2781,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 ca3e14c

Please sign in to comment.