Skip to content

Commit

Permalink
Refactor list subcommand (#2062)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored May 20, 2024
1 parent 587843f commit 9e658fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 10 additions & 15 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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!();
}
}
Expand Down

0 comments on commit 9e658fb

Please sign in to comment.