Skip to content

Commit

Permalink
✨ Add more shell completions (nu, fig)
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixr-codes committed Oct 18, 2024
1 parent bcfc8ce commit 9ec753e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ rhai = { version = "1.17.1", features = [
"no_custom_syntax"
] }
git2 = { version = "0.18.3", optional = true }
clap_complete = { version = "4.5.2", optional = true }
clap_complete = { version = "4.5.33", optional = true }
clap_complete_nushell = { version = "4.5.4", optional = true }
clap_complete_fig = { version = "4.5.2", optional = true }

[features]
# See README.md for descriptions about the features.
Expand All @@ -55,7 +57,7 @@ git = ["git2"]
manual = ["open"]
share = ["tokio", "warp", "qrcode", "local-ip-address"]
watch = ["notify", "notify-debouncer-mini"]
shell-completions = ["clap_complete"]
shell-completions = ["clap_complete", "clap_complete_nushell", "clap_complete_fig"]


# [lib]
Expand Down
70 changes: 67 additions & 3 deletions src/cli/completions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::{Arg, ArgMatches, Command};
use clap_complete::Shell;
use clap::{builder::PossibleValue, Arg, ArgMatches, Command};
use clap_complete::Generator;
use std::{io, process::ExitCode};

pub fn cmd() -> Command {
Expand All @@ -9,18 +9,82 @@ pub fn cmd() -> Command {
Arg::new("shell")
.help("The shell for which to generate completions")
.required(true)
.ignore_case(true)
.value_parser(clap::value_parser!(Shell)),
)
}

pub fn run(matches: &ArgMatches) -> ExitCode {
let generator = matches.get_one::<Shell>("shell").copied().unwrap();
let generator: Shell = matches.get_one::<Shell>("shell").cloned().unwrap();
let mut command = super::cmd();
print_completions(generator, &mut command);

ExitCode::SUCCESS
}

#[derive(Clone, Copy, Debug)]
enum Shell {
Bash,
Elvish,
Fish,
PowerShell,
Zsh,
Nushell,
Fig,
}

impl Generator for Shell {
fn file_name(&self, name: &str) -> String {
match self {
Self::Bash => clap_complete::shells::Bash.file_name(name),
Self::Elvish => clap_complete::shells::Elvish.file_name(name),
Self::Fish => clap_complete::shells::Fish.file_name(name),
Self::PowerShell => clap_complete::shells::PowerShell.file_name(name),
Self::Zsh => clap_complete::shells::Zsh.file_name(name),
Self::Nushell => clap_complete_nushell::Nushell.file_name(name),
Self::Fig => clap_complete_fig::Fig.file_name(name),
}
}

fn generate(&self, cmd: &Command, buf: &mut dyn io::Write) {
match self {
Self::Bash => clap_complete::shells::Bash.generate(cmd, buf),
Self::Elvish => clap_complete::shells::Elvish.generate(cmd, buf),
Self::Fish => clap_complete::shells::Fish.generate(cmd, buf),
Self::PowerShell => clap_complete::shells::PowerShell.generate(cmd, buf),
Self::Zsh => clap_complete::shells::Zsh.generate(cmd, buf),
Self::Nushell => clap_complete_nushell::Nushell.generate(cmd, buf),
Self::Fig => clap_complete_fig::Fig.generate(cmd, buf),
}
}
}

impl clap::ValueEnum for Shell {
fn value_variants<'a>() -> &'a [Self] {
&[
Self::Bash,
Self::Elvish,
Self::Fish,
Self::PowerShell,
Self::Zsh,
Self::Nushell,
Self::Fig,
]
}

fn to_possible_value(&self) -> Option<PossibleValue> {
match self {
Self::Bash => Some(PossibleValue::new("bash")),
Self::Elvish => Some(PossibleValue::new("elvish")),
Self::Fish => Some(PossibleValue::new("fish")),
Self::PowerShell => Some(PossibleValue::new("powershell")),
Self::Zsh => Some(PossibleValue::new("zsh")),
Self::Nushell => Some(PossibleValue::new("nushell")),
Self::Fig => Some(PossibleValue::new("fig")),
}
}
}

fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut Command) {
clap_complete::generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

0 comments on commit 9ec753e

Please sign in to comment.