-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added comments and changed internal variable names
- Loading branch information
Timothy
committed
Mar 28, 2019
1 parent
9f6f08e
commit 5d4e1f6
Showing
1 changed file
with
52 additions
and
31 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,65 @@ | ||
################################################################################ | ||
############ AutoVenv for Fish Shell v2019.03.28 by @TimothyBrown ############ | ||
################################################################################ | ||
## AutoVenv Settings | ||
if status is-interactive | ||
test -z "$autovenv_announce" | ||
and set -g autovenv_announce "yes" | ||
test -z "$autovenv_enable" | ||
and set -g autovenv_enable "yes" | ||
end | ||
|
||
function autovenv --on-variable PWD -d "Automatic activation of Python virtual enviroments" | ||
test ! "$autovenv_enable" = "yes" | ||
and return | ||
for autovenv_find_dir in (string split -n '/' "$PWD") | ||
set autovenv_find_tree "$autovenv_find_tree/$autovenv_find_dir" | ||
if test -e "$autovenv_find_tree/bin/activate.fish" | ||
set autovenv_source "$autovenv_find_tree/bin/activate.fish" | ||
if test "$autovenv_announce" = "yes" | ||
set autovenv_name_old "$autovenv_name" | ||
set -g autovenv_name (basename "$autovenv_find_tree") | ||
end | ||
break | ||
## AutoVenv Function | ||
# Activates on directory changes. | ||
function autovenv --on-variable PWD -d "Automatic activation of Python virtual environments" | ||
# Check for the enable flag and make sure we're running interactive, if not return. | ||
test ! "$autovenv_enable" = "yes" | ||
or not status is-interactive | ||
and return | ||
# We start by splitting our CWD path into individual elements and iterating over each element. | ||
# If our CWD is `/opt/my/hovercraft/eels` we split it into a variable containing 4 entries: | ||
# `opt`, `my`, `hovercraft` and `eels`. We then build back up the path by iterating over the list | ||
# and adding each element to the previous one. (We start with `/opt`, then `/opt/my` and so on.) | ||
# During each run through the loop we test for a sub-directory/file called `bin/activate.fish`. If a | ||
# venv is found we go ahead and break out of the loop, otherwise continue. We go through all of this | ||
# instead of just checking the CWD to handle cases where the user moves into a sub-directory of the venv. | ||
for _dir in (string split -n '/' "$PWD") | ||
set -l _tree "$_tree/$_dir" | ||
if test -e "$_tree/bin/activate.fish" | ||
set _source "$_tree/bin/activate.fish" | ||
if test "$autovenv_announce" = "yes" | ||
set -g __autovenv_old $__autovenv_new | ||
set -g __autovenv_new (basename $_tree) | ||
end | ||
break | ||
end | ||
end | ||
# If we're *not* in an active venv and the venv source dir exists we activate it and return. | ||
if test -z "$VIRTUAL_ENV" -a -e "$_source" | ||
source "$_source" | ||
if test "$autovenv_announce" = "yes" | ||
echo "Activated Virtual Environment ($__autovenv_new)" | ||
end | ||
if test -z "$VIRTUAL_ENV" -a -e "$autovenv_source" | ||
source "$autovenv_source" | ||
# Next we check to see if we're already in an active venv. If so we proceed with further tests. | ||
else if test -n "$VIRTUAL_ENV" | ||
# Check to see if our CWD is inside the venv directory. | ||
set _dir (string match -n "$VIRTUAL_ENV*" "$PWD") | ||
# If we're no longer inside the venv dirctory deactivate it and return. | ||
if test -z "$_dir" -a ! -e "$_source" | ||
deactivate | ||
if test "$autovenv_announce" = "yes" | ||
echo "Activated Virtual Enviroment ($autovenv_name)" | ||
echo "Deactivated Virtual Enviroment ($__autovenv_new)" | ||
set -e __autovenv_new | ||
set -e __autovenv_old | ||
end | ||
else if test -n "$VIRTUAL_ENV" | ||
set autovenv_dir (string match -n "$VIRTUAL_ENV*" "$PWD") | ||
if test -z "$autovenv_dir" -a ! -e "$autovenv_source" | ||
deactivate | ||
if test "$autovenv_announce" = "yes" | ||
echo "Deactivated Virtual Enviroment ($autovenv_name)" | ||
set -e autovenv_name | ||
set -e autovenv_name_old | ||
end | ||
else if test -z "$autovenv_dir" -a -e "$autovenv_source" | ||
deactivate | ||
source "$autovenv_source" | ||
if test "$autovenv_announce" = "yes" | ||
echo "Switched Virtual Enviroments ($autovenv_name_old => $autovenv_name)" | ||
end | ||
# If we've switched into a different venv directory, deactivate the old and activate the new. | ||
else if test -z "$_dir" -a -e "$_source" | ||
deactivate | ||
source "$_source" | ||
if test "$autovenv_announce" = "yes" | ||
echo "Switched Virtual Environments ($__autovenv_old => $__autovenv_new)" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
################################################################################ |