Skip to content

Commit

Permalink
refactor(help): Clarify style meaning
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 18, 2023
1 parent 7cf08e6 commit 8fd0a93
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 88 deletions.
34 changes: 17 additions & 17 deletions clap_builder/src/builder/styled_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,30 +203,30 @@ impl std::fmt::Display for StyledStr {
#[non_exhaustive]
#[allow(missing_copy_implementations)] // Large enough type that I want an explicit `clone()` for now
pub struct Styles {
/// Heading style, e.g. [`help_heading`][crate::Arg::help_heading]
/// General Heading style, e.g. [`help_heading`][crate::Arg::help_heading]
pub header: anstyle::Style,
/// Literal command-line syntax, like `--help`
pub literal: anstyle::Style,
/// Descriptions within command-line syntax, like [`value_name`][crate::Arg::value_name]
pub placeholder: anstyle::Style,
/// Suggested usage
pub good: anstyle::Style,
/// Invalid usage
pub warning: anstyle::Style,
/// Error heading
pub error: anstyle::Style,
/// Literal command-line syntax, e.g. `--help`
pub literal: anstyle::Style,
/// Descriptions within command-line syntax, e.g. [`value_name`][crate::Arg::value_name]
pub placeholder: anstyle::Style,
/// Highlight suggested usage
pub valid: anstyle::Style,
/// Highlight invalid usage
pub invalid: anstyle::Style,
}

impl Styles {
/// No terminal styling
pub const fn plain() -> Self {
Self {
header: anstyle::Style::new(),
error: anstyle::Style::new(),
literal: anstyle::Style::new(),
placeholder: anstyle::Style::new(),
good: anstyle::Style::new(),
warning: anstyle::Style::new(),
error: anstyle::Style::new(),
valid: anstyle::Style::new(),
invalid: anstyle::Style::new(),
}
}

Expand All @@ -236,15 +236,15 @@ impl Styles {
{
Self {
header: anstyle::Style::new().bold().underline(),
error: anstyle::Style::new()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red)))
.bold(),
literal: anstyle::Style::new().bold(),
placeholder: anstyle::Style::new(),
good: anstyle::Style::new()
valid: anstyle::Style::new()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green))),
warning: anstyle::Style::new()
invalid: anstyle::Style::new()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
error: anstyle::Style::new()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red)))
.bold(),
}
}
#[cfg(not(feature = "color"))]
Expand Down
116 changes: 65 additions & 51 deletions clap_builder/src/error/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ErrorFormatter for RichFormatter {
fn format_error(error: &crate::error::Error<Self>) -> StyledStr {
use std::fmt::Write as _;
let styles = &error.inner.styles;
let good = &styles.good;
let valid = &styles.valid;

let mut styled = StyledStr::new();
start_error(&mut styled, styles);
Expand Down Expand Up @@ -108,8 +108,8 @@ impl ErrorFormatter for RichFormatter {
let _ = write!(
styled,
"\n{TAB}{}tip:{} ",
good.render(),
good.render_reset()
valid.render(),
valid.render_reset()
);
styled.push_styled(suggestion);
}
Expand Down Expand Up @@ -140,8 +140,8 @@ fn write_dynamic_context(
styles: &Styles,
) -> bool {
use std::fmt::Write as _;
let good = styles.good;
let warning = styles.warning;
let valid = styles.valid;
let invalid = styles.invalid;
let literal = styles.literal;

match error.kind() {
Expand All @@ -155,15 +155,15 @@ fn write_dynamic_context(
let _ = write!(
styled,
"the argument '{}{invalid_arg}{}' cannot be used multiple times",
warning.render(),
warning.render_reset()
invalid.render(),
invalid.render_reset()
);
} else {
let _ = write!(
styled,
"the argument '{}{invalid_arg}{}' cannot be used with",
warning.render(),
warning.render_reset()
invalid.render(),
invalid.render_reset()
);

match prior_arg {
Expand All @@ -173,17 +173,17 @@ fn write_dynamic_context(
let _ = write!(
styled,
"\n{TAB}{}{v}{}",
warning.render(),
warning.render_reset()
invalid.render(),
invalid.render_reset()
);
}
}
ContextValue::String(value) => {
let _ = write!(
styled,
" '{}{value}{}'",
warning.render(),
warning.render_reset()
invalid.render(),
invalid.render_reset()
);
}
_ => {
Expand All @@ -202,8 +202,8 @@ fn write_dynamic_context(
let _ = write!(
styled,
"equal sign is needed when assigning values to '{}{invalid_arg}{}'",
warning.render(),
warning.render_reset()
invalid.render(),
invalid.render_reset()
);
true
} else {
Expand All @@ -222,15 +222,15 @@ fn write_dynamic_context(
let _ = write!(
styled,
"a value is required for '{}{invalid_arg}{}' but none was supplied",
warning.render(),
warning.render_reset()
invalid.render(),
invalid.render_reset()
);
} else {
let _ = write!(
styled,
"invalid value '{}{invalid_value}{}' for '{}{invalid_arg}{}'",
warning.render(),
warning.render_reset(),
invalid.render(),
invalid.render_reset(),
literal.render(),
literal.render_reset()
);
Expand All @@ -245,17 +245,17 @@ fn write_dynamic_context(
let _ = write!(
styled,
"{}{}{}, ",
good.render(),
valid.render(),
Escape(v),
good.render_reset()
valid.render_reset()
);
}
let _ = write!(
styled,
"{}{}{}",
good.render(),
valid.render(),
Escape(last),
good.render_reset()
valid.render_reset()
);
}
styled.push_str("]");
Expand All @@ -272,8 +272,8 @@ fn write_dynamic_context(
let _ = write!(
styled,
"unrecognized subcommand '{}{invalid_sub}{}'",
warning.render(),
warning.render_reset()
invalid.render(),
invalid.render_reset()
);
true
} else {
Expand All @@ -285,7 +285,12 @@ fn write_dynamic_context(
if let Some(ContextValue::Strings(invalid_arg)) = invalid_arg {
styled.push_str("the following required arguments were not provided:");
for v in invalid_arg {
let _ = write!(styled, "\n{TAB}{}{v}{}", good.render(), good.render_reset());
let _ = write!(
styled,
"\n{TAB}{}{v}{}",
valid.render(),
valid.render_reset()
);
}
true
} else {
Expand All @@ -298,8 +303,8 @@ fn write_dynamic_context(
let _ = write!(
styled,
"'{}{invalid_sub}{}' requires a subcommand but one was not provided",
warning.render(),
warning.render_reset()
invalid.render(),
invalid.render_reset()
);

let possible_values = error.get(ContextKind::ValidSubcommand);
Expand All @@ -311,17 +316,17 @@ fn write_dynamic_context(
let _ = write!(
styled,
"{}{}{}, ",
good.render(),
valid.render(),
Escape(v),
good.render_reset()
valid.render_reset()
);
}
let _ = write!(
styled,
"{}{}{}",
good.render(),
valid.render(),
Escape(last),
good.render_reset()
valid.render_reset()
);
}
styled.push_str("]");
Expand All @@ -345,8 +350,8 @@ fn write_dynamic_context(
let _ = write!(
styled,
"unexpected value '{}{invalid_value}{}' for '{}{invalid_arg}{}' found; no more were expected",
warning.render(),
warning.render_reset(),
invalid.render(),
invalid.render_reset(),
literal.render(),
literal.render_reset(),
);
Expand All @@ -369,12 +374,12 @@ fn write_dynamic_context(
let _ = write!(
styled,
"{}{min_values}{} more values required by '{}{invalid_arg}{}'; only {}{actual_num_values}{}{were_provided}",
good.render(),
good.render_reset(),
valid.render(),
valid.render_reset(),
literal.render(),
literal.render_reset(),
warning.render(),
warning.render_reset(),
invalid.render(),
invalid.render_reset(),
);
true
} else {
Expand All @@ -392,8 +397,8 @@ fn write_dynamic_context(
let _ = write!(
styled,
"invalid value '{}{invalid_value}{}' for '{}{invalid_arg}{}'",
warning.render(),
warning.render_reset(),
invalid.render(),
invalid.render_reset(),
literal.render(),
literal.render_reset(),
);
Expand All @@ -419,12 +424,12 @@ fn write_dynamic_context(
let _ = write!(
styled,
"{}{num_values}{} values required for '{}{invalid_arg}{}' but {}{actual_num_values}{}{were_provided}",
good.render(),
good.render_reset(),
valid.render(),
valid.render_reset(),
literal.render(),
literal.render_reset(),
warning.render(),
warning.render_reset(),
invalid.render(),
invalid.render_reset(),
);
true
} else {
Expand All @@ -437,8 +442,8 @@ fn write_dynamic_context(
let _ = write!(
styled,
"unexpected argument '{}{invalid_arg}{}' found",
warning.render(),
warning.render_reset(),
invalid.render(),
invalid.render_reset(),
);
true
} else {
Expand Down Expand Up @@ -513,15 +518,19 @@ fn try_help(styled: &mut StyledStr, styles: &Styles, help: Option<&str>) {
#[cfg(feature = "error-context")]
fn did_you_mean(styled: &mut StyledStr, styles: &Styles, context: &str, valid: &ContextValue) {
use std::fmt::Write as _;
let good = &styles.good;

let _ = write!(styled, "{TAB}{}tip:{}", good.render(), good.render_reset());
let _ = write!(
styled,
"{TAB}{}tip:{}",
styles.valid.render(),
styles.valid.render_reset()
);
if let ContextValue::String(valid) = valid {
let _ = write!(
styled,
" a similar {context} exists: '{}{valid}{}'",
good.render(),
good.render_reset()
styles.valid.render(),
styles.valid.render_reset()
);
} else if let ContextValue::Strings(valid) = valid {
if valid.len() == 1 {
Expand All @@ -533,7 +542,12 @@ fn did_you_mean(styled: &mut StyledStr, styles: &Styles, context: &str, valid: &
if i != 0 {
styled.push_str(", ");
}
let _ = write!(styled, "'{}{valid}{}'", good.render(), good.render_reset());
let _ = write!(
styled,
"'{}{valid}{}'",
styles.valid.render(),
styles.valid.render_reset()
);
}
}
}
Expand Down
Loading

0 comments on commit 8fd0a93

Please sign in to comment.