Skip to content

Commit

Permalink
Merge pull request #5877 from therealprof/features/use-btreemap-inste…
Browse files Browse the repository at this point in the history
…ad-of-sortec-vec

Use BTreeMap instead of a sorted Vec
  • Loading branch information
epage authored Jan 9, 2025
2 parents b5a47c4 + 6604e79 commit fd5e691
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions clap_builder/src/output/help_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Std
use std::borrow::Cow;
use std::cmp;
use std::collections::BTreeMap;

// Internal
use crate::builder::PossibleValue;
Expand Down Expand Up @@ -467,7 +468,7 @@ impl HelpTemplate<'_, '_> {
debug!("HelpTemplate::write_args {_category}");
// The shortest an arg can legally be is 2 (i.e. '-x')
let mut longest = 2;
let mut ord_v = Vec::new();
let mut ord_v = BTreeMap::new();

// Determine the longest
for &arg in args.iter().filter(|arg| {
Expand All @@ -486,9 +487,8 @@ impl HelpTemplate<'_, '_> {
}

let key = (sort_key)(arg);
ord_v.push((key, arg));
ord_v.insert(key, arg);
}
ord_v.sort_by(|a, b| a.0.cmp(&b.0));

let next_line_help = self.will_args_wrap(args, longest);

Expand Down Expand Up @@ -858,19 +858,17 @@ impl HelpTemplate<'_, '_> {
use std::fmt::Write as _;
let header = &self.styles.get_header();

let mut ord_v = Vec::new();
let mut ord_v = BTreeMap::new();
for subcommand in cmd
.get_subcommands()
.filter(|subcommand| should_show_subcommand(subcommand))
{
ord_v.push((
subcommand.get_display_order(),
subcommand.get_name(),
ord_v.insert(
(subcommand.get_display_order(), subcommand.get_name()),
subcommand,
));
);
}
ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
for (_, _, subcommand) in ord_v {
for (_, subcommand) in ord_v {
if !*first {
self.writer.push_str("\n\n");
}
Expand Down Expand Up @@ -915,7 +913,7 @@ impl HelpTemplate<'_, '_> {

// The shortest an arg can legally be is 2 (i.e. '-x')
let mut longest = 2;
let mut ord_v = Vec::new();
let mut ord_v = BTreeMap::new();
for subcommand in cmd
.get_subcommands()
.filter(|subcommand| should_show_subcommand(subcommand))
Expand All @@ -930,19 +928,18 @@ impl HelpTemplate<'_, '_> {
let _ = write!(styled, ", {literal}--{long}{literal:#}",);
}
longest = longest.max(styled.display_width());
ord_v.push((subcommand.get_display_order(), styled, subcommand));
ord_v.insert((subcommand.get_display_order(), styled), subcommand);
}
ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));

debug!("HelpTemplate::write_subcommands longest = {longest}");

let next_line_help = self.will_subcommands_wrap(cmd.get_subcommands(), longest);

for (i, (_, sc_str, sc)) in ord_v.into_iter().enumerate() {
for (i, (sc_str, sc)) in ord_v.into_iter().enumerate() {
if 0 < i {
self.writer.push_str("\n");
}
self.write_subcommand(sc_str, sc, next_line_help, longest);
self.write_subcommand(sc_str.1, sc, next_line_help, longest);
}
}

Expand Down

0 comments on commit fd5e691

Please sign in to comment.