From bdeece88e466d817208a668beb889e25cc536d0b Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 9 Aug 2021 19:33:53 -0400 Subject: [PATCH] wip: color modular feature --- src/output/fmt.rs | 9 +++++++++ src/parse/errors.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/output/fmt.rs b/src/output/fmt.rs index e385defe..14c17c90 100644 --- a/src/output/fmt.rs +++ b/src/output/fmt.rs @@ -29,6 +29,15 @@ pub(crate) struct Colorizer { } impl Colorizer { + #[inline] + pub(crate) fn from_string(use_stderr: bool, color_when: ColorChoice, s: String) -> Self { + Colorizer { + use_stderr, + color_when, + pieces: vec![(s, None)], + } + } + #[inline] pub(crate) fn new(use_stderr: bool, color_when: ColorChoice) -> Self { Colorizer { diff --git a/src/parse/errors.rs b/src/parse/errors.rs index e09a1751..b8ce9434 100644 --- a/src/parse/errors.rs +++ b/src/parse/errors.rs @@ -873,6 +873,47 @@ impl Error { } } + #[cfg(not(any(feature = "color", feature = "full")))] + pub(crate) fn wrong_number_of_values( + arg: &Arg, + num_vals: usize, + curr_vals: usize, + usage: String, + color: ColorChoice, + ) -> Self { + // let verb = Error::singular_or_plural(curr_vals); + + // Error { + // message: Colorizer::from_string( + // true, + // color, + // format!("error: The argument '{}' requires {} values, but {} {} provided\n\n{}\n\nFor more information try --help\n", arg,num_vals, curr_vals, verb, usage)), + // kind: ErrorKind::WrongNumberOfValues, + // info: vec![arg.to_string(), curr_vals.to_string(), num_vals.to_string()], + // source: None, + // } + let mut c = Colorizer::new(true, color); + let verb = Error::singular_or_plural(curr_vals); + + start_error(&mut c, "The argument '"); + c.warning(arg.to_string()); + c.none("' requires "); + c.warning(num_vals.to_string()); + c.none(" values, but "); + c.warning(curr_vals.to_string()); + c.none(format!(" {} provided", verb)); + put_usage(&mut c, usage); + try_help(&mut c); + + Error { + message: c, + kind: ErrorKind::WrongNumberOfValues, + info: vec![arg.to_string(), curr_vals.to_string(), num_vals.to_string()], + source: None, + } + } + + #[cfg(any(feature = "color", feature = "full"))] pub(crate) fn wrong_number_of_values( arg: &Arg, num_vals: usize,