Skip to content

Commit

Permalink
fix(cli): Control clap colors through term.color
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 20, 2024
1 parent f3627f0 commit ae6920a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
28 changes: 23 additions & 5 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,14 +13,15 @@ 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(config: &mut Config) -> CliResult {
// CAUTION: Be careful with using `config` until it is configured below.
// In general, try to avoid loading config values unless necessary (like
// the [alias] table).

let args = cli().try_get_matches()?;
let args = cli(config).try_get_matches()?;

// Update the process-level notion of cwd
if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
Expand Down Expand Up @@ -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(config).print_help()?;
return Ok(());
}
};
Expand Down Expand Up @@ -335,7 +337,9 @@ For more information, see issue #12207 <https://github.com/rust-lang/cargo/issue
// Note that an alias to an external command will not receive
// these arguments. That may be confusing, but such is life.
let global_args = GlobalArgs::new(sub_args);
let new_args = cli().no_binary_name(true).try_get_matches_from(alias)?;
let new_args = cli(config)
.no_binary_name(true)
.try_get_matches_from(alias)?;

let new_cmd = new_args.subcommand_name().expect("subcommand is required");
already_expanded.push(cmd.to_string());
Expand Down Expand Up @@ -511,7 +515,19 @@ impl GlobalArgs {
}
}

pub fn cli() -> Command {
pub fn cli(config: &Config) -> Command {
// Don't let config errors get in the way of parsing arguments
let term = config.get::<TermConfig>("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!("<cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS] [COMMAND]</>\n <cyan,bold>cargo</> <cyan>[+toolchain] [OPTIONS]</> <cyan,bold>-Zscript</> <cyan><<MANIFEST_RS>> [ARGS]...</>")
} else {
Expand All @@ -536,6 +552,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)
Expand Down Expand Up @@ -645,7 +662,8 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp

#[test]
fn verify_cli() {
cli().debug_assert();
let config = Config::default().unwrap();
cli(&config).debug_assert();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
}
}
} else {
let mut cmd = crate::cli::cli();
let mut cmd = crate::cli::cli(config);
let _ = cmd.print_help();
}
Ok(())
Expand Down
12 changes: 6 additions & 6 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2626,14 +2626,14 @@ impl BuildTargetConfig {
}

#[derive(Deserialize, Default)]
struct TermConfig {
verbose: Option<bool>,
quiet: Option<bool>,
color: Option<String>,
hyperlinks: Option<bool>,
pub struct TermConfig {
pub verbose: Option<bool>,
pub quiet: Option<bool>,
pub color: Option<String>,
pub hyperlinks: Option<bool>,
#[serde(default)]
#[serde(deserialize_with = "progress_or_string")]
progress: Option<ProgressConfig>,
pub progress: Option<ProgressConfig>,
}

#[derive(Debug, Default, Deserialize)]
Expand Down

0 comments on commit ae6920a

Please sign in to comment.