Skip to content

Commit

Permalink
Merge pull request #5721 from epage/update
Browse files Browse the repository at this point in the history
fix(help): Style arg groups
  • Loading branch information
epage authored Sep 4, 2024
2 parents e09f793 + 75365ad commit 1192002
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 266 deletions.
32 changes: 6 additions & 26 deletions clap_builder/src/builder/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4329,14 +4329,9 @@ impl Arg {
let mut styled = StyledStr::new();
// Write the name such --long or -l
if let Some(l) = self.get_long() {
let _ = write!(
styled,
"{}--{l}{}",
literal.render(),
literal.render_reset()
);
let _ = write!(styled, "{literal}--{l}{literal:#}",);
} else if let Some(s) = self.get_short() {
let _ = write!(styled, "{}-{s}{}", literal.render(), literal.render_reset());
let _ = write!(styled, "{literal}-{s}{literal:#}");
}
styled.push_styled(&self.stylize_arg_suffix(styles, required));
styled
Expand Down Expand Up @@ -4364,32 +4359,17 @@ impl Arg {
} else {
(placeholder, " ")
};
let _ = write!(styled, "{}{start}{}", style.render(), style.render_reset());
let _ = write!(styled, "{style}{start}{style:#}");
}
if self.is_takes_value_set() || self.is_positional() {
let required = required.unwrap_or_else(|| self.is_required_set());
let arg_val = self.render_arg_val(required);
let _ = write!(
styled,
"{}{arg_val}{}",
placeholder.render(),
placeholder.render_reset()
);
let _ = write!(styled, "{placeholder}{arg_val}{placeholder:#}",);
} else if matches!(*self.get_action(), ArgAction::Count) {
let _ = write!(
styled,
"{}...{}",
placeholder.render(),
placeholder.render_reset()
);
let _ = write!(styled, "{placeholder}...{placeholder:#}",);
}
if need_closing_bracket {
let _ = write!(
styled,
"{}]{}",
placeholder.render(),
placeholder.render_reset()
);
let _ = write!(styled, "{placeholder}]{placeholder:#}",);
}

styled
Expand Down
7 changes: 4 additions & 3 deletions clap_builder/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4561,6 +4561,8 @@ impl Command {
}

pub(crate) fn format_group(&self, g: &Id) -> StyledStr {
use std::fmt::Write as _;

let g_string = self
.unroll_args_in_group(g)
.iter()
Expand All @@ -4576,10 +4578,9 @@ impl Command {
})
.collect::<Vec<_>>()
.join("|");
let placeholder = self.get_styles().get_placeholder();
let mut styled = StyledStr::new();
styled.push_str("<");
styled.push_string(g_string);
styled.push_str(">");
write!(&mut styled, "{placeholder}<{g_string}>{placeholder:#}").unwrap();
styled
}
}
Expand Down
133 changes: 29 additions & 104 deletions clap_builder/src/error/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,7 @@ impl ErrorFormatter for RichFormatter {
styled.push_str("\n");
}
for suggestion in suggestions {
let _ = write!(
styled,
"\n{TAB}{}tip:{} ",
valid.render(),
valid.render_reset()
);
let _ = write!(styled, "\n{TAB}{valid}tip:{valid:#} ",);
styled.push_styled(suggestion);
}
}
Expand All @@ -132,7 +127,7 @@ impl ErrorFormatter for RichFormatter {
fn start_error(styled: &mut StyledStr, styles: &Styles) {
use std::fmt::Write as _;
let error = &styles.get_error();
let _ = write!(styled, "{}error:{} ", error.render(), error.render_reset());
let _ = write!(styled, "{error}error:{error:#} ");
}

#[must_use]
Expand All @@ -155,26 +150,20 @@ fn write_dynamic_context(
prior_arg = None;
let _ = write!(
styled,
"the argument '{}{invalid_arg}{}' cannot be used multiple times",
invalid.render(),
invalid.render_reset()
"the argument '{invalid}{invalid_arg}{invalid:#}' cannot be used multiple times",
);
} else {
let _ = write!(
styled,
"the argument '{}{invalid_arg}{}' cannot be used with",
invalid.render(),
invalid.render_reset()
"the argument '{invalid}{invalid_arg}{invalid:#}' cannot be used with",
);
}
} else if let Some(ContextValue::String(invalid_arg)) =
error.get(ContextKind::InvalidSubcommand)
{
let _ = write!(
styled,
"the subcommand '{}{invalid_arg}{}' cannot be used with",
invalid.render(),
invalid.render_reset()
"the subcommand '{invalid}{invalid_arg}{invalid:#}' cannot be used with",
);
} else {
styled.push_str(error.kind().as_str().unwrap());
Expand All @@ -185,21 +174,11 @@ fn write_dynamic_context(
ContextValue::Strings(values) => {
styled.push_str(":");
for v in values {
let _ = write!(
styled,
"\n{TAB}{}{v}{}",
invalid.render(),
invalid.render_reset()
);
let _ = write!(styled, "\n{TAB}{invalid}{v}{invalid:#}",);
}
}
ContextValue::String(value) => {
let _ = write!(
styled,
" '{}{value}{}'",
invalid.render(),
invalid.render_reset()
);
let _ = write!(styled, " '{invalid}{value}{invalid:#}'",);
}
_ => {
styled.push_str(" one or more of the other specified arguments");
Expand All @@ -214,9 +193,7 @@ fn write_dynamic_context(
if let Some(ContextValue::String(invalid_arg)) = invalid_arg {
let _ = write!(
styled,
"equal sign is needed when assigning values to '{}{invalid_arg}{}'",
invalid.render(),
invalid.render_reset()
"equal sign is needed when assigning values to '{invalid}{invalid_arg}{invalid:#}'",
);
true
} else {
Expand All @@ -234,18 +211,12 @@ fn write_dynamic_context(
if invalid_value.is_empty() {
let _ = write!(
styled,
"a value is required for '{}{invalid_arg}{}' but none was supplied",
invalid.render(),
invalid.render_reset()
"a value is required for '{invalid}{invalid_arg}{invalid:#}' but none was supplied",
);
} else {
let _ = write!(
styled,
"invalid value '{}{invalid_value}{}' for '{}{invalid_arg}{}'",
invalid.render(),
invalid.render_reset(),
literal.render(),
literal.render_reset()
"invalid value '{invalid}{invalid_value}{invalid:#}' for '{literal}{invalid_arg}{literal:#}'",
);
}

Expand All @@ -262,9 +233,7 @@ fn write_dynamic_context(
if let Some(ContextValue::String(invalid_sub)) = invalid_sub {
let _ = write!(
styled,
"unrecognized subcommand '{}{invalid_sub}{}'",
invalid.render(),
invalid.render_reset()
"unrecognized subcommand '{invalid}{invalid_sub}{invalid:#}'",
);
true
} else {
Expand All @@ -276,12 +245,7 @@ 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}{}",
valid.render(),
valid.render_reset()
);
let _ = write!(styled, "\n{TAB}{valid}{v}{valid:#}",);
}
true
} else {
Expand All @@ -293,9 +257,7 @@ fn write_dynamic_context(
if let Some(ContextValue::String(invalid_sub)) = invalid_sub {
let _ = write!(
styled,
"'{}{invalid_sub}{}' requires a subcommand but one was not provided",
invalid.render(),
invalid.render_reset()
"'{invalid}{invalid_sub}{invalid:#}' requires a subcommand but one was not provided",
);
let values = error.get(ContextKind::ValidSubcommand);
write_values_list("subcommands", styled, valid, values);
Expand All @@ -316,11 +278,7 @@ fn write_dynamic_context(
{
let _ = write!(
styled,
"unexpected value '{}{invalid_value}{}' for '{}{invalid_arg}{}' found; no more were expected",
invalid.render(),
invalid.render_reset(),
literal.render(),
literal.render_reset(),
"unexpected value '{invalid}{invalid_value}{invalid:#}' for '{literal}{invalid_arg}{literal:#}' found; no more were expected",
);
true
} else {
Expand All @@ -340,13 +298,7 @@ fn write_dynamic_context(
let were_provided = singular_or_plural(*actual_num_values as usize);
let _ = write!(
styled,
"{}{min_values}{} values required by '{}{invalid_arg}{}'; only {}{actual_num_values}{}{were_provided}",
valid.render(),
valid.render_reset(),
literal.render(),
literal.render_reset(),
invalid.render(),
invalid.render_reset(),
"{valid}{min_values}{valid:#} values required by '{literal}{invalid_arg}{literal:#}'; only {invalid}{actual_num_values}{invalid:#}{were_provided}",
);
true
} else {
Expand All @@ -363,11 +315,7 @@ fn write_dynamic_context(
{
let _ = write!(
styled,
"invalid value '{}{invalid_value}{}' for '{}{invalid_arg}{}'",
invalid.render(),
invalid.render_reset(),
literal.render(),
literal.render_reset(),
"invalid value '{invalid}{invalid_value}{invalid:#}' for '{literal}{invalid_arg}{literal:#}'",
);
if let Some(source) = error.inner.source.as_deref() {
let _ = write!(styled, ": {source}");
Expand All @@ -390,13 +338,7 @@ fn write_dynamic_context(
let were_provided = singular_or_plural(*actual_num_values as usize);
let _ = write!(
styled,
"{}{num_values}{} values required for '{}{invalid_arg}{}' but {}{actual_num_values}{}{were_provided}",
valid.render(),
valid.render_reset(),
literal.render(),
literal.render_reset(),
invalid.render(),
invalid.render_reset(),
"{valid}{num_values}{valid:#} values required for '{literal}{invalid_arg}{literal:#}' but {invalid}{actual_num_values}{invalid:#}{were_provided}",
);
true
} else {
Expand All @@ -408,9 +350,7 @@ fn write_dynamic_context(
if let Some(ContextValue::String(invalid_arg)) = invalid_arg {
let _ = write!(
styled,
"unexpected argument '{}{invalid_arg}{}' found",
invalid.render(),
invalid.render_reset(),
"unexpected argument '{invalid}{invalid_arg}{invalid:#}' found",
);
true
} else {
Expand All @@ -437,13 +377,11 @@ fn write_values_list(
if !possible_values.is_empty() {
let _ = write!(styled, "\n{TAB}[{list_name}: ");

let style = valid.render();
let reset = valid.render_reset();
for (idx, val) in possible_values.iter().enumerate() {
if idx > 0 {
styled.push_str(", ");
}
let _ = write!(styled, "{style}{}{reset}", Escape(val));
let _ = write!(styled, "{valid}{}{valid:#}", Escape(val));
}

styled.push_str("]");
Expand Down Expand Up @@ -517,48 +455,35 @@ fn try_help(styled: &mut StyledStr, styles: &Styles, help: Option<&str>) {
let literal = &styles.get_literal();
let _ = write!(
styled,
"\n\nFor more information, try '{}{help}{}'.\n",
literal.render(),
literal.render_reset()
"\n\nFor more information, try '{literal}{help}{literal:#}'.\n",
);
} else {
styled.push_str("\n");
}
}

#[cfg(feature = "error-context")]
fn did_you_mean(styled: &mut StyledStr, styles: &Styles, context: &str, valid: &ContextValue) {
fn did_you_mean(styled: &mut StyledStr, styles: &Styles, context: &str, possibles: &ContextValue) {
use std::fmt::Write as _;

let _ = write!(
styled,
"{TAB}{}tip:{}",
styles.get_valid().render(),
styles.get_valid().render_reset()
);
if let ContextValue::String(valid) = valid {
let valid = &styles.get_valid();
let _ = write!(styled, "{TAB}{valid}tip:{valid:#}",);
if let ContextValue::String(possible) = possibles {
let _ = write!(
styled,
" a similar {context} exists: '{}{valid}{}'",
styles.get_valid().render(),
styles.get_valid().render_reset()
" a similar {context} exists: '{valid}{possible}{valid:#}'",
);
} else if let ContextValue::Strings(valid) = valid {
if valid.len() == 1 {
} else if let ContextValue::Strings(possibles) = possibles {
if possibles.len() == 1 {
let _ = write!(styled, " a similar {context} exists: ",);
} else {
let _ = write!(styled, " some similar {context}s exist: ",);
}
for (i, valid) in valid.iter().enumerate() {
for (i, possible) in possibles.iter().enumerate() {
if i != 0 {
styled.push_str(", ");
}
let _ = write!(
styled,
"'{}{valid}{}'",
styles.get_valid().render(),
styles.get_valid().render_reset()
);
let _ = write!(styled, "'{valid}{possible}{valid:#}'",);
}
}
}
Expand Down
Loading

0 comments on commit 1192002

Please sign in to comment.