From cac6f70e79cb4f8d24aa089a325933726c2c44fa Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 20 Feb 2024 11:16:30 -0600 Subject: [PATCH] fix(cli): Control clap colors through term.color Fixes #9012 --- src/bin/cargo/cli.rs | 26 +++++++++++++++++++++----- src/bin/cargo/commands/help.rs | 2 +- src/cargo/util/config/mod.rs | 12 ++++++------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 88c9a7f4a52f..f6c3f5f34970 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -1,5 +1,6 @@ use anyhow::{anyhow, Context as _}; use cargo::core::{features, CliUnstable}; +use cargo::util::config::TermConfig; use cargo::{drop_print, drop_println, CargoResult}; use clap::builder::UnknownArgumentValueParser; use itertools::Itertools; @@ -12,6 +13,7 @@ use super::commands; use super::list_commands; use crate::command_prelude::*; use crate::util::is_rustup; +use cargo::core::shell::ColorChoice; use cargo::util::style; pub fn main(gctx: &mut GlobalContext) -> CliResult { @@ -19,7 +21,7 @@ pub fn main(gctx: &mut GlobalContext) -> CliResult { // In general, try to avoid loading config values unless necessary (like // the [alias] table). - let args = cli().try_get_matches()?; + let args = cli(gctx).try_get_matches()?; // Update the process-level notion of cwd if let Some(new_cwd) = args.get_one::("directory") { @@ -172,7 +174,7 @@ Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder Some((cmd, args)) => (cmd, args), _ => { // No subcommand provided. - cli().print_help()?; + cli(gctx).print_help()?; return Ok(()); } }; @@ -335,7 +337,7 @@ For more information, see issue #12207 Command { +pub fn cli(gctx: &GlobalContext) -> Command { + // Don't let config errors get in the way of parsing arguments + let term = gctx.get::("term").unwrap_or_default(); + let color = term + .color + .and_then(|c| c.parse().ok()) + .unwrap_or(ColorChoice::CargoAuto); + let color = match color { + ColorChoice::Always => clap::ColorChoice::Always, + ColorChoice::Never => clap::ColorChoice::Never, + ColorChoice::CargoAuto => clap::ColorChoice::Auto, + }; + let usage = if is_rustup() { color_print::cstr!("cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript <> [ARGS]...") } else { @@ -536,6 +550,7 @@ pub fn cli() -> Command { // We also want these to come before auto-generated `--help` .next_display_order(800) .allow_external_subcommands(true) + .color(color) .styles(styles) // Provide a custom help subcommand for calling into man pages .disable_help_subcommand(true) @@ -645,7 +660,8 @@ See 'cargo help <>' for more information on a sp #[test] fn verify_cli() { - cli().debug_assert(); + let gctx = GlobalContext::default().unwrap(); + cli(&gctx).debug_assert(); } #[test] diff --git a/src/bin/cargo/commands/help.rs b/src/bin/cargo/commands/help.rs index 48d0df0ecf06..a92f5d140bcb 100644 --- a/src/bin/cargo/commands/help.rs +++ b/src/bin/cargo/commands/help.rs @@ -39,7 +39,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { } } } else { - let mut cmd = crate::cli::cli(); + let mut cmd = crate::cli::cli(gctx); let _ = cmd.print_help(); } Ok(()) diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 356209dcbf43..fe8383de2e9d 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -2636,14 +2636,14 @@ impl BuildTargetConfig { } #[derive(Deserialize, Default)] -struct TermConfig { - verbose: Option, - quiet: Option, - color: Option, - hyperlinks: Option, +pub struct TermConfig { + pub verbose: Option, + pub quiet: Option, + pub color: Option, + pub hyperlinks: Option, #[serde(default)] #[serde(deserialize_with = "progress_or_string")] - progress: Option, + pub progress: Option, } #[derive(Debug, Default, Deserialize)]