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 tokens
21    set -l tokenCount
22
23    if commandline -x >/dev/null 2>&1
24        set curr (commandline -xpt)
25        set words (commandline -xp)
26
27        set tokens (commandline -xc)
28    else
29        set curr (commandline -opt)
30        set words (commandline -op)
31
32        set tokens (commandline -oc)
33    end
34
35    set -l cword (count $words)
36    set -l prev $tokens[-1]
37
38    set -l completions (PROGRAM --_shell SHELL --_curr "$curr" --_prev "$prev" (string split " " -- $words))
39
40    if test -n "$completions"
41        string split '\n' -- $completions
42    else
43        __fish_complete_path "$curr" "paths"
44    end
45end
46
47complete -f -c PROGRAM -a '(__complete_handler_NAME)'
48"#;
49
50        Ok(template
51            .replace("NAME", name)
52            .replace("PROGRAM", bin)
53            .replace("SHELL", SHELL_FISH))
54    }
55}