From 414c8546a0d3992d9e187deb49596065b36c99b4 Mon Sep 17 00:00:00 2001 From: Gerald Pinder Date: Sat, 4 Jan 2025 20:48:24 -0500 Subject: [PATCH] feat: Add nushell completions --- Cargo.lock | 11 +++++++ Cargo.toml | 1 + src/commands/completions.rs | 7 ++-- src/commands/completions/shells.rs | 52 ++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/commands/completions/shells.rs diff --git a/Cargo.lock b/Cargo.lock index d9509e5..a866442 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -362,6 +362,7 @@ dependencies = [ "clap", "clap-verbosity-flag", "clap_complete", + "clap_complete_nushell", "colored", "fuzzy-matcher", "indexmap 2.7.0", @@ -698,6 +699,16 @@ dependencies = [ "clap", ] +[[package]] +name = "clap_complete_nushell" +version = "4.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6a8b1593457dfc2fe539002b795710d022dc62a65bf15023f039f9760c7b18a" +dependencies = [ + "clap", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.5.18" diff --git a/Cargo.toml b/Cargo.toml index 4ab4963..496ac3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,6 +69,7 @@ blue-build-utils = { version = "=0.9.1", path = "./utils" } blue-build-process-management = { version = "=0.9.1", path = "./process" } clap-verbosity-flag = "3" clap_complete = "4" +clap_complete_nushell = "4" fuzzy-matcher = "0.3" jsonschema = { version = "0.28", optional = true } open = "5" diff --git a/src/commands/completions.rs b/src/commands/completions.rs index 5f7ff11..c761ea9 100644 --- a/src/commands/completions.rs +++ b/src/commands/completions.rs @@ -1,15 +1,18 @@ use clap::{Args, CommandFactory}; -use clap_complete::{generate, Shell as CompletionShell}; +use clap_complete::generate; use miette::Result; +use shells::Shells; use crate::commands::BlueBuildArgs; use super::BlueBuildCommand; +mod shells; + #[derive(Debug, Clone, Args)] pub struct CompletionsCommand { #[arg(value_enum)] - shell: CompletionShell, + shell: Shells, } impl BlueBuildCommand for CompletionsCommand { diff --git a/src/commands/completions/shells.rs b/src/commands/completions/shells.rs new file mode 100644 index 0000000..d081767 --- /dev/null +++ b/src/commands/completions/shells.rs @@ -0,0 +1,52 @@ +use clap::ValueEnum; +use clap_complete::{Generator, Shell as CompletionShell}; +use clap_complete_nushell::Nushell; + +#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq, Hash)] +pub enum Shells { + /// Bourne Again `SHell` (bash) + Bash, + /// Elvish shell + Elvish, + /// Friendly Interactive `SHell` (fish) + Fish, + /// `PowerShell` + PowerShell, + /// Z `SHell` (zsh) + Zsh, + /// Nushell (nu) + Nushell, +} + +impl Generator for Shells { + fn file_name(&self, name: &str) -> String { + match *self { + Self::Bash => CompletionShell::Bash.file_name(name), + Self::Elvish => CompletionShell::Elvish.file_name(name), + Self::Fish => CompletionShell::Fish.file_name(name), + Self::PowerShell => CompletionShell::PowerShell.file_name(name), + Self::Zsh => CompletionShell::Zsh.file_name(name), + Self::Nushell => Nushell.file_name(name), + } + } + + fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) { + match *self { + Self::Bash => CompletionShell::Bash.generate(cmd, buf), + Self::Elvish => CompletionShell::Elvish.generate(cmd, buf), + Self::Fish => CompletionShell::Fish.generate(cmd, buf), + Self::PowerShell => CompletionShell::PowerShell.generate(cmd, buf), + Self::Zsh => CompletionShell::Zsh.generate(cmd, buf), + Self::Nushell => Nushell.generate(cmd, buf), + } + } +} + +impl std::fmt::Display for Shells { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.to_possible_value() + .expect("no values are skipped") + .get_name() + .fmt(f) + } +}