Skip to content

Commit d5c2ea5

Browse files
committed
feat: add nushell completions
closes #353
1 parent d52b9f2 commit d5c2ea5

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

Cargo.lock

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ parse_duration = "2.1.1"
2929
atty = "0.2"
3030
clap_generate = "3.0.3"
3131
clap_complete = "4.5.38"
32+
clap_complete_nushell = "4.5"
3233

3334
[dev-dependencies]
3435
tempdir = "0.3.7"

src/cli_config.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ use crate::translators::{PayloadItem, SupportedTypes, TimeFormat};
22
use crate::utils::parse_duration_string;
33
use chrono::format::{parse, Parsed, StrftimeItems};
44
use clap::{Parser, Subcommand, ValueEnum};
5-
use clap_complete::Shell;
65
use jsonwebtoken::Algorithm;
76
use std::path::PathBuf;
87

8+
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
9+
pub enum ShellCompletion {
10+
Bash,
11+
Elvish,
12+
Fish,
13+
Powershell,
14+
Zsh,
15+
Nushell,
16+
}
17+
918
#[derive(Parser, Debug)]
1019
#[clap(name = "jwt")]
1120
#[clap(about, version, author)]
1221
#[clap(propagate_version = true)]
13-
1422
pub struct App {
1523
#[clap(subcommand)]
1624
pub command: Commands,
@@ -33,7 +41,7 @@ pub struct CompletionArgs {
3341
/// the shell to generate completions for
3442
#[clap(value_enum)]
3543
#[clap(index = 1)]
36-
pub shell: Shell,
44+
pub shell: ShellCompletion,
3745
}
3846

3947
#[derive(Debug, Clone, Parser)]

src/main.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use clap::{Command, CommandFactory, Parser};
1+
use clap::{CommandFactory, Parser};
2+
use clap_complete::Shell;
23
use clap_complete::{generate, Generator};
3-
use cli_config::{App, Commands, EncodeArgs};
4+
use clap_complete_nushell::Nushell;
5+
use cli_config::{App, Commands, EncodeArgs, ShellCompletion};
46
use std::io;
57
use std::process::exit;
68
use translators::decode::{decode_token, print_decoded_token};
@@ -16,8 +18,22 @@ fn warn_unsupported(arguments: &EncodeArgs) {
1618
};
1719
}
1820

19-
fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
20-
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
21+
fn print_completions(shell: &ShellCompletion) {
22+
fn generate_completions(gen: impl Generator) {
23+
let mut cmd = App::command();
24+
let name = cmd.get_name().to_string();
25+
26+
generate(gen, &mut cmd, name, &mut io::stdout());
27+
}
28+
29+
match shell {
30+
ShellCompletion::Nushell => generate_completions(Nushell),
31+
ShellCompletion::Bash => generate_completions(Shell::Bash),
32+
ShellCompletion::Elvish => generate_completions(Shell::Elvish),
33+
ShellCompletion::Fish => generate_completions(Shell::Fish),
34+
ShellCompletion::Powershell => generate_completions(Shell::PowerShell),
35+
ShellCompletion::Zsh => generate_completions(Shell::Zsh),
36+
}
2137
}
2238

2339
fn main() {
@@ -48,8 +64,7 @@ fn main() {
4864
);
4965
}
5066
Commands::Completion(arguments) => {
51-
let mut cmd = App::command();
52-
print_completions(arguments.shell, &mut cmd);
67+
print_completions(&arguments.shell);
5368
exit(0)
5469
}
5570
};

0 commit comments

Comments
 (0)