Skip to main content

git_wok/cmd/
completion.rs

1use anyhow::Result;
2use clap::{CommandFactory, ValueEnum};
3use clap_complete::{Shell, generate};
4use std::io;
5
6/// Generate shell completion script for the specified shell.
7pub fn completion<T: CommandFactory>(shell: CompletionShell) -> Result<()> {
8    let mut cmd = T::command();
9    let bin_name = "wok";
10    let shell: Shell = shell.into();
11
12    generate(shell, &mut cmd, bin_name, &mut io::stdout());
13
14    Ok(())
15}
16
17/// Shell types for completion generation.
18#[derive(Debug, Clone, Copy, ValueEnum)]
19pub enum CompletionShell {
20    Bash,
21    Fish,
22    Zsh,
23}
24
25impl From<CompletionShell> for Shell {
26    fn from(shell: CompletionShell) -> Self {
27        match shell {
28            CompletionShell::Bash => Shell::Bash,
29            CompletionShell::Fish => Shell::Fish,
30            CompletionShell::Zsh => Shell::Zsh,
31        }
32    }
33}