Skip to main content

git_stk/
completions.rs

1use std::io;
2
3use anyhow::Result;
4use clap::CommandFactory;
5use clap_complete::Shell;
6
7use crate::cli::Cli;
8
9/// Lets git's bash completion handle `git stk <TAB>` by delegating to the
10/// clap-generated completer, which only knows the `git-stk` binary form.
11///
12/// The clap completer reads the command name, current word, and previous word
13/// from its positional arguments (the way `complete -F` invokes it), so the
14/// shim must pass them explicitly after rewriting the word list.
15const BASH_GIT_SHIM: &str = r#"
16_git_stk() {
17    local cur prev
18    COMP_WORDS=("git-stk" "${COMP_WORDS[@]:2}")
19    COMP_CWORD=$((COMP_CWORD - 1))
20    cur="${COMP_WORDS[COMP_CWORD]}"
21    prev="${COMP_WORDS[COMP_CWORD - 1]}"
22    _git-stk git-stk "$cur" "$prev"
23}
24"#;
25
26pub fn print(shell: Shell) -> Result<()> {
27    let mut command = Cli::command();
28    clap_complete::generate(shell, &mut command, "git-stk", &mut io::stdout());
29
30    if shell == Shell::Bash {
31        print!("{BASH_GIT_SHIM}");
32    }
33
34    Ok(())
35}