-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmfp_bash_completion.sh
More file actions
executable file
·71 lines (59 loc) · 2.11 KB
/
mfp_bash_completion.sh
File metadata and controls
executable file
·71 lines (59 loc) · 2.11 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
68
69
70
71
#!/bin/bash
# mfp-xxx bash autocompletion script.
#
# put this file in the /etc/bash_completion.d/ or source it from your
# $HOME/.bashrc
__mfp_complete_log()
{
# Uncomment for logging
#echo "$@" >> __mfp_complete.log
return
}
__mfp_complete()
{
local cmd args words cword cur cur_raw reply r r2 prefix
__mfp_complete_log =====
# Shell passes us command line split into words in the
# COMP_WORDS array, but its idea about word separator
# characters doesn't match our goals. In particular, it
# considers '=' character the word separator, which breaks
# the long options and considers ':' character the word
# separator, which breaks the host:port parameters and
# HTTP URLs.
#
# _get_comp_words_by_ref from the bash-completion package re-splits
# the command line allowing some characters to be excluded
# from the word separators list with the -n option. Nut now
# we have another problem: shell automatically removes current
# word prefix from the COMPREPLY strings, but after re-split the
# current word might have been extended with the additional
# prefix which shell doesn't know about and hence cannot
# remove, so we have to help it.
_get_comp_words_by_ref -n "=:" words cword cur
cur_raw="${COMP_WORDS[COMP_CWORD]}"
if [ "${cur}" != "${cur_raw}" ]; then
prefix="${cur}"
prefix="${cur%"${cur_raw}"}"
if [ "${cur_raw}" == "=" ]; then
prefix="${prefix}${cur_raw}"
fi
fi
cmd="$1"
args=("${words[@]:1:$cword}")
IFS=$'\n' read -r -d '' -a reply < <("$cmd" --bash-completion "${args[@]}" && printf '\0')
__mfp_complete_log "cur: $cur, cur_raw: $cur_raw, prefix: $prefix"
# Build COMPREPLY, removing prefix
COMPREPLY=()
for r in "${reply[@]}"; do
r2="${r#"${prefix}"}"
COMPREPLY+=("${r2}")
__mfp_complete_log strip: "$r->$r2" "(prefix: ${prefix})"
done
}
complete -o nospace -F __mfp_complete mfp-cups
complete -o nospace -F __mfp_complete mfp-discover
complete -o nospace -F __mfp_complete mfp-model
complete -o nospace -F __mfp_complete mfp-ppd
complete -o nospace -F __mfp_complete mfp-proxy
complete -o nospace -F __mfp_complete mfp-usb
complete -o nospace -F __mfp_complete mfp-virtual