summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemmy Cat Stock <3317423+remmycat@users.noreply.github.com>2024-02-29 12:08:32 +0100
committerGitHub <noreply@github.com>2024-02-29 11:08:32 +0000
commit5f0e6dd3076fe61c3e70898d789f4a7087f53d49 (patch)
tree7791a004da0255e8023b457ac02a0232dda97122
parent593dc410eb3d5d7d409b780b9c7bd22ee4aa8654 (diff)
feat(nushell): add nushell completion generation (#1791)
-rw-r--r--.github/workflows/release.yaml2
-rw-r--r--Cargo.lock11
-rw-r--r--Cargo.toml2
-rw-r--r--atuin/Cargo.toml3
-rw-r--r--atuin/src/command/gen_completions.rs84
-rw-r--r--atuin/src/command/mod.rs35
6 files changed, 104 insertions, 33 deletions
diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml
index f6e335bc..a904a8e0 100644
--- a/.github/workflows/release.yaml
+++ b/.github/workflows/release.yaml
@@ -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
diff --git a/Cargo.lock b/Cargo.lock
index 2dd87a21..338dab76 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -191,6 +191,7 @@ dependencies = [
"base64 0.21.7",
"clap",
"clap_complete",
+ "clap_complete_nushell",
"cli-clipboard",
"colored",
"crossterm",
@@ -662,6 +663,16 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index f1057382..29ff07dd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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"
diff --git a/atuin/Cargo.toml b/atuin/Cargo.toml
index 6333a566..21318431 100644
--- a/atuin/Cargo.toml
+++ b/atuin/Cargo.toml
@@ -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"
diff --git a/atuin/src/command/gen_completions.rs b/atuin/src/command/gen_completions.rs
new file mode 100644
index 00000000..91b1b66f
--- /dev/null
+++ b/atuin/src/command/gen_completions.rs
@@ -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(())
+ }
+}
diff --git a/atuin/src/command/mod.rs b/atuin/src/command/mod.rs
index 44725cb2..09df430e 100644
--- a/atuin/src/command/mod.rs
+++ b/atuin/src/command/mod.rs
@@ -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(),
}
}
}