Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BTreeMap instead of a sorted Vec #5877

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions clap_builder/src/output/help_template.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
// Std
use std::borrow::Cow;
use std::cmp;
use std::collections::BTreeMap;

// Internal
use crate::builder::PossibleValue;
@@ -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| {
@@ -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);

@@ -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");
}
@@ -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))
@@ -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);
}
}