From 480960ce2b65437c814135a1c22ef04e49cf0ea9 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Thu, 22 Dec 2022 19:09:44 -0500 Subject: [PATCH] bootstrap: recreate completion function to use clap_completion::generator::Generator trait to specify generic parameter --- src/cmd/bootstrap/mod.rs | 63 +++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/src/cmd/bootstrap/mod.rs b/src/cmd/bootstrap/mod.rs index 60e2225..46956f0 100644 --- a/src/cmd/bootstrap/mod.rs +++ b/src/cmd/bootstrap/mod.rs @@ -1,8 +1,13 @@ use crate::cmd; -use clap::{value_parser, Arg, ArgMatches, Command, ArgAction}; -use clap_complete::{generate_to, shells}; +use clap::{value_parser, Arg, ArgAction, ArgMatches, Command}; +use clap_complete::{generate_to, shells, Generator}; +use clap_complete_nushell::Nushell; use clap_mangen::Man; -use std::{fs, io, path::{Path, PathBuf}, process}; +use std::{ + fs, io, + path::{Path, PathBuf}, + process, +}; const COMMANDS: [fn() -> clap::Command; 8] = [ cmd::bootstrap::cli, @@ -109,26 +114,22 @@ fn manpage(prefix: &str, f: &dyn Fn() -> Command) -> Result<(), io::Error> { fn manpages(prefix: &str) -> Result<(), io::Error> { println!("Generating Unix man pages:"); - COMMANDS.iter().try_for_each(|cmd| { - manpage(prefix, &cmd) - })?; + COMMANDS.iter().try_for_each(|cmd| manpage(prefix, &cmd))?; Ok(()) } -fn gen_bash_completion(outdir: &Path, f: &dyn Fn() -> Command) -> Result<(), io::Error> { +fn generate_completions( + outdir: &Path, + f: &dyn Fn() -> Command, + gen: impl Generator, +) -> Result<(), io::Error> { let cmd = f(); let name = cmd.get_name(); let mut cmd = cmd.clone(); - let path = generate_to(shells::Bash, &mut cmd, name, outdir)?; - println!(" {}", path.display()); - Ok(()) -} - -fn gen_fish_completion(outdir: &Path, f: &dyn Fn() -> Command) -> Result<(), io::Error> { - let cmd = f(); - let name = cmd.get_name(); - let mut cmd = cmd.clone(); - let path = generate_to(shells::Fish, &mut cmd, name, outdir)?; + if !outdir.exists() { + fs::create_dir_all(&outdir)?; + } + let path = generate_to(gen, &mut cmd, name, outdir)?; println!(" {}", path.display()); Ok(()) } @@ -139,25 +140,33 @@ fn completions(prefix: &str, matches: &ArgMatches) -> Result<(), io::Error> { let outdir: PathBuf = [prefix, "share", "bash-completion", "completion"] .iter() .collect(); - if !outdir.exists() { - fs::create_dir_all(&outdir)?; - } - COMMANDS.iter().try_for_each(|cmd| gen_bash_completion(&outdir, &cmd) )?; + COMMANDS + .iter() + .try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Bash))?; } if matches.get_flag("fish") || matches.get_flag("all") { - let outdir: PathBuf = [prefix, "share", "fish", "completions"] + let outdir: PathBuf = [prefix, "share", "fish", "completions"].iter().collect(); + COMMANDS .iter() - .collect(); - if !outdir.exists() { - fs::create_dir_all(&outdir)?; - } - COMMANDS.iter().try_for_each(|cmd| gen_fish_completion(&outdir, &cmd) )?; + .try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Fish))?; } if matches.get_flag("nu") || matches.get_flag("all") { + let outdir: PathBuf = [prefix, "share", "nu", "completions"].iter().collect(); + COMMANDS + .iter() + .try_for_each(|cmd| generate_completions(&outdir, &cmd, Nushell))?; } if matches.get_flag("pwsh") || matches.get_flag("all") { + let outdir: PathBuf = [prefix, "share", "pwsh", "completions"].iter().collect(); + COMMANDS + .iter() + .try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::PowerShell))?; } if matches.get_flag("zsh") || matches.get_flag("all") { + let outdir: PathBuf = [prefix, "share", "zsh", "site-functions"].iter().collect(); + COMMANDS + .iter() + .try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Zsh))?; } Ok(()) }