-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathbash_autocomplete
More file actions
executable file
·67 lines (59 loc) · 2 KB
/
bash_autocomplete
File metadata and controls
executable file
·67 lines (59 loc) · 2 KB
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
67
#!/bin/bash
# This is a shell completion script auto-generated by https://github.com/urfave/cli for bash.
# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
__%[1]s_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}
__%[1]s_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
__%[1]s_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-shell-completion"
else
requestComp="${words[*]} --generate-shell-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
# Separate completions and descriptions
local completions=()
local descriptions=()
local longest=0
while IFS=$'\n' read -r line; do
local comp_part desc_part
if [[ "$line" == *:* ]]; then
comp_part="${line%%:*}"
desc_part="${line#*:}"
else
comp_part="$line"
desc_part=""
fi
completions+=("$comp_part")
descriptions+=("$desc_part")
(( ${#comp_part} > longest )) && longest=${#comp_part}
done <<< "$opts"
# Format completions with aligned descriptions
for i in "${!completions[@]}"; do
local padded_completion="${completions[i]}"
local pad_len=$((longest - ${#padded_completion}))
if (( pad_len > 0 )); then
padded_completion="${padded_completion}$(head -c $pad_len < /dev/zero | tr '\0' ' ')"
fi
if [[ -n "${descriptions[i]}" ]]; then
COMPREPLY+=("${padded_completion} -- ${descriptions[i]}")
else
COMPREPLY+=("${padded_completion}")
fi
done
return 0
fi
}
complete -o bashdefault -o default -o nospace -F __%[1]s_bash_autocomplete %[1]s