From b21b96f5008fe374083545401088f1383d3c9829 Mon Sep 17 00:00:00 2001 From: m Date: Mon, 8 Apr 2024 13:39:23 -0700 Subject: [PATCH] switch to `colorchoice-clap` crate for color handling it still breaks tests, i think it's an issue with how we handle the `check-release` subcommand --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/main.rs | 33 ++++----------------------------- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c470d5e9..4fdf9698 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,6 +296,7 @@ dependencies = [ "clap", "clap-cargo", "clap-verbosity-flag", + "colorchoice-clap", "directories", "gix", "handlebars", @@ -436,6 +437,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "colorchoice-clap" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe9ad5d064caf028aeb50818a5c7857c28f746ad04111652b85d9bca8b0d0be" +dependencies = [ + "clap", + "colorchoice", +] + [[package]] name = "console" version = "0.15.8" diff --git a/Cargo.toml b/Cargo.toml index af01b256..ed2a0220 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ rustc_version = "0.4.0" rayon = "1.8.0" anstyle = "1.0.6" anstream = "0.6.13" +colorchoice-clap = "1.0.3" [dev-dependencies] assert_cmd = "2.0" diff --git a/src/main.rs b/src/main.rs index 84d976df..f48089bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,16 +5,14 @@ use std::path::PathBuf; use cargo_semver_checks::{ GlobalConfig, PackageSelection, ReleaseType, Rustdoc, ScopeSelection, SemverQuery, }; -use clap::{builder::PossibleValue, Args, Parser, Subcommand, ValueEnum}; +use clap::{Args, Parser, Subcommand}; fn main() { human_panic::setup_panic!(); let Cargo::SemverChecks(args) = Cargo::parse(); - if let Some(color_choice) = args.check_release.color_choice { - color_choice.0.write_global(); - } + args.check_release.color_choice.write_global(); if args.bugreport { use bugreport::{bugreport, collector::*, format::Markdown}; @@ -111,29 +109,6 @@ fn exit_on_error(log_errors: bool, inner: impl Fn() -> anyhow::Result) -> } } -/// helper enum to derive [`clap::ValueEnum`] on [`anstream::ColorChoice`] -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub(crate) struct ColorChoice(pub(crate) anstream::ColorChoice); - -impl ValueEnum for ColorChoice { - fn value_variants<'a>() -> &'a [Self] { - use anstream::ColorChoice::*; - &[Self(Always), Self(AlwaysAnsi), Self(Auto), Self(Never)] - } - - fn to_possible_value(&self) -> Option { - use anstream::ColorChoice::*; - let name = match self.0 { - Always => "always", - AlwaysAnsi => "always-ansi", - Auto => "auto", - Never => "never", - }; - - Some(PossibleValue::new(name)) - } -} - #[derive(Debug, Parser)] #[command(name = "cargo")] #[command(bin_name = "cargo")] @@ -317,8 +292,8 @@ struct CheckRelease { /// auto (based on whether output is a tty), and never /// /// Default is auto (use colors if output is a TTY, otherwise don't use colors) - #[arg(value_enum, long = "color")] - color_choice: Option, + #[command(flatten)] + color_choice: colorchoice_clap::Color, } impl From for cargo_semver_checks::Check {