Skip to content

Commit

Permalink
refactor(cli): remove allocation and use format arg capture
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Jan 9, 2024
1 parent ea9c75a commit d7664df
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,28 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
let reset = anstyle::Reset.render();

let options = CliUnstable::help();
let non_hidden_options: Vec<(String, String)> = options
.iter()
.filter(|(_, help_message)| *help_message != HIDDEN)
.map(|(name, help)| (name.to_string(), help.to_string()))
.collect();
let longest_option = non_hidden_options
let max_length = options
.iter()
.filter(|(_, help)| *help != HIDDEN)
.map(|(option_name, _)| option_name.len())
.max()
.unwrap_or(0);
let help_lines: Vec<String> = non_hidden_options
.unwrap();
let z_flags = options
.iter()
.map(|(option_name, option_help_message)| {
let option_name_kebab_case = option_name.replace("_", "-");
format!(
" {literal}-Z {:<longest_option$}{reset} {}",
option_name_kebab_case, option_help_message
)
.filter(|(_, help)| *help != HIDDEN)
.map(|(opt, help)| {
let opt = opt.replace("_", "-");
format!(" {literal}-Z {opt:<max_length$}{reset} {help}")
})
.collect();
let joined = help_lines.join("\n");
.join("\n");
drop_println!(
config,
"\
{header}Available unstable (nightly-only) flags:{reset}
{}
{z_flags}
Run with `{literal}cargo -Z{reset} {placeholder}[FLAG] [COMMAND]{reset}`",
joined
);
if !config.nightly_features_allowed {
drop_println!(
Expand Down

0 comments on commit d7664df

Please sign in to comment.