aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/ud.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2015-10-21 13:08:09 +1300
committerTom Ryder <tom@sanctum.geek.nz>2015-10-21 13:08:09 +1300
commitdcfc80aaa112498498d93bae3ae0eb2088244c47 (patch)
tree889854ba1c96f1c86465da1bbd5cc87f0c8d4b9f /bash/bashrc.d/ud.bash
parentHandle spaces correctly in completions (diff)
downloaddotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.tar.gz
dotfiles-dcfc80aaa112498498d93bae3ae0eb2088244c47.zip
Tidy up completion considerably; no more compgen
* Remove all instances of compgen; for filename completion it's quite broken as it relies on implicit wordsplitting in array context, and doesn't have an option to print with a null delimiter; replaced with manual for/while loops instead * Add IFS= to while/read loops over filenames * Use "dirname/s" instead of "dir/s" variables to avoid keyword collisions and for clarity * Remove some unnecessary variables * Use shorter syntax for loop exit conditions * Move completion options into functions where applicable rather than having them on the completion definition itself
Diffstat (limited to 'bash/bashrc.d/ud.bash')
-rw-r--r--bash/bashrc.d/ud.bash51
1 files changed, 35 insertions, 16 deletions
diff --git a/bash/bashrc.d/ud.bash b/bash/bashrc.d/ud.bash
index b5279004..7a715a52 100644
--- a/bash/bashrc.d/ud.bash
+++ b/bash/bashrc.d/ud.bash
@@ -32,34 +32,53 @@ ud() {
# Check and save optional second argument, target directory; default to
# $PWD (typical usage case)
- local dir
- dir=${2:-$PWD}
- if [[ ! -e $dir ]] ; then
- printf 'bash: %s: Target dir %s does not exist\n' "$FUNCNAME" "$2" >&2
+ local dirname
+ dirname=${2:-$PWD}
+ if [[ ! -e $dirname ]] ; then
+ printf 'bash: %s: Target directory %s does not exist\n' "$FUNCNAME" "$2" >&2
return 1
fi
# Append /.. to the target the specified number of times
local -i i
for (( i = 0 ; i < steps ; i++ )) ; do
- dir=${dir%/}/..
+ dirname=${dirname%/}/..
done
# Try to change into it
- cd "${opts[@]}" -- "$dir"
+ cd "${opts[@]}" -- "$dirname"
}
-# Completion is only useful for the second argument
+# Completion setup for ud
_ud() {
- if ((COMP_CWORD == 2)) ; then
- local word
- word=${COMP_WORDS[COMP_CWORD]}
- compopt -o filenames
- local IFS=$'\n'
- COMPREPLY=( $(compgen -A directory -- "$word" ) )
- else
- return 1
- fi
+
+ # The completions given are filenames and may require escaping
+ compopt -o filenames
+
+ # Only makes sense for the second argument
+ ((COMP_CWORD == 2)) || return 1
+
+ # Iterate through directories, null-separated, add them to completions
+ local dirname
+ while IFS= read -d '' -r dirname ; do
+ [[ $dirname == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
+ COMPREPLY=("${COMPREPLY[@]}" "$dirname")
+ done < <(
+
+ # Set options to glob correctly
+ shopt -s dotglob nullglob
+
+ # Collect directory names, strip trailing slashes
+ local -a dirnames
+ dirnames=(*/)
+ dirnames=("${dirnames[@]%/}")
+
+ # Bail if no results to prevent empty output
+ ((${#dirnames[@]})) || exit 1
+
+ # Print results null-delimited
+ printf '%s\0' "${dirnames[@]}"
+ )
}
complete -F _ud ud