usage/complete/
bash.rs

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
55
56
57
58
59
60
61
62
63
64
65
66
use heck::ToShoutySnakeCase;

pub fn complete_bash(bin: &str, usage_cmd: &str) -> String {
    // let usage = env::USAGE_BIN.display();
    let bin_up = bin.to_shouty_snake_case();
    // let bin = &spec.bin;
    // let raw = shell_escape::unix::escape(spec.to_string().into());
    format!(
        r#"
_{bin}() {{
    if ! command -v usage &> /dev/null; then
        echo >&2
        echo "Error: usage CLI not found. This is required for completions to work in {bin}." >&2
        echo "See https://usage.jdx.dev for more information." >&2
        return 1
    fi

    if [[ -z ${{_USAGE_SPEC_{bin_up}:-}} ]]; then
        _USAGE_SPEC_{bin_up}="$({usage_cmd})"
    fi

    COMPREPLY=( $(usage complete-word --shell bash -s "${{_USAGE_SPEC_{bin_up}}}" --cword="$COMP_CWORD" -- "${{COMP_WORDS[@]}}" ) )
    if [[ $? -ne 0 ]]; then
        unset COMPREPLY
    fi
    return 0
}}

shopt -u hostcomplete && complete -o nospace -o bashdefault -o nosort -F _{bin} {bin}
# vim: noet ci pi sts=0 sw=4 ts=4 ft=sh
"#
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use insta::assert_snapshot;

    #[test]
    fn test_complete_bash() {
        assert_snapshot!(complete_bash("mycli", "mycli complete --usage").trim(), @r###"
        _mycli() {
            if ! command -v usage &> /dev/null; then
                echo >&2
                echo "Error: usage CLI not found. This is required for completions to work in mycli." >&2
                echo "See https://usage.jdx.dev for more information." >&2
                return 1
            fi

            if [[ -z ${_USAGE_SPEC_MYCLI:-} ]]; then
                _USAGE_SPEC_MYCLI="$(mycli complete --usage)"
            fi

            COMPREPLY=( $(usage complete-word --shell bash -s "${_USAGE_SPEC_MYCLI}" --cword="$COMP_CWORD" -- "${COMP_WORDS[@]}" ) )
            if [[ $? -ne 0 ]]; then
                unset COMPREPLY
            fi
            return 0
        }

        shopt -u hostcomplete && complete -o nospace -o bashdefault -o nosort -F _mycli mycli
        # vim: noet ci pi sts=0 sw=4 ts=4 ft=sh
        "###);
    }
}