Skip to main content

usage/complete/
bash.rs

1use crate::complete::CompleteOptions;
2use heck::ToSnakeCase;
3
4pub fn complete_bash(opts: &CompleteOptions) -> String {
5    let usage_bin = &opts.usage_bin;
6    let bin = &opts.bin;
7    let bin_snake = bin.to_snake_case();
8    let spec_variable = if let Some(cache_key) = &opts.cache_key {
9        format!("_usage_spec_{bin_snake}_{}", cache_key.to_snake_case())
10    } else {
11        format!("_usage_spec_{bin_snake}")
12    };
13    let mut out = vec![];
14    let generated_comment = if let Some(source_file) = &opts.source_file {
15        format!("# @generated by usage-cli from {source_file}")
16    } else {
17        "# @generated by usage-cli from usage spec".to_string()
18    };
19    out.push(generated_comment);
20    if opts.include_bash_completion_lib {
21        out.push(include_str!("../../bash-completion/bash_completion").to_string());
22        out.push("\n".to_string());
23    };
24    out.push(format!(
25        r#"_{bin_snake}() {{
26    if ! type -p {usage_bin} &> /dev/null; then
27        echo >&2
28        echo "Error: {usage_bin} CLI not found. This is required for completions to work in {bin}." >&2
29        echo "See https://usage.jdx.dev for more information." >&2
30        return 1
31    fi"#));
32
33    // Build logic to write spec directly to file without storing in shell variables
34    let file_write_logic = if let Some(usage_cmd) = &opts.usage_cmd {
35        if opts.cache_key.is_some() {
36            format!(
37                r#"if [[ ! -f "$spec_file" ]]; then
38        {usage_cmd} >| "$spec_file"
39    fi"#
40            )
41        } else {
42            format!(r#"{usage_cmd} >| "$spec_file""#)
43        }
44    } else if let Some(spec) = &opts.spec {
45        let heredoc = format!(
46            r#"cat >| "$spec_file" <<'__USAGE_EOF__'
47{spec}
48__USAGE_EOF__"#,
49            spec = spec.to_string().trim()
50        );
51        if opts.cache_key.is_some() {
52            format!(
53                r#"if [[ ! -f "$spec_file" ]]; then
54    {heredoc}
55fi"#
56            )
57        } else {
58            heredoc.to_string()
59        }
60    } else {
61        String::new()
62    };
63
64    out.push(format!(
65        r#"
66	local cur prev words cword was_split comp_args
67    _comp_initialize -n : -- "$@" || return
68    local spec_file="${{TMPDIR:-/tmp}}/usage_{spec_variable}.spec"
69    {file_write_logic}
70    # shellcheck disable=SC2207
71	_comp_compgen -- -W "$(command {usage_bin} complete-word --shell bash -f "$spec_file" --cword="$cword" -- "${{words[@]}}")"
72	_comp_ltrim_colon_completions "$cur"
73    # shellcheck disable=SC2181
74    if [[ $? -ne 0 ]]; then
75        unset COMPREPLY
76    fi
77    return 0
78}}
79
80if [[ "${{BASH_VERSINFO[0]}}" -eq 4 && "${{BASH_VERSINFO[1]}}" -ge 4 || "${{BASH_VERSINFO[0]}}" -gt 4 ]]; then
81    shopt -u hostcomplete && complete -o nospace -o bashdefault -o nosort -F _{bin_snake} {bin}
82else
83    shopt -u hostcomplete && complete -o nospace -o bashdefault -F _{bin_snake} {bin}
84fi
85# vim: noet ci pi sts=0 sw=4 ts=4 ft=sh
86"#
87    ));
88
89    out.join("\n")
90}
91
92/// Generates a bash "init" script that wires up tab-completion for any command
93/// on `$PATH` whose first line is a `usage` shebang. The user sources this once
94/// from their shell rc; per-script `usage g completion bash <bin> -f <script>`
95/// generation is no longer required.
96///
97/// Mechanism: registers a `complete -D` default handler. On `<Tab>`, the handler
98/// resolves the command path, peeks the first line, and if it looks like a
99/// `usage` shebang, dispatches to `usage complete-word`. Otherwise it chains to
100/// `_completion_loader` (bash-completion's dynamic loader) when present.
101pub fn complete_bash_init(usage_bin: &str) -> String {
102    format!(
103        r##"# @generated by usage-cli — auto-completion for usage shebang scripts
104# Source this file from your bashrc to enable <Tab> completion for any command
105# on $PATH whose first line is a `usage` shebang.
106#
107# Sourcing order: this script registers a `complete -D` default handler. Bash
108# only allows one such handler — whichever script registers last wins. Source
109# this AFTER bash-completion (e.g. `/etc/bash_completion` or
110# `/usr/share/bash-completion/bash_completion`) so that bash-completion's own
111# default handler is captured below and chained to for non-usage commands.
112
113# Capture any pre-existing `complete -D` handler so we can chain to it for
114# commands that aren't usage shebang scripts (e.g. bash-completion's loader).
115# `complete -p -D` exits non-zero if no -D handler is set; tolerate that under
116# `set -e`.
117_usage_chained_default_complete=""
118{{
119    _usage_existing_d="$(complete -p -D 2>/dev/null || true)"
120    if [[ "$_usage_existing_d" =~ -F[[:space:]]+([^[:space:]]+) ]]; then
121        _usage_chained_default_complete="${{BASH_REMATCH[1]}}"
122    fi
123    unset _usage_existing_d
124}}
125
126_usage_default_complete() {{
127    local cmd="${{COMP_WORDS[0]}}"
128    local cmdpath
129    if [[ "$cmd" == */* ]]; then
130        cmdpath="$cmd"
131    else
132        cmdpath="$(type -P "$cmd" 2>/dev/null)"
133    fi
134
135    if [[ -n "$cmdpath" && -f "$cmdpath" ]]; then
136        local first
137        if IFS= read -r first < "$cmdpath" 2>/dev/null && [[ "$first" == "#!"*"usage"* ]]; then
138            if type -p {usage_bin} &> /dev/null; then
139                local IFS=$'\n'
140                # shellcheck disable=SC2207
141                COMPREPLY=( $(command {usage_bin} complete-word --shell bash -f "$cmdpath" --cword="$COMP_CWORD" -- "${{COMP_WORDS[@]}}") )
142                return 0
143            fi
144        fi
145    fi
146
147    # Chain to whatever default handler was registered before us, if any.
148    if [[ -n "$_usage_chained_default_complete" ]] \
149        && [[ "$_usage_chained_default_complete" != "_usage_default_complete" ]] \
150        && declare -F "$_usage_chained_default_complete" >/dev/null 2>&1; then
151        "$_usage_chained_default_complete" "$@"
152        return $?
153    fi
154    if declare -F _completion_loader >/dev/null 2>&1; then
155        _completion_loader "$@"
156        return $?
157    fi
158    return 1
159}}
160
161complete -D -F _usage_default_complete -o bashdefault -o default
162# vim: noet ci pi sts=0 sw=4 ts=4 ft=sh
163"##
164    )
165}
166
167#[cfg(test)]
168mod tests {
169    use super::*;
170    use crate::test::SPEC_KITCHEN_SINK;
171    use insta::assert_snapshot;
172
173    #[test]
174    fn test_complete_bash_init() {
175        assert_snapshot!(complete_bash_init("usage"));
176    }
177
178    #[test]
179    fn test_complete_bash() {
180        assert_snapshot!(complete_bash(&CompleteOptions {
181            usage_bin: "usage".to_string(),
182            shell: "bash".to_string(),
183            bin: "mycli".to_string(),
184            cache_key: None,
185            spec: None,
186            usage_cmd: Some("mycli complete --usage".to_string()),
187            include_bash_completion_lib: false,
188            source_file: None,
189        }));
190        assert_snapshot!(complete_bash(&CompleteOptions {
191            usage_bin: "usage".to_string(),
192            shell: "bash".to_string(),
193            bin: "mycli".to_string(),
194            cache_key: Some("1.2.3".to_string()),
195            spec: None,
196            usage_cmd: Some("mycli complete --usage".to_string()),
197            include_bash_completion_lib: false,
198            source_file: None,
199        }));
200        assert_snapshot!(complete_bash(&CompleteOptions {
201            usage_bin: "usage".to_string(),
202            shell: "bash".to_string(),
203            bin: "mycli".to_string(),
204            cache_key: None,
205            spec: Some(SPEC_KITCHEN_SINK.clone()),
206            usage_cmd: None,
207            include_bash_completion_lib: false,
208            source_file: None,
209        }));
210    }
211}