Created
April 23, 2018 06:15
-
-
Save AnjaneyuluBatta505/251155b206e6ef22c157c4edd412a6cb to your computer and use it in GitHub Desktop.
workon is a bash completion function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
workon is a function | |
workon () | |
{ | |
typeset -a in_args; | |
typeset -a out_args; | |
in_args=("$@"); | |
if [ -n "$ZSH_VERSION" ]; then | |
i=1; | |
tst="-le"; | |
else | |
i=0; | |
tst="-lt"; | |
fi; | |
typeset cd_after_activate=$VIRTUALENVWRAPPER_WORKON_CD; | |
while [ $i $tst $# ]; do | |
a="${in_args[$i]}"; | |
case "$a" in | |
-h | --help) | |
virtualenvwrapper_workon_help; | |
return 0 | |
;; | |
-n | --no-cd) | |
cd_after_activate=0 | |
;; | |
-c | --cd) | |
cd_after_activate=1 | |
;; | |
*) | |
if [ ${#out_args} -gt 0 ]; then | |
out_args=("${out_args[@]-}" "$a"); | |
else | |
out_args=("$a"); | |
fi | |
;; | |
esac; | |
i=$(( $i + 1 )); | |
done; | |
set -- "${out_args[@]}"; | |
typeset env_name="$1"; | |
if [ "$env_name" = "" ]; then | |
lsvirtualenv -b; | |
return 1; | |
else | |
if [ "$env_name" = "." ]; then | |
IFS='%'; | |
env_name="$(basename $(pwd))"; | |
unset IFS; | |
fi; | |
fi; | |
virtualenvwrapper_verify_workon_home || return 1; | |
virtualenvwrapper_verify_workon_environment "$env_name" || return 1; | |
activate="$WORKON_HOME/$env_name/$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate"; | |
if [ ! -f "$activate" ]; then | |
echo "ERROR: Environment '$WORKON_HOME/$env_name' does not contain an activate script." 1>&2; | |
return 1; | |
fi; | |
type deactivate > /dev/null 2>&1; | |
if [ $? -eq 0 ]; then | |
type deactivate | grep --color=auto 'typeset env_postdeactivate_hook' > /dev/null 2>&1; | |
if [ $? -eq 0 ]; then | |
deactivate; | |
unset -f deactivate > /dev/null 2>&1; | |
fi; | |
fi; | |
virtualenvwrapper_run_hook "pre_activate" "$env_name"; | |
source "$activate"; | |
virtualenvwrapper_original_deactivate=`typeset -f deactivate | sed 's/deactivate/virtualenv_deactivate/g'`; | |
eval "$virtualenvwrapper_original_deactivate"; | |
unset -f deactivate > /dev/null 2>&1; | |
eval 'deactivate () { | |
typeset env_postdeactivate_hook | |
typeset old_env | |
# Call the local hook before the global so we can undo | |
# any settings made by the local postactivate first. | |
virtualenvwrapper_run_hook "pre_deactivate" | |
env_postdeactivate_hook="$VIRTUAL_ENV/$VIRTUALENVWRAPPER_ENV_BIN_DIR/postdeactivate" | |
old_env=$(basename "$VIRTUAL_ENV") | |
# Call the original function. | |
virtualenv_deactivate $1 | |
virtualenvwrapper_run_hook "post_deactivate" "$old_env" | |
if [ ! "$1" = "nondestructive" ] | |
then | |
# Remove this function | |
unset -f virtualenv_deactivate >/dev/null 2>&1 | |
unset -f deactivate >/dev/null 2>&1 | |
fi | |
}'; | |
VIRTUALENVWRAPPER_PROJECT_CD=$cd_after_activate virtualenvwrapper_run_hook "post_activate"; | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment