mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 07:17:26 +00:00
refactor(help): Move near use
This commit is contained in:
parent
9036d3fd3c
commit
6ea5d46300
4 changed files with 20 additions and 30 deletions
|
@ -4074,10 +4074,6 @@ impl Arg {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn longest_filter(&self) -> bool {
|
||||
self.is_takes_value_set() || self.long.is_some() || self.short.is_none()
|
||||
}
|
||||
|
||||
// Used for positionals when printing
|
||||
pub(crate) fn name_no_brackets(&self) -> String {
|
||||
debug!("Arg::name_no_brackets:{}", self.get_id());
|
||||
|
|
|
@ -4283,18 +4283,6 @@ impl Command {
|
|||
self.get_arguments().filter(|a| !a.is_positional())
|
||||
}
|
||||
|
||||
/// Iterate through the *positionals* that don't have custom heading.
|
||||
pub(crate) fn get_positionals_with_no_heading(&self) -> impl Iterator<Item = &Arg> {
|
||||
self.get_positionals()
|
||||
.filter(|a| a.get_help_heading().is_none())
|
||||
}
|
||||
|
||||
/// Iterate through the *flags* & *options* that don't have custom heading.
|
||||
pub(crate) fn get_non_positionals_with_no_heading(&self) -> impl Iterator<Item = &Arg> {
|
||||
self.get_non_positionals()
|
||||
.filter(|a| a.get_help_heading().is_none())
|
||||
}
|
||||
|
||||
pub(crate) fn find(&self, arg_id: &Id) -> Option<&Arg> {
|
||||
self.args.args().find(|a| a.id == *arg_id)
|
||||
}
|
||||
|
|
|
@ -86,12 +86,6 @@ impl StyledStr {
|
|||
self.pieces = self.pieces.trim_end().to_owned();
|
||||
}
|
||||
|
||||
pub(crate) fn replace_newline(&mut self) {
|
||||
for (_, content) in self.iter_mut() {
|
||||
*content = content.replace("{n}", "\n");
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn indent(&mut self, initial: &str, trailing: &str) {
|
||||
if let Some((_, first)) = self.iter_mut().next() {
|
||||
first.insert_str(0, initial);
|
||||
|
|
|
@ -278,7 +278,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
|||
self.none("\n");
|
||||
}
|
||||
let mut output = output.clone();
|
||||
output.replace_newline();
|
||||
replace_newline_var(&mut output);
|
||||
output.wrap(self.term_w);
|
||||
self.writer.extend(output.into_iter());
|
||||
if after_new_line {
|
||||
|
@ -298,7 +298,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
|||
};
|
||||
if let Some(output) = before_help {
|
||||
let mut output = output.clone();
|
||||
output.replace_newline();
|
||||
replace_newline_var(&mut output);
|
||||
output.wrap(self.term_w);
|
||||
self.writer.extend(output.into_iter());
|
||||
self.none("\n\n");
|
||||
|
@ -317,7 +317,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
|||
if let Some(output) = after_help {
|
||||
self.none("\n\n");
|
||||
let mut output = output.clone();
|
||||
output.replace_newline();
|
||||
replace_newline_var(&mut output);
|
||||
output.wrap(self.term_w);
|
||||
self.writer.extend(output.into_iter());
|
||||
}
|
||||
|
@ -332,12 +332,14 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
|||
debug!("HelpTemplate::write_all_args");
|
||||
let pos = self
|
||||
.cmd
|
||||
.get_positionals_with_no_heading()
|
||||
.get_positionals()
|
||||
.filter(|a| a.get_help_heading().is_none())
|
||||
.filter(|arg| should_show_arg(self.use_long, arg))
|
||||
.collect::<Vec<_>>();
|
||||
let non_pos = self
|
||||
.cmd
|
||||
.get_non_positionals_with_no_heading()
|
||||
.get_non_positionals()
|
||||
.filter(|a| a.get_help_heading().is_none())
|
||||
.filter(|arg| should_show_arg(self.use_long, arg))
|
||||
.collect::<Vec<_>>();
|
||||
let subcmds = self.cmd.has_visible_subcommands();
|
||||
|
@ -423,7 +425,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
|||
// args alignment
|
||||
should_show_arg(self.use_long, *arg)
|
||||
}) {
|
||||
if arg.longest_filter() {
|
||||
if longest_filter(arg) {
|
||||
longest = longest.max(display_width(&arg.to_string()));
|
||||
debug!(
|
||||
"HelpTemplate::write_args: arg={:?} longest={}",
|
||||
|
@ -567,7 +569,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
|||
let trailing_indent = self.get_spaces(trailing_indent);
|
||||
|
||||
let mut help = about.clone();
|
||||
help.replace_newline();
|
||||
replace_newline_var(&mut help);
|
||||
if !spec_vals.is_empty() {
|
||||
if !help.is_empty() {
|
||||
let sep = if self.use_long && arg.is_some() {
|
||||
|
@ -655,7 +657,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
|
|||
};
|
||||
|
||||
let mut help = help.clone();
|
||||
help.replace_newline();
|
||||
replace_newline_var(&mut help);
|
||||
help.wrap(avail_chars);
|
||||
help.indent("", &trailing_indent);
|
||||
self.writer.extend(help.into_iter());
|
||||
|
@ -1002,6 +1004,16 @@ fn should_show_subcommand(subcommand: &Command) -> bool {
|
|||
!subcommand.is_hide_set()
|
||||
}
|
||||
|
||||
fn replace_newline_var(styled: &mut StyledStr) {
|
||||
for (_, content) in styled.iter_mut() {
|
||||
*content = content.replace("{n}", "\n");
|
||||
}
|
||||
}
|
||||
|
||||
fn longest_filter(arg: &Arg) -> bool {
|
||||
arg.is_takes_value_set() || arg.long.is_some() || arg.short.is_none()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
|
Loading…
Add table
Reference in a new issue