summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Reisner <dreisner@archlinux.org>2012-04-22 19:21:47 +0200
committerDave Reisner <dreisner@archlinux.org>2012-04-22 22:29:06 +0200
commit6e0046b7830f95a52a551c846d8c76880a3872f9 (patch)
treedaacbde0ac1421693196e5fe92804a85eade0a16
parent710650c0bfdfed3423d0c17bb7955d4dc1151b25 (diff)
downloadmkinitcpio-6e0046b7830f95a52a551c846d8c76880a3872f9.tar.gz
mkinitcpio-6e0046b7830f95a52a551c846d8c76880a3872f9.tar.xz
bash-completion: avoid using cd
Introduce _files_from_dir, which loops over arguments to find files in the given directories. Additionally allow passing a flag which will strip suffixes from any found files. Signed-off-by: Dave Reisner <dreisner@archlinux.org>
-rw-r--r--bash-completion23
1 files changed, 19 insertions, 4 deletions
diff --git a/bash-completion b/bash-completion
index b7ab5bc..9732747 100644
--- a/bash-completion
+++ b/bash-completion
@@ -35,6 +35,23 @@ _find_kernel_versions() {
COMPREPLY=($(compgen -W "${matches[*]}" -- $cur))
}
+_files_from_dirs() {
+ local files stripsuf d f
+
+ if [[ $1 = -s ]]; then
+ stripsuf=$2
+ shift 2
+ fi
+
+ for d in "$@"; do
+ for f in "$d"/*; do
+ [[ -f $f ]] && files+=("${f##*/}")
+ done
+ done
+
+ printf '%s\n' "${files[@]%$stripsuf}"
+}
+
_mkinitcpio() {
local action cur prev opts
opts=(-A --add -b --basedir -c --config -g --generate -H --hookhelp
@@ -52,11 +69,9 @@ _mkinitcpio() {
-[bt]|--basedir|--builddir)
_filedir -d ;;
-p|--preset)
- COMPREPLY=($(cd /etc/mkinitcpio.d/ && compgen -X '!*.preset' -f -- $cur))
- COMPREPLY=("${COMPREPLY[@]%.preset}") ;;
+ COMPREPLY=($(compgen -W "$(_files_from_dirs -s .preset /etc/mkinitcpio.d)" -- "$cur")) ;;
-[AHS]|--add|--hookhelp|--skiphooks)
- COMPREPLY=($(cd /lib/initcpio/install/ && compgen -f -- $cur)
- $(cd /usr/lib/initcpio/install/ && compgen -f -- $cur)) ;;
+ COMPREPLY=($(compgen -W "$(_files_from_dirs {/usr,}/lib/initcpio/install)" -- "$cur")) ;;
*)
COMPREPLY=($(compgen -W "${opts[*]}" -- "$cur")) ;;
esac