-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaggie.bash-completion
161 lines (144 loc) · 4.99 KB
/
flaggie.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#
# Flaggie Bash Shell Command Completion
#
# this code depends on 'gentoo' completion file from gentoo-bashcomp, so be sure
# to either install that package and then enable 'gentoo' via 'eselect':
# # eselect bashcomp enable gentoo
# or get the 'gentoo.bash-completion' from my bitbucket repository
# (http://bitbucket.org/mruwek/bash-completion/) and place it in
# /root/.bash_completion.d
__get_iuse()
{
local pkgcache ns
pkgcache="$1"
ns="$2"
if [[ $ns == "lic::" ]]; then
sed -n "7 p" $pkgcache 2>/dev/null | sed -e 's/linguas_.\{2,5\} //g;s/^|| ( \(.*\) )/\1/g;s/-\* //g;s/\(\s\)[-+]\|^[-+]/\1/g';
elif [[ $ns == "kw::" ]]; then
sed -n "9 p" $pkgcache 2>/dev/null | sed -e 's/linguas_.\{2,5\} //g;s/^|| ( \(.*\) )/\1/g;s/-\* //g;s/\(\s\)[-+]\|^[-+]/\1/g';
elif [[ $ns == "use::" ]]; then
sed -n "11 p" $pkgcache 2>/dev/null | sed -e 's/linguas_.\{2,5\} //g;s/^|| ( \(.*\) )/\1/g;s/-\* //g;s/\(\s\)[-+]\|^[-+]/\1/g';
elif [[ $ns == "env::" ]]; then
find /etc/portage/env/ -type f | sed -e 's:/etc/portage/env/::'
find /etc/portage/env/ -type d | sed -e '1d;s:/etc/portage/env/\(.*\):\1/:'
else
sed -n "7 p;9 p;11 p" $pkgcache 2>/dev/null | sed -e 's/linguas_.\{2,5\} //g;s/^|| ( \(.*\) )/\1/g;s/-\* //g;s/\(\s\)[-+]\|^[-+]/\1/g';
find /etc/portage/env/ -type f| sed -e 's:/etc/portage/env/::'
find /etc/portage/env/ -type d | sed -e '1d;s:/etc/portage/env/\(.*\):\1/:'
fi
}
_flaggie_use()
{
local cur mode portdir pkg atom iuse ns
mode="$1"
cur="$2"
pkg=${COMP_WORDS[1]}
[[ ${pkg} == '=' ]] && pkg=${COMP_WORDS[2]}
portdir=$(_portdir -o)
atom=(`qatom "${pkg}"`)
if [[ ${prev} == *lic* || ${prev} == :: && ${COMP_WORDS[$COMP_CWORD-2]} == *lic ]]; then
ns='lic::'
elif [[ ${prev} == *kw* || ${prev} == :: && ${COMP_WORDS[$COMP_CWORD-2]} == *kw ]]; then
ns='kw::'
elif [[ ${prev} == *use* || ${prev} == :: && ${COMP_WORDS[$COMP_CWORD-2]} == *use ]]; then
ns='use::'
elif [[ ${prev} == *env* || ${prev} == :: && ${COMP_WORDS[$COMP_CWORD-2]} == *env ]]; then
ns='env::'
else
ns=''
fi
if [[ ${atom[2]} == "(null)" ]]; then
iuse=$(\
for pd in ${portdir}; do
builtin cd ${pd}/metadata/cache/${atom[0]} 2>/dev/null;
for pkgcache in ${atom[1]}-[0-9]*; do
__get_iuse $pkgcache $ns
done
done)
else
if [[ ${atom[3]} == "" ]]; then
iuse=$(\
for pd in ${portdir}; do
builtin cd ${pd}/metadata/cache/${atom[0]} 2>/dev/null;
pkgcache="${atom[1]}-${atom[2]}";
__get_iuse $pkgcache $ns
done)
else
iuse=$(\
for pd in ${portdir}; do
builtin cd ${pd}/metadata/cache/${atom[0]} 2>/dev/null;
pkgcache="${atom[1]}-${atom[2]}-${atom[3]}";
__get_iuse $pkgcache $ns
done)
fi
fi
if [[ -n "${iuse}" && -z $ns ]]; then
if [[ $mode == "enable" ]]; then
iuse=`echo $iuse | sed -e "s/\(^\| \)/\1+/g"`;
elif [[ $mode == "disable" ]]; then
iuse=`echo $iuse | sed -e 's/\(^\| \)/\1-/g'`;
elif [[ $mode == "reset" ]]; then
iuse=`echo $iuse | sed -e 's/\(^\| \)/\1%/g'`;
elif [[ $mode == "status_print" ]]; then
iuse=`echo $iuse | sed -e 's/^/\?/g;s/ / \?/g'`;
if [[ $cur == "i" ]]; then
COMPREPLY=($(compgen -W "$iuse" -- "?"))
fi
fi
if [[ $COMPREPLY == "" ]]; then
COMPREPLY=($(compgen -W "$iuse" -- "$cur"))
fi
return
elif [[ -n "${iuse}" && -n $ns ]]; then
COMPREPLY=($(compgen -W "$iuse" -- "$cur"))
else
COMPREPLY=()
return
fi
}
#
# Main completion function for flaggie
#
_flaggie()
{
local cur prev mode portdir use atom
portdir=$(_portdir)
COMPREPLY=()
prev="$3"
cur="$2"
case "${prev}" in
flaggie)
case "${cur}" in
-*)
COMPREPLY=($(compgen -W "--quiet --strict --drop-ineffective
--sort-entries --sort-flags --sort --cleanup
--drop-unmatched-pkgs --drop-unmatched-flags --destructive-cleanup" -- "$cur"))
;;
*)
_pkgname -A $cur
;;
esac
;;
--)
return
;;
*)
case "${cur}" in
-*)
_flaggie_use disable $cur
;;
%*)
_flaggie_use reset $cur
;;
"?"*)
_flaggie_use status_print $cur
;;
*)
_flaggie_use enable $cur
;;
esac
;;
esac
}
complete -F _flaggie flaggie
# vim: set ft=sh tw=80 sw=4 et :