Skip to content

Commit

Permalink
feat(nushell): add nushell completion generation (#1791)
Browse files Browse the repository at this point in the history
  • Loading branch information
remmycat committed Feb 29, 2024
1 parent 593dc41 commit 5f0e6dd
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion atuin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
84 changes: 84 additions & 0 deletions atuin/src/command/gen_completions.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
35 changes: 5 additions & 30 deletions atuin/src/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clap::{CommandFactory, Subcommand};
use clap_complete::{generate, generate_to, Shell};
use clap::Subcommand;
use eyre::Result;

#[cfg(not(windows))]
Expand All @@ -13,6 +12,8 @@ mod server;

mod contributors;

mod gen_completions;

#[derive(Subcommand)]
#[command(infer_subcommands = true)]
pub enum AtuinCmd {
Expand All @@ -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 {
Expand All @@ -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(),
}
}
}

0 comments on commit 5f0e6dd

Please sign in to comment.