Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Added option to specify Crosstool-NG version, using `-c` or `--crosstools-toolchain-version`
- Added an option for [NuShell](https://www.nushell.sh/) completion

### Fixed

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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async-trait = "0.1.88"
bytes = "1.10.1"
clap = { version = "4.5.38", features = ["derive", "env"] }
clap_complete = "4.5.50"
clap_complete_nushell = "4.5.8"
directories = "6.0.0"
env_logger = "0.11.8"
flate2 = "1.1.1"
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ Options:
```
### Completions Subcommand

For detailed instructions on how to enable tab completion, see [Enable tab completion for Bash, Fish, Zsh, or PowerShell](#enable-tab-completion-for-bash-fish-zsh-or-powershell) section.
For detailed instructions on how to enable tab completion, see [Enable tab completion for Bash, Fish, Zsh, PowerShell or NuShell](#enable-tab-completion-for-bash-fish-zsh-or-powershell) section.

```
Usage: espup completions [OPTIONS] <SHELL>

Arguments:
<SHELL> Shell to generate completions for [possible values: bash, elvish, fish, powershell, zsh]
<SHELL> Shell to generate completions for [possible values: bash, zsh, fish, powershell, elvish, nushell]

Options:
-l, --log-level <LOG_LEVEL> Verbosity level of the logs [default: info] [possible values: debug, info, warn, error]
Expand Down Expand Up @@ -295,6 +295,10 @@ $ espup completions zsh > ~/.zfunc/_espup
$ espup completions powershell >> $PROFILE.CurrentUserCurrentHost
# or
$ espup completions powershell | Out-String | Invoke-Expression

# NuShell
$ mkdir -p ~/.config/nushell/completions
$ espup completions nushell > ~/.config/nushell/completions/espup.nu
```

**Note**: you may need to restart your shell in order for the changes to take
Expand Down
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Command line interface.

use crate::completion_shell::CompletionShell;
use crate::targets::{Target, parse_targets};
use clap::Parser;
use clap_complete::Shell;
use std::{collections::HashSet, path::PathBuf};

#[derive(Debug, Parser)]
Expand All @@ -11,7 +11,7 @@ pub struct CompletionsOpts {
#[arg(short = 'l', long, default_value = "info", value_parser = ["debug", "info", "warn", "error"])]
pub log_level: String,
/// Shell to generate completions for.
pub shell: Shell,
pub shell: CompletionShell,
}

#[derive(Debug, Parser)]
Expand Down
72 changes: 72 additions & 0 deletions src/completion_shell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use clap::ValueEnum;
use clap_complete::Shell;
use clap_complete_nushell::Nushell;
use std::fmt;
use std::str::FromStr;

#[derive(Debug, Clone)]
pub enum CompletionShell {
Clap(Shell),
Nushell,
}

impl CompletionShell {
pub fn generate(&self, cmd: &mut clap::Command, bin_name: &str, out: &mut dyn std::io::Write) {
match self {
CompletionShell::Clap(sh) => {
clap_complete::generate(*sh, cmd, bin_name, out);
}
CompletionShell::Nushell => {
clap_complete::generate(Nushell, cmd, bin_name, out);
}
}
}
}

impl ValueEnum for CompletionShell {
fn value_variants<'a>() -> &'a [Self] {
use Shell::*;
// Static list of variants, one for each possible shell
const VARIANTS: &[CompletionShell] = &[
CompletionShell::Clap(Bash),
CompletionShell::Clap(Zsh),
CompletionShell::Clap(Fish),
CompletionShell::Clap(PowerShell),
CompletionShell::Clap(Elvish),
CompletionShell::Nushell,
];
VARIANTS
}

fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
Some(match self {
CompletionShell::Clap(sh) => sh.to_possible_value()?,
CompletionShell::Nushell => clap::builder::PossibleValue::new("nushell"),
})
}
}

impl fmt::Display for CompletionShell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CompletionShell::Clap(shell) => write!(f, "{shell}"),
CompletionShell::Nushell => write!(f, "nushell"),
}
}
}

impl FromStr for CompletionShell {
type Err = String;

fn from_str(input: &str) -> Result<Self, Self::Err> {
match input.to_ascii_lowercase().as_str() {
"bash" | "zsh" | "fish" | "powershell" | "elvish" => input
.parse::<Shell>()
.map(CompletionShell::Clap)
.map_err(|e| e.to_string()),

"nushell" => Ok(CompletionShell::Nushell),
_ => Err(format!("unsupported shell: {input}")),
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cli;
mod completion_shell;
pub mod env;
pub mod error;
pub mod host_triple;
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ async fn completions(args: CompletionsOpts) -> Result<()> {

info!("Generating completions for {} shell", args.shell);

clap_complete::generate(args.shell, &mut Cli::command(), "espup", &mut stdout());
args.shell
.generate(&mut Cli::command(), "espup", &mut stdout());

info!("Completions successfully generated!");

Expand Down
Loading