From 6e0046b7830f95a52a551c846d8c76880a3872f9 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sun, 22 Apr 2012 13:21:47 -0400 Subject: 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 --- bash-completion | 23 +++++++++++++++++++---- 1 file 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 -- cgit v1.2.3-24-g4f1b