feat(nushell): add nushell completion generation (#1791)

This commit is contained in:
Remmy Cat Stock 2024-02-29 12:08:32 +01:00 committed by GitHub
parent 593dc410eb
commit 5f0e6dd307
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 104 additions and 33 deletions

View File

@ -154,7 +154,7 @@ jobs:
esac;
# Shell completions
for sh in 'bash' 'fish' 'zsh'; do
for sh in 'bash' 'fish' 'zsh' 'nushell'; do
$QEMU_PREFIX "${{ steps.strip.outputs.BIN_PATH }}" gen-completions -s $sh -o "${ARCHIVE_DIR}/completions"
done

11
Cargo.lock generated
View File

@ -191,6 +191,7 @@ dependencies = [
"base64 0.21.7",
"clap",
"clap_complete",
"clap_complete_nushell",
"cli-clipboard",
"colored",
"crossterm",
@ -661,6 +662,16 @@ dependencies = [
"clap",
]
[[package]]
name = "clap_complete_nushell"
version = "4.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80d0e48e026ce7df2040239117d25e4e79714907420c70294a5ce4b6bbe6a7b6"
dependencies = [
"clap",
"clap_complete",
]
[[package]]
name = "clap_derive"
version = "4.5.0"

View File

@ -29,7 +29,7 @@ time = { version = "0.3", features = [
"macros",
"local-offset",
] }
clap = { version = "4.0.18", features = ["derive"] }
clap = { version = "4.5.1", features = ["derive"] }
config = { version = "0.13", default-features = false, features = ["toml"] }
directories = "5.0.1"
eyre = "0.6"

View File

@ -63,7 +63,8 @@ async-trait = { workspace = true }
interim = { workspace = true }
base64 = { workspace = true }
clap = { workspace = true }
clap_complete = "4.0.3"
clap_complete = "4.5.1"
clap_complete_nushell = "4.5.1"
fs-err = { workspace = true }
whoami = { workspace = true }
rpassword = "7.0"

View File

@ -0,0 +1,84 @@
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{generate, generate_to, Generator, Shell};
use clap_complete_nushell::Nushell;
use eyre::Result;
// clap put nushell completions into a seperate package due to the maintainers
// being a little less commited to support them.
// This means we have to do a tiny bit of legwork to combine these completions
// into one command.
#[derive(Debug, Clone, ValueEnum)]
#[value(rename_all = "lower")]
pub enum GenShell {
Bash,
Elvish,
Fish,
Nushell,
PowerShell,
Zsh,
}
impl Generator for GenShell {
fn file_name(&self, name: &str) -> String {
match self {
// clap_complete
Self::Bash => Shell::Bash.file_name(name),
Self::Elvish => Shell::Elvish.file_name(name),
Self::Fish => Shell::Fish.file_name(name),
Self::PowerShell => Shell::PowerShell.file_name(name),
Self::Zsh => Shell::Zsh.file_name(name),
// clap_complete_nushell
Self::Nushell => Nushell.file_name(name),
}
}
fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::prelude::Write) {
match self {
// clap_complete
Self::Bash => Shell::Bash.generate(cmd, buf),
Self::Elvish => Shell::Elvish.generate(cmd, buf),
Self::Fish => Shell::Fish.generate(cmd, buf),
Self::PowerShell => Shell::PowerShell.generate(cmd, buf),
Self::Zsh => Shell::Zsh.generate(cmd, buf),
// clap_complete_nushell
Self::Nushell => Nushell.generate(cmd, buf),
}
}
}
#[derive(Debug, Parser)]
pub struct Cmd {
/// Set the shell for generating completions
#[arg(long, short)]
shell: GenShell,
/// Set the output directory
#[arg(long, short)]
out_dir: Option<String>,
}
impl Cmd {
pub fn run(self) -> Result<()> {
let Cmd { shell, out_dir } = self;
let mut cli = crate::Atuin::command();
match out_dir {
Some(out_dir) => {
generate_to(shell, &mut cli, env!("CARGO_PKG_NAME"), &out_dir)?;
}
None => {
generate(
shell,
&mut cli,
env!("CARGO_PKG_NAME"),
&mut std::io::stdout(),
);
}
}
Ok(())
}
}

View File

@ -1,5 +1,4 @@
use clap::{CommandFactory, Subcommand};
use clap_complete::{generate, generate_to, Shell};
use clap::Subcommand;
use eyre::Result;
#[cfg(not(windows))]
@ -13,6 +12,8 @@ mod server;
mod contributors;
mod gen_completions;
#[derive(Subcommand)]
#[command(infer_subcommands = true)]
pub enum AtuinCmd {
@ -31,15 +32,7 @@ pub enum AtuinCmd {
Contributors,
/// Generate shell completions
GenCompletions {
/// Set the shell for generating completions
#[arg(long, short)]
shell: Shell,
/// Set the output directory
#[arg(long, short)]
out_dir: Option<String>,
},
GenCompletions(gen_completions::Cmd),
}
impl AtuinCmd {
@ -66,25 +59,7 @@ impl AtuinCmd {
println!("{}", atuin_common::utils::uuid_v7().as_simple());
Ok(())
}
Self::GenCompletions { shell, out_dir } => {
let mut cli = crate::Atuin::command();
match out_dir {
Some(out_dir) => {
generate_to(shell, &mut cli, env!("CARGO_PKG_NAME"), &out_dir)?;
}
None => {
generate(
shell,
&mut cli,
env!("CARGO_PKG_NAME"),
&mut std::io::stdout(),
);
}
}
Ok(())
}
Self::GenCompletions(gen_completions) => gen_completions.run(),
}
}
}