Skip to content

Commit 5a8ef3f

Browse files
committed
fix(cli): Control clap colors through term.color
Fixes #9012
1 parent ecdf8ec commit 5a8ef3f

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/bin/cargo/cli.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::{anyhow, Context as _};
22
use cargo::core::{features, CliUnstable};
3+
use cargo::util::config::TermConfig;
34
use cargo::{self, drop_print, drop_println, CargoResult, CliResult, Config};
45
use clap::{builder::UnknownArgumentValueParser, Arg, ArgMatches};
56
use itertools::Itertools;
@@ -12,14 +13,15 @@ use super::commands;
1213
use super::list_commands;
1314
use crate::command_prelude::*;
1415
use crate::util::is_rustup;
16+
use cargo::core::shell::ColorChoice;
1517
use cargo::util::style;
1618

1719
pub fn main(config: &mut Config) -> CliResult {
1820
// CAUTION: Be careful with using `config` until it is configured below.
1921
// In general, try to avoid loading config values unless necessary (like
2022
// the [alias] table).
2123

22-
let args = cli().try_get_matches()?;
24+
let args = cli(config).try_get_matches()?;
2325

2426
// Update the process-level notion of cwd
2527
if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
@@ -172,7 +174,7 @@ Run with `{literal}cargo -Z{literal:#} {placeholder}[FLAG] [COMMAND]{placeholder
172174
Some((cmd, args)) => (cmd, args),
173175
_ => {
174176
// No subcommand provided.
175-
cli().print_help()?;
177+
cli(config).print_help()?;
176178
return Ok(());
177179
}
178180
};
@@ -335,7 +337,9 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
335337
// Note that an alias to an external command will not receive
336338
// these arguments. That may be confusing, but such is life.
337339
let global_args = GlobalArgs::new(sub_args);
338-
let new_args = cli().no_binary_name(true).try_get_matches_from(alias)?;
340+
let new_args = cli(config)
341+
.no_binary_name(true)
342+
.try_get_matches_from(alias)?;
339343

340344
let new_cmd = new_args.subcommand_name().expect("subcommand is required");
341345
already_expanded.push(cmd.to_string());
@@ -511,7 +515,19 @@ impl GlobalArgs {
511515
}
512516
}
513517

514-
pub fn cli() -> Command {
518+
pub fn cli(config: &Config) -> Command {
519+
// Don't let config errors get in the way of parsing arguments
520+
let term = config.get::<TermConfig>("term").unwrap_or_default();
521+
let color = term
522+
.color
523+
.and_then(|c| c.parse().ok())
524+
.unwrap_or(ColorChoice::CargoAuto);
525+
let color = match color {
526+
ColorChoice::Always => clap::ColorChoice::Always,
527+
ColorChoice::Never => clap::ColorChoice::Never,
528+
ColorChoice::CargoAuto => clap::ColorChoice::Auto,
529+
};
530+
515531
let usage = if is_rustup() {
516532
color_print::cstr!("<cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS] [COMMAND]</>\n <cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS]</> <cyan,bold>-Zscript</> <cyan><<MANIFEST_RS>> [ARGS]...</>")
517533
} else {
@@ -536,6 +552,7 @@ pub fn cli() -> Command {
536552
// We also want these to come before auto-generated `--help`
537553
.next_display_order(800)
538554
.allow_external_subcommands(true)
555+
.color(color)
539556
.styles(styles)
540557
// Provide a custom help subcommand for calling into man pages
541558
.disable_help_subcommand(true)

src/bin/cargo/commands/help.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
3939
}
4040
}
4141
} else {
42-
let mut cmd = crate::cli::cli();
42+
let mut cmd = crate::cli::cli(config);
4343
let _ = cmd.print_help();
4444
}
4545
Ok(())

src/cargo/util/config/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,14 +2626,14 @@ impl BuildTargetConfig {
26262626
}
26272627

26282628
#[derive(Deserialize, Default)]
2629-
struct TermConfig {
2630-
verbose: Option<bool>,
2631-
quiet: Option<bool>,
2632-
color: Option<String>,
2633-
hyperlinks: Option<bool>,
2629+
pub struct TermConfig {
2630+
pub verbose: Option<bool>,
2631+
pub quiet: Option<bool>,
2632+
pub color: Option<String>,
2633+
pub hyperlinks: Option<bool>,
26342634
#[serde(default)]
26352635
#[serde(deserialize_with = "progress_or_string")]
2636-
progress: Option<ProgressConfig>,
2636+
pub progress: Option<ProgressConfig>,
26372637
}
26382638

26392639
#[derive(Debug, Default, Deserialize)]

0 commit comments

Comments
 (0)