Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ codegen-units = 1
[dependencies]
clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.5"
clap_complete_nushell = "4.5"
dirs = "6.0.0"
dunce = "1.0"
serde = { version = "1.0", features = ["derive"] }
Expand Down
16 changes: 15 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
use clap::{Parser, ValueEnum};

/// Shell options for completions
#[derive(Clone, ValueEnum)]
pub enum CompletionShell {
Bash,
Elvish,
Fish,
Nushell,
PowerShell,
Zsh,
}

#[derive(Parser)]
#[clap(name = "Juliaup", version)]
#[command(
Expand Down Expand Up @@ -51,7 +62,10 @@ pub enum Juliaup {
#[clap(subcommand, name = "self")]
SelfSubCmd(SelfSubCmd),
/// Generate tab-completion scripts for your shell
Completions { shell: clap_complete::Shell },
Completions {
#[arg(value_enum, value_name = "SHELL")]
shell: CompletionShell
},
// This is used for the cron jobs that we create. By using this UUID for the command
// We can identify the cron jobs that were created by juliaup for uninstall purposes
#[cfg(feature = "selfupdate")]
Expand Down
52 changes: 44 additions & 8 deletions src/command_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,51 @@ use crate::cli;
use anyhow::Result;
use clap::CommandFactory;
use clap_complete::Shell;
use cli::Juliaup;
use clap_complete_nushell::Nushell;
use cli::{CompletionShell, Juliaup};
use std::io;

pub fn run_command_completions(shell: Shell) -> Result<()> {
clap_complete::generate(
shell,
&mut Juliaup::command(),
"juliaup",
&mut io::stdout().lock(),
);
fn shell_to_string(shell: CompletionShell) -> &'static str {
match shell {
CompletionShell::Bash => "bash",
CompletionShell::Elvish => "elvish",
CompletionShell::Fish => "fish",
CompletionShell::Nushell => "nushell",
CompletionShell::PowerShell => "powershell",
CompletionShell::Zsh => "zsh",
}
}

pub fn run_command_completions(shell: CompletionShell) -> Result<()> {
generate_completion_for_command::<Juliaup>(shell_to_string(shell), "juliaup")
}

/// Generic completion generator that supports both standard shells and nushell
pub fn generate_completion_for_command<T: CommandFactory>(
shell: &str,
app_name: &str,
) -> Result<()> {
let mut cmd = T::command();

// Try to parse as standard clap shell first
if let Ok(clap_shell) = shell.parse::<Shell>() {
clap_complete::generate(
clap_shell,
&mut cmd,
app_name,
&mut io::stdout().lock(),
);
} else if shell.eq_ignore_ascii_case("nushell") {
// Handle nushell separately
clap_complete::generate(
Nushell,
&mut cmd,
app_name,
&mut io::stdout().lock(),
);
} else {
anyhow::bail!("Unsupported shell: {}", shell);
}

Ok(())
}
Loading