diff --git a/Cargo.lock b/Cargo.lock index c3aa5fdde..e20d0dc9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -437,6 +437,16 @@ dependencies = [ "clap", ] +[[package]] +name = "clap_complete_nushell" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "315902e790cc6e5ddd20cbd313c1d0d49db77f191e149f96397230fb82a17677" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.5.28" @@ -3093,6 +3103,7 @@ dependencies = [ "chrono", "clap", "clap_complete", + "clap_complete_nushell", "deelevate", "dirs 6.0.0", "dunce", diff --git a/Cargo.toml b/Cargo.toml index cb8400937..a0cb5c828 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ gix-faster = ["gix-features/zlib-stock", "gix/fast-sha1"] chrono = { version = "0.4.40", default-features = false, features = ["clock", "std", "wasmbind"] } clap = { version = "4.5.31", features = ["derive", "cargo", "unicode"] } clap_complete = "4.5.46" +clap_complete_nushell = "4.5.4" dirs = "6.0.0" dunce = "1.0.5" # default feature restriction addresses https://github.com/starship/starship/issues/4251 diff --git a/src/main.rs b/src/main.rs index 1e22ce6cf..09671faa7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,8 @@ use std::path::PathBuf; use std::thread::available_parallelism; use std::time::SystemTime; -use clap::{CommandFactory, Parser, Subcommand}; -use clap_complete::{generate, Shell as CompletionShell}; +use clap::{CommandFactory, Parser, Subcommand, ValueEnum}; +use clap_complete::generate; use rand::distributions::Alphanumeric; use rand::Rng; use starship::context::{Context, Properties, Target}; @@ -28,6 +28,36 @@ struct Cli { command: Commands, } +#[derive(clap::Parser, ValueEnum, Debug, Clone, PartialEq, Eq)] +enum CompletionShell { + Bash, + Elvish, + Fish, + Nushell, + PowerShell, + Zsh, +} + +fn generate_shell(shell: impl clap_complete::Generator) { + generate( + shell, + &mut Cli::command(), + "starship", + &mut io::stdout().lock(), + ) +} + +fn generate_completions(shell: CompletionShell) { + match shell { + CompletionShell::Bash => generate_shell(clap_complete::Shell::Bash), + CompletionShell::Elvish => generate_shell(clap_complete::Shell::Elvish), + CompletionShell::Fish => generate_shell(clap_complete::Shell::Fish), + CompletionShell::PowerShell => generate_shell(clap_complete::Shell::PowerShell), + CompletionShell::Zsh => generate_shell(clap_complete::Shell::Zsh), + CompletionShell::Nushell => generate_shell(clap_complete_nushell::Nushell), + } +} + #[derive(Subcommand, Debug)] enum Commands { /// Create a pre-populated GitHub issue with information about your configuration @@ -236,12 +266,7 @@ fn main() { } Commands::Explain(props) => print::explain(props), Commands::Timings(props) => print::timings(props), - Commands::Completions { shell } => generate( - shell, - &mut Cli::command(), - "starship", - &mut io::stdout().lock(), - ), + Commands::Completions { shell } => generate_completions(shell), Commands::Session => println!( "{}", rand::thread_rng()