Refactor list subcommand (#2062)

This commit is contained in:
Casey Rodarmor 2024-05-19 23:47:57 -07:00 committed by GitHub
parent 587843f09c
commit 9e658fba39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 15 deletions

View File

@ -38,6 +38,7 @@ pub(crate) use {
use_color::UseColor, variables::Variables, verbosity::Verbosity, warning::Warning,
},
std::{
borrow::Cow,
cmp,
collections::{BTreeMap, BTreeSet, HashMap},
env,

View File

@ -505,7 +505,7 @@ impl Subcommand {
}
}
let max_line_width = cmp::min(line_widths.values().copied().max().unwrap_or(0), MAX_WIDTH);
let max_line_width = line_widths.values().copied().max().unwrap_or_default();
let doc_color = config.color.stdout().doc();
if level == 0 {
@ -524,28 +524,23 @@ impl Subcommand {
print!(" {}", parameter.color_display(config.color.stdout()));
}
// Declaring this outside of the nested loops will probably be more efficient,
// but it creates all sorts of lifetime issues with variables inside the loops.
// If this is inlined like the docs say, it shouldn't make any difference.
let print_doc = |doc| {
let doc = match (i, recipe.doc) {
(0, Some(doc)) => Some(Cow::Borrowed(doc)),
(0, None) => None,
_ => Some(Cow::Owned(format!("alias for `{}`", recipe.name))),
};
if let Some(doc) = doc {
print!(
" {:padding$}{} {}",
"",
doc_color.paint("#"),
doc_color.paint(doc),
doc_color.paint(&doc),
padding = max_line_width
.saturating_sub(line_widths.get(name).copied().unwrap_or(max_line_width))
);
};
match (i, recipe.doc) {
(0, Some(doc)) => print_doc(doc),
(0, None) => (),
_ => {
let alias_doc = format!("alias for `{}`", recipe.name);
print_doc(&alias_doc);
}
}
println!();
}
}