Skip to content

Commit fd5e691

Browse files
authored
Merge pull request #5877 from therealprof/features/use-btreemap-instead-of-sortec-vec
Use BTreeMap instead of a sorted Vec
2 parents b5a47c4 + 6604e79 commit fd5e691

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

clap_builder/src/output/help_template.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Std
77
use std::borrow::Cow;
88
use std::cmp;
9+
use std::collections::BTreeMap;
910

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

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

488489
let key = (sort_key)(arg);
489-
ord_v.push((key, arg));
490+
ord_v.insert(key, arg);
490491
}
491-
ord_v.sort_by(|a, b| a.0.cmp(&b.0));
492492

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

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

861-
let mut ord_v = Vec::new();
861+
let mut ord_v = BTreeMap::new();
862862
for subcommand in cmd
863863
.get_subcommands()
864864
.filter(|subcommand| should_show_subcommand(subcommand))
865865
{
866-
ord_v.push((
867-
subcommand.get_display_order(),
868-
subcommand.get_name(),
866+
ord_v.insert(
867+
(subcommand.get_display_order(), subcommand.get_name()),
869868
subcommand,
870-
));
869+
);
871870
}
872-
ord_v.sort_by(|a, b| (a.0, &a.1).cmp(&(b.0, &b.1)));
873-
for (_, _, subcommand) in ord_v {
871+
for (_, subcommand) in ord_v {
874872
if !*first {
875873
self.writer.push_str("\n\n");
876874
}
@@ -915,7 +913,7 @@ impl HelpTemplate<'_, '_> {
915913

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

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

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

941-
for (i, (_, sc_str, sc)) in ord_v.into_iter().enumerate() {
938+
for (i, (sc_str, sc)) in ord_v.into_iter().enumerate() {
942939
if 0 < i {
943940
self.writer.push_str("\n");
944941
}
945-
self.write_subcommand(sc_str, sc, next_line_help, longest);
942+
self.write_subcommand(sc_str.1, sc, next_line_help, longest);
946943
}
947944
}
948945

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy