Skip to content

Commit fad89bf

Browse files
committed
feat(cli/rustup-mode): support rustup completion for nushell
1 parent c037aec commit fad89bf

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ chrono = { version = "0.4", default-features = false, features = ["std"] }
4949
clap = { version = "4", features = ["derive", "wrap_help", "string"] }
5050
clap-cargo = "0.18.3"
5151
clap_complete = "4"
52+
clap_complete_nushell = "4.5"
5253
console = "0.16"
5354
curl = { version = "0.4.44", optional = true }
5455
effective-limits = "0.5.5"

src/cli/rustup_mode.rs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ enum RustupSubcmd {
281281
/// Generate tab-completion scripts for your shell
282282
#[command(after_help = completions_help(), arg_required_else_help = true)]
283283
Completions {
284-
shell: Shell,
284+
shell: CompletionShell,
285285

286286
#[arg(default_value = "rustup")]
287287
command: CompletionCommand,
@@ -1853,8 +1853,57 @@ impl fmt::Display for CompletionCommand {
18531853
}
18541854
}
18551855

1856+
#[derive(clap::ValueEnum, Clone, Copy, Debug)]
1857+
enum CompletionShell {
1858+
Bash,
1859+
Elvish,
1860+
Fish,
1861+
Nushell,
1862+
PowerShell,
1863+
Zsh,
1864+
}
1865+
1866+
impl fmt::Display for CompletionShell {
1867+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1868+
f.write_str(match self {
1869+
CompletionShell::Bash => "bash",
1870+
CompletionShell::Elvish => "elvish",
1871+
CompletionShell::Fish => "fish",
1872+
CompletionShell::Nushell => "nushell",
1873+
CompletionShell::PowerShell => "powershell",
1874+
CompletionShell::Zsh => "zsh",
1875+
})
1876+
}
1877+
}
1878+
1879+
impl clap_complete::Generator for CompletionShell {
1880+
fn file_name(&self, name: &str) -> String {
1881+
match self {
1882+
CompletionShell::Bash => Shell::Bash.file_name(name),
1883+
CompletionShell::Elvish => Shell::Elvish.file_name(name),
1884+
CompletionShell::Fish => Shell::Fish.file_name(name),
1885+
CompletionShell::PowerShell => Shell::PowerShell.file_name(name),
1886+
CompletionShell::Zsh => Shell::Zsh.file_name(name),
1887+
1888+
CompletionShell::Nushell => clap_complete_nushell::Nushell.file_name(name),
1889+
}
1890+
}
1891+
1892+
fn generate(&self, cmd: &clap::Command, buf: &mut dyn Write) {
1893+
match self {
1894+
CompletionShell::Bash => Shell::Bash.generate(cmd, buf),
1895+
CompletionShell::Elvish => Shell::Elvish.generate(cmd, buf),
1896+
CompletionShell::Fish => Shell::Fish.generate(cmd, buf),
1897+
CompletionShell::PowerShell => Shell::PowerShell.generate(cmd, buf),
1898+
CompletionShell::Zsh => Shell::Zsh.generate(cmd, buf),
1899+
1900+
CompletionShell::Nushell => clap_complete_nushell::Nushell.generate(cmd, buf),
1901+
}
1902+
}
1903+
}
1904+
18561905
fn output_completion_script(
1857-
shell: Shell,
1906+
shell: CompletionShell,
18581907
command: CompletionCommand,
18591908
process: &Process,
18601909
) -> Result<ExitCode> {
@@ -1869,8 +1918,8 @@ fn output_completion_script(
18691918
}
18701919
CompletionCommand::Cargo => {
18711920
let script = match shell {
1872-
Shell::Bash => "/etc/bash_completion.d/cargo",
1873-
Shell::Zsh => {
1921+
CompletionShell::Bash => "/etc/bash_completion.d/cargo",
1922+
CompletionShell::Zsh => {
18741923
writeln!(process.stdout().lock(), "#compdef cargo")?;
18751924
"/share/zsh/site-functions/_cargo"
18761925
}

tests/suite/cli_rustup_ui/rustup_completions_cmd_help_flag.stdout.term.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)