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 shell completion #97

Merged
merged 6 commits into from May 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ most recent changes however.

## May

- Rye's `self` command now includes a `completion` subcommand to generate
a completion script for your shell.

- The downloaded Python distributions are now validated against the
SHA-256 hashes.

Expand Down
10 changes: 10 additions & 0 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 rye/Cargo.toml
Expand Up @@ -9,6 +9,7 @@ license = "MIT"
[dependencies]
anyhow = { version = "1.0.70", features = ["backtrace"] }
clap = { version = "4.2.2", default-features = false, features = ["derive", "usage", "wrap_help", "std"] }
clap_complete = "4.2.1"
console = "0.15.5"
curl = { version = "0.4.44", features = ["ssl", "static-curl", "static-ssl"] }
decompress = { version = "0.6.0", default-features = false, features = ["tarzst", "targz"] }
Expand Down
24 changes: 23 additions & 1 deletion rye/src/cli/rye.rs
@@ -1,7 +1,8 @@
use std::process::Command;

use anyhow::{bail, Context, Error};
use clap::Parser;
use clap::{CommandFactory, Parser};
use clap_complete::Shell;

/// Rye self management
#[derive(Parser, Debug)]
Expand All @@ -10,6 +11,14 @@ pub struct Args {
command: SubCommand,
}

/// 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>,
}

/// Performs an update of rye.
///
/// This currently just is an alias to running cargo install again with the
Expand All @@ -29,15 +38,28 @@ pub struct UpdateCommand {

#[derive(Parser, Debug)]
enum SubCommand {
Completion(CompletionCommand),
Update(UpdateCommand),
}

pub fn execute(cmd: Args) -> Result<(), Error> {
match cmd.command {
SubCommand::Completion(args) => completion(args),
SubCommand::Update(args) => update(args),
}
}

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

Ok(())
}

fn update(args: UpdateCommand) -> Result<(), Error> {
let mut helper = rename_helper::RenameHelper::new()?;
let mut cmd = Command::new("cargo");
Expand Down