1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::{env, Spec};

pub fn complete_bash(spec: &Spec) -> String {
    let usage = &*env::USAGE_CMD;
    let bin = &spec.bin;
    let raw = shell_escape::unix::escape(spec.to_string().into());
    format!(
        r#"
_{bin}() {{
    local raw
    spec={raw}

    COMPREPLY=($({usage} complete-word -s "$spec" --cword="$COMP_CWORD" -- "${{COMP_WORDS[@]}}"))
    #COMPREPLY=($(compgen -W "${{COMPREPLY[*]}}" -- "${{COMP_WORDS[$COMP_CWORD]}}"))
    return 0
}}

complete -F _{bin} {bin}
# vim: noet ci pi sts=0 sw=4 ts=4 ft=sh
"#
    )
}

// fn render_args(cmds: &[&SchemaCmd]) -> String {
//     format!("XX")
// }

#[cfg(test)]
mod tests {
    use crate::parse::spec::Spec;

    use super::*;

    #[test]
    fn test_complete_bash() {
        let spec: Spec = r#"
        "#
        .parse()
        .unwrap();
        assert_snapshot!(complete_bash(&spec).trim(), @r###"
        _() {
            local raw
            spec=''

            COMPREPLY=($(usage complete-word -s "$spec" --cword="$COMP_CWORD" -- "${COMP_WORDS[@]}"))
            #COMPREPLY=($(compgen -W "${COMPREPLY[*]}" -- "${COMP_WORDS[$COMP_CWORD]}"))
            return 0
        }

        complete -F _ 
        # vim: noet ci pi sts=0 sw=4 ts=4 ft=sh
        "###);
    }
}