aopt_shell/script/
fish.rs

1use super::Generator;
2
3use crate::acore::Error;
4use crate::SHELL_FISH;
5
6#[derive(Debug, Clone, Copy, Default)]
7pub struct Fish;
8
9impl Generator for Fish {
10    type Err = Error;
11
12    fn is_avail(&self, name: &str) -> bool {
13        name == SHELL_FISH
14    }
15
16    fn generate(&self, name: &str, bin: &str) -> Result<String, Self::Err> {
17        let template = r#"function __complete_handler_NAME
18    set -l words
19    set -l curr
20    set -l prevs
21
22    if commandline -x >/dev/null 2>&1
23        set curr (commandline -xpt)
24        set words (commandline -xp)
25        set prevs (commandline -xc)
26    else
27        set curr (commandline -opt)
28        set words (commandline -op)
29        set prevs (commandline -oc)
30    end
31
32    set -l cword (count $prevs)
33    set -l prev $prevs[-1]
34
35    set -l completions (PROGRAM --_shell SHELL --_curr "$curr" --_prev "$prev" --_cword "$cword" (string split " " -- $words))
36
37    if test -n "$completions"
38        string split '\n' -- $completions
39    else
40        __fish_complete_path "$curr" "paths"
41    end
42end
43
44complete -f -c PROGRAM -a '(__complete_handler_NAME)'
45"#;
46
47        Ok(template
48            .replace("NAME", name)
49            .replace("PROGRAM", bin)
50            .replace("SHELL", SHELL_FISH))
51    }
52}