diff --git a/Cargo.lock b/Cargo.lock index 3ced7ea..be1c05a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -408,13 +408,23 @@ dependencies = [ [[package]] name = "clap_complete" -version = "4.5.48" +version = "4.5.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be8c97f3a6f02b9e24cadc12aaba75201d18754b53ea0a9d99642f806ccdb4c9" +checksum = "1a554639e42d0c838336fc4fbedb9e2df3ad1fa4acda149f9126b4ccfcd7900f" dependencies = [ "clap", ] +[[package]] +name = "clap_complete_nushell" +version = "4.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9801fe85d7986742027c6d365728a6a4ecb6d2b09866de18be836fef7ebf7df1" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.5.32" @@ -1017,6 +1027,7 @@ dependencies = [ "cfg-if 1.0.0", "clap", "clap_complete", + "clap_complete_nushell", "clap_mangen", "cli-clipboard", "colored", diff --git a/Cargo.toml b/Cargo.toml index 1a66202..679a2ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ open = "5.1.4" rand = "0.8.5" tempfile = "3" dialoguer = "0.11.0" +clap_complete_nushell = "4.5.6" [target.'cfg(target_os = "android")'.dependencies] terminal-clipboard = "0.4.1" diff --git a/src/cli.rs b/src/cli.rs index 2e2021c..57ec5ae 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,6 @@ use crate::mod_file::PlatformName; +use clap::{Command, ValueEnum}; +use clap_complete::Generator; /// Command-line interface for Geode #[derive(clap::Parser, Debug)] @@ -8,6 +10,40 @@ pub struct Args { pub command: GeodeCommands, } +#[derive(Debug, ValueEnum, Clone)] +pub enum Shell { + Bash, + Elvish, + Fish, + PowerShell, + Zsh, + NuShell, +} + +impl Generator for Shell { + fn file_name(&self, name: &str) -> String { + match self { + Shell::Bash => format!("{}.bash", name), + Shell::Elvish => format!("{}.elv", name), + Shell::Fish => format!("{}.fish", name), + Shell::PowerShell => format!("_{}.ps1", name), + Shell::Zsh => format!("_{}", name), + Shell::NuShell => clap_complete_nushell::Nushell.file_name(name), + } + } + + fn generate(&self, cmd: &Command, buf: &mut dyn std::io::Write) { + match self { + Shell::Bash => clap_complete::shells::Bash.generate(cmd, buf), + Shell::Elvish => clap_complete::shells::Elvish.generate(cmd, buf), + Shell::Fish => clap_complete::shells::Fish.generate(cmd, buf), + Shell::PowerShell => clap_complete::shells::PowerShell.generate(cmd, buf), + Shell::Zsh => clap_complete::shells::Zsh.generate(cmd, buf), + Shell::NuShell => clap_complete_nushell::Nushell.generate(cmd, buf), + } + } +} + #[derive(clap::Subcommand, Debug)] pub enum GeodeCommands { /// Initialize a new Geode project @@ -17,7 +53,7 @@ pub enum GeodeCommands { }, /// Generate shell completions and print it to stdout - Completions { shell: clap_complete::Shell }, + Completions { shell: Shell }, /// Generate manpage and print it to stdout GenerateManpage {},