venv: add pyenv support

This commit is contained in:
Alexander Gehrke 2020-04-07 11:16:29 +02:00
parent 0b1b1344d7
commit 284a454a9a

View file

@ -1,7 +1,16 @@
#!/bin/zsh #!/bin/zsh
zparseopts -D -E c=cdhere -cdvenv=cdvenv h=help -help=help n=newvenv -new=newvenv t=tmsu -tmsu=tmsu source ${$(realpath "$0"):h:h}/lib/common.zsh
if [[ -n $help ]]; then
zparseopts -D -E \
c=cdvenv -cdvenv=cdvenv \
h=help -help=help \
n=newvenv -new=newvenv \
t=tmsu -tmsu=tmsu \
p=pyenv -pyenv=pyenv \
f=find -find=find
if [[ $help ]]; then
<<-HELP <<-HELP
Usage: ${0:t} [-c] [DIR] Usage: ${0:t} [-c] [DIR]
${0:t} -t ${0:t} -t
@ -10,9 +19,14 @@ if [[ -n $help ]]; then
Otherwise recursively look up virtualenvs in DIR (by looking for dirs ending Otherwise recursively look up virtualenvs in DIR (by looking for dirs ending
in "venv") and select one with fzf. in "venv") and select one with fzf.
OPTIONS:
-c, --cdvenv Change to the venv directory instead of going back to the -c, --cdvenv Change to the venv directory instead of going back to the
current working directory current working directory (not for pyenv virtualenvs)
-t, --tmsu list only venvs in a directory with tmsu tag lang=python -t, --tmsu list only venvs named "venv" in a directory with tmsu tag lang=python
-p, --pyenv Use pyenv virtualenvs
-f, --find Search for virtualenvs by looking for directories ending with 'venv'
If neither -p or -f are given, the default behaviour is to use both
HELP HELP
exit exit
fi fi
@ -28,6 +42,14 @@ venvsh() {
$SHELL $SHELL
} }
pyenvsh() {
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
pyenv activate "$1"
$SHELL
}
if [[ -n $tmsu ]]; then if [[ -n $tmsu ]]; then
venv=$( venv=$(
for i in $(tmsu files --directory lang=python); do for i in $(tmsu files --directory lang=python); do
@ -40,7 +62,7 @@ if [[ -n $tmsu ]]; then
fi fi
fi fi
if [[ -n $newvenv ]]; then if [[ $newvenv ]]; then
python -mvenv ${1:-venv} python -mvenv ${1:-venv}
exec venvsh ${1:-venv} exec venvsh ${1:-venv}
fi fi
@ -49,7 +71,23 @@ if [[ -n $1 && -e $1/bin/activate ]]; then
exec venvsh $1 exec venvsh $1
fi fi
pyenvs() {
if exists pyenv && pyenv commands | grep -q 'virtualenvs'; then
pyenv virtualenvs --bare --skip-aliases | sed -e 's/^/pyenv!/'
fi
}
if [[ $pyenv && ! $find ]]; then
venv=$(pyenvs | fzf)
elif [[ ! $pyenv && $find ]]; then
venv=$(fd -I -t d 'venv$' $1 | fzf) venv=$(fd -I -t d 'venv$' $1 | fzf)
else
venv=$(cat <(fd -I -t d 'venv$' $1) <(pyenvs) | fzf)
fi
if [[ -n $venv ]]; then if [[ -n $venv ]]; then
if [[ $venv =~ pyenv\!* ]]; then
exec pyenvsh ${venv##pyenv!}
else
exec venvsh $venv exec venvsh $venv
fi fi
fi