aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/sd.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-12-02 17:59:29 +1300
committerTom Ryder <tom@sanctum.geek.nz>2018-12-02 17:59:29 +1300
commit7d6fe8b1886f902f8ffbec2a9985fae9f91121cb (patch)
tree282fac0bb1809e726a373916c90260832ab23ced /bash/bash_completion.d/sd.bash
parentReduce ud() completion to just dirnames (diff)
downloaddotfiles-7d6fe8b1886f902f8ffbec2a9985fae9f91121cb.tar.gz
dotfiles-7d6fe8b1886f902f8ffbec2a9985fae9f91121cb.zip
Overhaul Bash completion scripts
Some general changes: * Apply case sensitivity switching in more contexts, using a dynamically loaded helper function * Use array counters for appending to COMPREPLY where possible * Lots more short-circuiting to limit structural depth These changes are expansive and there will definitely be bugs.
Diffstat (limited to 'bash/bash_completion.d/sd.bash')
-rw-r--r--bash/bash_completion.d/sd.bash60
1 files changed, 20 insertions, 40 deletions
diff --git a/bash/bash_completion.d/sd.bash b/bash/bash_completion.d/sd.bash
index d6e93c78..66dea73b 100644
--- a/bash/bash_completion.d/sd.bash
+++ b/bash/bash_completion.d/sd.bash
@@ -1,52 +1,32 @@
+# Load _completion_ignore_case helper function
+if ! declare -F _completion_ignore_case >/dev/null ; then
+ source "$HOME"/.bash_completion.d/_completion_ignore_case.bash
+fi
+
# Completion function for sd; any sibling directories, excluding the self
_sd() {
- # Only makes sense for the first argument
- ((COMP_CWORD == 1)) || return
-
- # Current directory can't be root directory
- case $PWD in
- /) return 1 ;;
- esac
-
# Build list of matching sibling directories
- local dirname
- while IFS= read -rd '' dirname ; do
- [[ -n $dirname ]] || continue
- COMPREPLY[${#COMPREPLY[@]}]=$dirname
+ local ci comp
+ while IFS= read -d / -r comp ; do
+ COMPREPLY[ci++]=$comp
done < <(
- # Set options to glob correctly
+ # Make globs expand appropriately
shopt -s dotglob nullglob
-
- # Make globbing case-insensitive if appropriate
- while read -r _ setting ; do
- case $setting in
- ('completion-ignore-case on')
- shopt -s nocaseglob
- break
- ;;
- esac
- done < <(bind -v)
-
- # Collect directory names, strip leading ../ and trailing /
- local -a dirnames
- dirnames=(../"$2"*/)
- dirnames=("${dirnames[@]#../}")
- dirnames=("${dirnames[@]%/}")
-
- # Iterate again, but exclude the current directory this time
- local -a sibs
- local dirname
- for dirname in "${dirnames[@]}" ; do
- case $dirname in
- "${PWD##*/}") ;;
- *) sibs[${#sibs[@]}]=$dirname ;;
+ if _completion_ignore_case ; then
+ shopt -s nocaseglob
+ fi
+
+ # Print matching sibling dirs that are not the current dir
+ for sibling in ../"$2"*/ ; do
+ sibling=${sibling%/}
+ sibling=${sibling#../}
+ case $sibling in
+ ("${PWD##*/}") ;;
+ (*) printf '%q/' "${sibling}" ;;
esac
done
-
- # Print quoted sibling directories, null-delimited
- printf '%q\0' "${sibs[@]}"
)
}
complete -F _sd sd