Skip to content

Commit

Permalink
feat: nargo command to generate shell completions (#6413)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Nov 1, 2024
1 parent b473d99 commit 13856a1
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 23 deletions.
74 changes: 56 additions & 18 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"arithmetization",
"arity",
"arkworks",
"autoload",
"backpropagate",
"Backpropagation",
"barebones",
Expand Down Expand Up @@ -51,6 +52,7 @@
"codespan",
"coeff",
"combinators",
"compinit",
"comptime",
"cpus",
"cranelift",
Expand Down Expand Up @@ -91,6 +93,7 @@
"forall",
"foralls",
"formatcp",
"fpath",
"frontends",
"fuzzer",
"fxhash",
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/getting_started/noir_installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Step 1: Follow the instructions [here](https://learn.microsoft.com/en-us/windows
step 2: Follow the [Noirup instructions](#installing-noirup).
## Setting up shell completions
Once `nargo` is installed, you can [set up shell completions for it](setting_up_shell_completions).
## Uninstalling Nargo
If you installed Nargo with `noirup`, you can uninstall Nargo by removing the files in `~/.nargo`, `~/nargo`, and `~/noir_cache`. This ensures that all installed binaries, configurations, and cache related to Nargo are fully removed from your system.
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/getting_started/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ curl -L noirup.dev | bash
noirup
```

Once installed, you can [set up shell completions for the `nargo` command](setting_up_shell_completions).

### Proving backend

After installing Noir, we install a proving backend to work with our Noir programs.
Expand Down
87 changes: 87 additions & 0 deletions docs/docs/getting_started/setting_up_shell_completions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: Setting up shell completions
tags: []
sidebar_position: 3
---

The `nargo` binary provides a command to generate shell completions:

```bash
nargo generate-completion-script [shell]
```

where `shell` must be one of `bash`, `elvish`, `fish`, `powershell`, and `zsh`.

Below we explain how to install them in some popular shells.

## Installing Zsh Completions

If you have `oh-my-zsh` installed, you might already have a directory of automatically loading completion scripts — `.oh-my-zsh/completions`.
If not, first create it:

```bash
mkdir -p ~/.oh-my-zsh/completions`
```

Then copy the completion script to that directory:

```bash
nargo generate-completion-script zsh > ~/.oh-my-zsh/completions/_nargo
```

Without `oh-my-zsh`, you’ll need to add a path for completion scripts to your function path, and turn on completion script auto-loading.
First, add these lines to `~/.zshrc`:

```bash
fpath=(~/.zsh/completions $fpath)
autoload -U compinit
compinit
```

Next, create a directory at `~/.zsh/completions`:

```bash
mkdir -p ~/.zsh/completions
```

Then copy the completion script to that directory:

```bash
nargo generate-completion-script zsh > ~/.zsh/completions/_nargo
```

## Installing Bash Completions

If you have [bash-completion](https://github.com/scop/bash-completion) installed, you can just copy the completion script to the `/usr/local/etc/bash_completion.d` directory:

```bash
nargo generate-completion-script bash > /usr/local/etc/bash_completion.d/nargo
```

Without `bash-completion`, you’ll need to source the completion script directly.
First create a directory such as `~/.bash_completions/`:

```bash
mkdir ~/.bash_completions/
```

Copy the completion script to that directory:

```bash
nargo generate-completion-script bash > ~/.bash_completions/nargo.bash
```

Then add the following line to `~/.bash_profile` or `~/.bashrc`:


```bash
source ~/.bash_completions/nargo.bash
```

## Installing Fish Completions

Copy the completion script to any path listed in the environment variable `$fish_completion_path`. For example, a typical location is `~/.config/fish/completions/nargo.fish`:

```bash
nargo generate-completion-script fish > ~/.config/fish/completions/nargo.fish
```
1 change: 1 addition & 0 deletions tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ termion = "3.0.0"
# Logs
tracing-subscriber.workspace = true
tracing-appender = "0.2.3"
clap_complete = "4.5.36"

[target.'cfg(not(unix))'.dependencies]
tokio-util = { version = "0.7.8", features = ["compat"] }
Expand Down
33 changes: 33 additions & 0 deletions tooling/nargo_cli/src/cli/generate_completion_script_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use clap::{Args, CommandFactory};
use clap_complete::Shell;

use crate::errors::CliError;

use super::NargoCli;

/// Generates a shell completion script for your favorite shell
#[derive(Debug, Clone, Args)]
pub(crate) struct GenerateCompletionScriptCommand {
/// The shell to generate completions for. One of: bash, elvish, fish, powershell, zsh
pub(crate) shell: String,
}

pub(crate) fn run(command: GenerateCompletionScriptCommand) -> Result<(), CliError> {
let shell = match command.shell.to_lowercase().as_str() {
"bash" => Shell::Bash,
"elvish" => Shell::Elvish,
"fish" => Shell::Fish,
"powershell" => Shell::PowerShell,
"zsh" => Shell::Zsh,
_ => {
return Err(CliError::Generic(
"Invalid shell. Supported shells are: bash, elvish, fish, powershell, zsh"
.to_string(),
))
}
};

clap_complete::generate(shell, &mut NargoCli::command(), "nargo", &mut std::io::stdout());

Ok(())
}
Loading

0 comments on commit 13856a1

Please sign in to comment.