Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): Propogate color #3014

Merged
merged 2 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}