.functions: Add autocompletion of proj() to bash

This commit is contained in:
Salt 2018-11-06 18:20:37 -06:00
parent b42eacc539
commit e7552dd682

View File

@ -10,14 +10,13 @@ proj() {
echo "proj: requires argument" echo "proj: requires argument"
return 1 return 1
fi fi
projname="${1//[^ a-zA-Z0-9]/}" projname="${1//[^ a-zA-Z0-9.]/}"
projdir="$HOME/Projects/$projname" projdir="$HOME/Projects/$projname"
# Ensure we have a ~/Projects directory # Ensure we have a ~/Projects directory
mkdir -p "$HOME/Projects" > /dev/null 2>&1 mkdir -p "$HOME/Projects" > /dev/null 2>&1
# cd into the project or make it if necessary # cd into the project or make it if necessary
if [ -d "$projdir" ]; then if [ -d "$projdir" ]; then
# It exists # It exists
echo "Going to existing project \"$projname\""
cd "$projdir" cd "$projdir"
else else
# It does not exist # It does not exist
@ -35,4 +34,21 @@ proj() {
fi fi
fi fi
} }
# Autocompletion for bash
complete -F _proj proj > /dev/null 2>&1 &&
_proj() {
[ "${#COMP_WORDS[@]}" != "2" ] && return 0
for dir in $HOME/Projects/*; do
reply="$(basename "$dir")"
reply="${reply//[^ a-zA-Z0-9.]/}"
COMPREPLY+=" $reply"
done
COMPREPLY=($(compgen -W "$COMPREPLY" "${COMP_WORDS[COMP_CWORD]}"))
return 0
}
# Autocompletion for zsh
compdef _proj proj > /dev/null 2>&1 &&
_proj() {
return 0
}