1use super::Generator;
2
3use crate::acore::Error;
4use crate::SHELL_ZSH;
5
6#[derive(Debug, Clone, Copy, Default)]
7pub struct Zsh;
8
9impl Generator for Zsh {
10 type Err = Error;
11
12 fn is_avail(&self, name: &str) -> bool {
13 name == SHELL_ZSH
14 }
15
16 fn generate(&self, name: &str, bin: &str) -> Result<String, Self::Err> {
17 let template = r#"#compdef NAME
18
19function __complete_handler_NAME() {
20 local curr prev cword
21
22 cword=$CURRENT
23 curr="${words[$cword]}"
24 prev="${words[$cword - 1]}"
25
26 local completions=("${(@f)$(PROGRAM --_shell SHELL --_curr "$curr" --_prev "$prev" --_cword "$cword" "${words[@]}")}")
27
28 if [[ -n $completions ]]; then
29 _describe 'values' completions
30 else
31 _files
32 fi
33}
34
35if [[ $zsh_eval_context[-1] == loadautofunc ]]; then
36 # autoload from fpath, call function directly, not sure how it works
37 __complete_handler_NAME "$@"
38else
39 # eval/source/. command, register function for later
40 compdef __complete_handler_NAME PROGRAM
41fi
42"#;
43
44 Ok(template
45 .replace("NAME", name)
46 .replace("PROGRAM", bin)
47 .replace("SHELL", SHELL_ZSH))
48 }
49}