Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nushell completion support #1030

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 docs/guide/commands/self/completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ _no arguments_

* `-s, --shell <SHELL>`: The shell to generate a completion script for (defaults to 'bash')

[possible values: `bash`, `elvish`, `fish`, `powershell`, `zsh`]
[possible values: `bash`, `elvish`, `fish`, `powershell`, `zsh`, `nushell`]

* `-h, --help`: Print help (see a summary with '-h')
8 changes: 7 additions & 1 deletion docs/guide/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ to learn more.

## Shell Completion

Rye supports generating completion scripts for Bash, Zsh, Fish or Powershell. Here are some common locations for each shell:
Rye supports generating completion scripts for Bash, Zsh, Fish, Powershell and Nushell. Here are some common locations for each shell:

=== "Bash"

Expand Down Expand Up @@ -183,6 +183,12 @@ Rye supports generating completion scripts for Bash, Zsh, Fish or Powershell. He
rye self completion -s powershell | Out-File -Encoding utf8 $PROFILE\..\Completions\rye_completion.ps1
```

=== "NuShell"

```nushell
rye self completion -s nushell | save --append $nu.env-path
```

## Updating Rye

To update rye to the latest version you can use `rye` itself:
Expand Down
1 change: 1 addition & 0 deletions rye/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ clap = { version = "4.3.5", default-features = false, features = [
"std",
] }
clap_complete = "4.2.1"
clap_complete_nushell = "4.5.1"
console = "0.15.7"
curl = { version = "0.4.44", features = ["ssl", "static-curl", "static-ssl"] }
flate2 = "1.0.25"
Expand Down
51 changes: 47 additions & 4 deletions rye/src/cli/rye.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use std::sync::Arc;
use std::{env, fs};

use anyhow::{anyhow, bail, Context, Error};
use clap::{CommandFactory, Parser};
use clap_complete::Shell;
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{Generator, Shell};
use clap_complete_nushell::Nushell;
use console::style;
use minijinja::render;
use self_replace::self_delete_outside_path;
Expand Down Expand Up @@ -52,12 +53,54 @@ pub struct Args {
command: SubCommand,
}

#[derive(Clone, Debug, ValueEnum)]
enum ShellCompletion {
MilesCranmer marked this conversation as resolved.
Show resolved Hide resolved
/// Bourne Again SHell (bash)
Bash,
/// Elvish shell
Elvish,
/// Friendly Interactive SHell (fish)
Fish,
/// PowerShell
PowerShell,
/// Z SHell (zsh)
Zsh,
/// Nushell
Nushell,
}

impl Generator for ShellCompletion {
/// Generate the file name for the completion script.
fn file_name(&self, name: &str) -> String {
match self {
ShellCompletion::Nushell => Nushell.file_name(name),
ShellCompletion::Bash => Shell::Bash.file_name(name),
ShellCompletion::Elvish => Shell::Elvish.file_name(name),
ShellCompletion::Fish => Shell::Fish.file_name(name),
ShellCompletion::PowerShell => Shell::PowerShell.file_name(name),
ShellCompletion::Zsh => Shell::Zsh.file_name(name),
MilesCranmer marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Generate the completion script for the shell.
fn generate(&self, cmd: &clap::Command, buf: &mut dyn std::io::Write) {
match self {
ShellCompletion::Nushell => Nushell.generate(cmd, buf),
ShellCompletion::Bash => Shell::Bash.generate(cmd, buf),
ShellCompletion::Elvish => Shell::Elvish.generate(cmd, buf),
ShellCompletion::Fish => Shell::Fish.generate(cmd, buf),
ShellCompletion::PowerShell => Shell::PowerShell.generate(cmd, buf),
ShellCompletion::Zsh => Shell::Zsh.generate(cmd, buf),
}
}
}

/// Generates a completion script for a shell.
#[derive(Parser, Debug)]
pub struct CompletionCommand {
/// The shell to generate a completion script for (defaults to 'bash').
#[arg(short, long)]
shell: Option<Shell>,
shell: Option<ShellCompletion>,
}

/// Performs an update of rye.
Expand Down Expand Up @@ -180,7 +223,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {

fn completion(args: CompletionCommand) -> Result<(), Error> {
clap_complete::generate(
args.shell.unwrap_or(Shell::Bash),
args.shell.unwrap_or(ShellCompletion::Bash),
&mut super::Args::command(),
"rye",
&mut std::io::stdout(),
Expand Down
Loading