Skip to content

Commit 581d160

Browse files
authored
Merge pull request #961 from scop/refactor/dpkg-funcs
refactor: dpkg xfuncs to API behavior
2 parents a805a2f + babfbce commit 581d160

File tree

4 files changed

+57
-49
lines changed

4 files changed

+57
-49
lines changed

completions/apt-build

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ _comp_cmd_apt_build()
1919
COMPREPLY=($(_comp_xfunc apt-cache packages))
2020
;;
2121
remove)
22-
COMPREPLY=(
23-
$(_comp_xfunc dpkg installed_packages "$cur"))
22+
_comp_xfunc dpkg installed_packages
2423
;;
2524
esac
2625
return

completions/apt-get

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ _comp_xfunc_apt_get_installed_packages()
44
{
55
if [[ -f /etc/debian_version ]]; then
66
# Debian system
7-
COMPREPLY=($(
8-
_comp_xfunc dpkg installed_packages "$cur"
9-
))
7+
_comp_xfunc dpkg installed_packages
108
else
119
# assume RPM based
1210
_comp_xfunc rpm installed_packages

completions/aptitude

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
# Debian aptitude(1) completion -*- shell-script -*-
22

3-
# TODO: rename per API conventions, rework to use vars rather than outputting
4-
if _comp_have_command grep-status; then
5-
_comp_dpkg_hold_packages()
6-
{
7-
grep-status -P -e "^$1" -a -FStatus 'hold' -n -s Package
8-
}
9-
else
10-
_comp_dpkg_hold_packages()
11-
{
12-
command grep -B 2 'hold' /var/lib/dpkg/status |
13-
awk "/Package: $1/ { print \$2 }"
14-
}
15-
fi
16-
173
_comp_cmd_aptitude()
184
{
195
local cur prev words cword comp_args
@@ -36,12 +22,11 @@ _comp_cmd_aptitude()
3622
return
3723
;;
3824
purge | remove | reinstall | forbid-version)
39-
COMPREPLY=(
40-
$(_comp_xfunc dpkg installed_packages "$cur"))
25+
_comp_xfunc dpkg installed_packages
4126
return
4227
;;
4328
unhold)
44-
COMPREPLY=($(_comp_dpkg_hold_packages "$cur"))
29+
_comp_xfunc dpkg held_packages
4530
return
4631
;;
4732
esac

completions/dpkg

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,63 @@
11
# dpkg(1) and related commands completion -*- shell-script -*-
22

3-
if _comp_have_command grep-status; then
4-
_comp_xfunc_dpkg_installed_packages()
5-
{
6-
grep-status -P -e "^${1-}" -a -FStatus 'ok installed' -n -s Package
7-
}
8-
9-
_comp_xfunc_dpkg_purgeable_packages()
10-
{
11-
grep-status -P -e "^${1-}" -a -FStatus 'ok installed' -o -FStatus 'ok config-files' -n -s Package
12-
}
13-
else
14-
_comp_xfunc_dpkg_installed_packages()
15-
{
16-
command awk -F '\n' -v RS="" "
17-
index(\$1, \"Package: ${1-}\") == 1 &&
3+
# @since 2.12
4+
_comp_xfunc_dpkg_installed_packages()
5+
{
6+
local pkgs
7+
pkgs=$(
8+
grep-status -P -e "^${cur-}" -a \
9+
-FStatus 'ok installed' \
10+
-n -s Package 2>/dev/null ||
11+
command awk -F '\n' -v RS="" "
12+
index(\$1, \"Package: ${cur-}\") == 1 &&
1813
\$2 ~ /ok installed|half-installed|unpacked|half-configured|^Essential: yes/ {
1914
print(substr(\$1, 10));
2015
}" /var/lib/dpkg/status 2>/dev/null
21-
}
16+
)
17+
_comp_compgen -a -- -W '$pkgs'
18+
}
2219

23-
_comp_xfunc_dpkg_purgeable_packages()
24-
{
25-
command awk -F '\n' -v RS="" "
26-
index(\$1, \"Package: ${1-}\") == 1 &&
20+
# @since 2.12
21+
_comp_xfunc_dpkg_purgeable_packages()
22+
{
23+
local pkgs
24+
pkgs=$(
25+
grep-status -P -e "^${cur-}" -a \
26+
-FStatus 'ok installed' -o -FStatus 'ok config-files' \
27+
-n -s Package 2>/dev/null ||
28+
command awk -F '\n' -v RS="" "
29+
index(\$1, \"Package: ${cur-}\") == 1 &&
2730
\$2 ~ /ok installed|half-installed|unpacked|half-configured|config-files|^Essential: yes/ {
2831
print(substr(\$1, 10));
2932
}" /var/lib/dpkg/status 2>/dev/null
30-
}
31-
fi
33+
)
34+
_comp_compgen -a -- -W '$pkgs'
35+
}
3236

33-
_comp_deprecate_func _comp_dpkg_installed_packages _comp_xfunc_dpkg_installed_packages
34-
_comp_deprecate_func _comp_dpkg_purgeable_packages _comp_xfunc_dpkg_purgeable_packages
37+
# @since 2.12
38+
_comp_xfunc_dpkg_held_packages()
39+
{
40+
local pkgs=$(dpkg --get-selections ${cur:+"$cur}"} |
41+
awk '{for(i=2;i<=NF;i++){ if($i=="hold"){ print $1;break }}}')
42+
_comp_compgen -a -- -W '$pkgs'
43+
}
44+
45+
# @deprecated 2.12 use _comp_xfunc_dpkg_installed_packages instead
46+
_comp_dpkg_installed_packages()
47+
{
48+
local COMPREPLY=() cur="${1-}"
49+
# shellcheck disable=SC2119
50+
_comp_xfunc_dpkg_installed_packages
51+
printf "%s\n" "${COMPREPLY[@]}"
52+
}
53+
# @deprecated 2.12 use _comp_xfunc_dpkg_purgeable_packages instead
54+
_comp_dpkg_purgeable_packages()
55+
{
56+
local COMPREPLY=() cur="${1-}"
57+
# shellcheck disable=SC2119
58+
_comp_xfunc_dpkg_purgeable_packages
59+
printf "%s\n" "${COMPREPLY[@]}"
60+
}
3561

3662
# Debian dpkg(1) completion
3763
#
@@ -78,11 +104,11 @@ _comp_cmd_dpkg()
78104
return
79105
;;
80106
--remove | --verify | -${noargopts}[rV])
81-
COMPREPLY=($(_comp_xfunc_dpkg_installed_packages "$cur"))
107+
_comp_xfunc_dpkg_installed_packages
82108
return
83109
;;
84110
--listfiles | --purge | -${noargopts}[LP])
85-
COMPREPLY=($(_comp_xfunc_dpkg_purgeable_packages "$cur"))
111+
_comp_xfunc_dpkg_purgeable_packages
86112
return
87113
;;
88114
--debug | -${noargopts}D)
@@ -153,7 +179,7 @@ _comp_cmd_dpkg_reconfigure()
153179
COMPREPLY=($(compgen -W '--frontend --priority --all --unseen-only
154180
--help --showold --force --terse' -- "$cur"))
155181
else
156-
COMPREPLY=($(_comp_xfunc_dpkg_installed_packages "$cur"))
182+
_comp_xfunc_dpkg_installed_packages
157183
fi
158184
} &&
159185
complete -F _comp_cmd_dpkg_reconfigure -o default dpkg-reconfigure

0 commit comments

Comments
 (0)