aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/path.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-07-30 01:17:09 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-07-30 02:09:30 +1200
commit4cbbd121c012b3962f12fdff0f1820c3b8636a44 (patch)
tree00149270df365ed0b7a1b9e9f4922fe7d5a80eb3 /bash/bash_completion.d/path.bash
parentChange ca from Bash func to sh script (diff)
downloaddotfiles-4cbbd121c012b3962f12fdff0f1820c3b8636a44.tar.gz
dotfiles-4cbbd121c012b3962f12fdff0f1820c3b8636a44.zip
Move bash completion setup into separate dir
Diffstat (limited to 'bash/bash_completion.d/path.bash')
-rw-r--r--bash/bash_completion.d/path.bash60
1 files changed, 60 insertions, 0 deletions
diff --git a/bash/bash_completion.d/path.bash b/bash/bash_completion.d/path.bash
new file mode 100644
index 00000000..fd94b7c4
--- /dev/null
+++ b/bash/bash_completion.d/path.bash
@@ -0,0 +1,60 @@
+# Completion for path
+_path() {
+
+ # What to do depends on which word we're completing
+ if ((COMP_CWORD == 1)) ; then
+
+ # Complete operation as first word
+ local cmd
+ for cmd in help list insert append remove set check ; do
+ [[ $cmd == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
+ COMPREPLY[${#COMPREPLY[@]}]=$cmd
+ done
+
+ # Complete with either directories or $PATH entries as all other words
+ else
+ case ${COMP_WORDS[1]} in
+
+ # Complete with a directory
+ insert|i|append|add|a|check|c|set|s)
+ local dirname
+ while IFS= read -rd '' dirname ; do
+ COMPREPLY[${#COMPREPLY[@]}]=$dirname
+ done < <(
+
+ # Set options to glob correctly
+ shopt -s dotglob nullglob
+
+ # Collect directory names, strip trailing slash
+ local -a dirnames
+ dirnames=("${COMP_WORDS[COMP_CWORD]}"*/)
+ dirnames=("${dirnames[@]%/}")
+
+ # Bail if no results to prevent empty output
+ ((${#dirnames[@]})) || exit 1
+
+ # Print results, quoted and null-delimited
+ printf '%q\0' "${dirnames[@]}"
+ )
+ ;;
+
+ # Complete with directories from PATH
+ remove|rm|r)
+ local -a promptarr
+ IFS=: read -d '' -a promptarr < <(printf '%s\0' "$PATH")
+ local part
+ for part in "${promptarr[@]}" ; do
+ [[ $part == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
+ COMPREPLY[${#COMPREPLY[@]}]=$(printf '%q\0' "$part")
+ done
+ ;;
+
+ # No completion
+ *)
+ return 1
+ ;;
+ esac
+ fi
+}
+
+complete -F _path path