forked from mgalgs/cower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_completion
121 lines (112 loc) · 3.31 KB
/
bash_completion
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
__inarray() {
local i
for i in "${@:2}"; do
[[ $i == "$1" ]] && return
done
}
__hasprefix() {
local i
for i in "${@:2}"; do
[[ $i = "$1"* ]] && return
done
}
_cower() {
local argv0=${COMP_WORDS[0]}
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD - 1]} prevprev=${COMP_WORDS[COMP_CWORD - 2]}
local shortopts=(-d -i -m -s -u -f -h -t -V -b -c -o -q -v)
local longopts=(--download --info --msearch --search --update --force --version
--brief --debug --ignore-ood --no-ignore-ood --quiet --verbose)
local longoptsarg=(--ignore --ignorerepo --target --threads --timeout --color --format
--sort --rsort -listdelim)
local allopts=("${shortopts[@]}" "${longopts[@]}" "${longoptsarg[@]}")
local sortfields=(firstsubmitted lastmodified license maintainer name outofdate version votes)
# maybe mangle the arguments in case we're looking at a --longopt=$val
[[ $cur = '=' ]] && cur=
if [[ $prev = '=' ]] && __inarray "$prevprev" "${allopts[@]}"; then
prev=$prevprev
fi
case $prev in
--format|--threads|--timeout|--listdelim)
COMPREPLY=()
return 0
;;
-t|--target)
COMPREPLY=($(compgen -d -- "$cur"))
compopt -o filenames
return 0
;;
-c|--color)
COMPREPLY=($(compgen -W 'auto never always' -- "$cur"))
return 0
;;
--rsort|--sort)
COMPREPLY=($(compgen -W '${sortfields[*]}' -- "$cur"))
return 0
;;
--ignore)
COMPREPLY=($(compgen -W "$(pacman -Qqm 2>/dev/null)" -- "$cur"))
return 0
;;
--ignorerepo)
COMPREPLY=($(compgen -W "$(sed '/^\[\(.*\)\]$/!d;s//\1/;/options/d' /etc/pacman.conf)" -- "$cur"))
return 0
;;
esac
case $cur in
--*)
if __hasprefix "$cur" "${longoptsarg[@]}"; then
compopt -o nospace
COMPREPLY=($(compgen -W '${longoptsarg[*]}' -S = -- "$cur"))
else
COMPREPLY=($(compgen -W '${longopts[*]}' -- "$cur"))
fi
return 0
;;
-*)
COMPREPLY=($(compgen -W '${allopts[*]}' -- "$cur"))
return 0
;;
*)
# completion based on mode -- always take the last specified
local i j w mode
for (( i = 1; i < ${#COMP_WORDS[*]}; i++ )); do
w=${COMP_WORDS[i]}
if (( i == 1 )) || [[ ${COMP_WORDS[i-1]} != -* ]]; then
case $w in
# mode from shortopt (possibly a bunch mushed together)
-[^-]*)
for (( j = 0; j < ${#w}; j++ )); do
case ${w:j:1} in
d|i|m|s|u)
mode=${w:j:1}
;;
esac
done
;;
# mode from long opt
--@(download|info|?(m)search|update))
mode=${w#--}
;;
esac
fi
done
case $mode in
d|download|i|info)
# complete based on AUR matches
if (( ${#cur} > 2 )); then
COMPREPLY=($(compgen -W "$("$argv0" -sq -- "$cur" 2>/dev/null)" -- "$cur"))
return 0
fi
;;
u|update)
COMPREPLY=($(compgen -W '$(pacman -Qqm 2>/dev/null)' -- "$cur"))
return 0
;;
*)
COMPREPLY=($(compgen -W '${allopts[*]}' -- "$cur"))
;;
esac
esac
}
complete -F _cower cower