Created
February 1, 2020 08:31
-
-
Save laughk/50d7a5323831cb170be46a0c99e623c5 to your computer and use it in GitHub Desktop.
zsh 向けの venv wrapper 的なやつ
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
#!/bin/zsh | |
VENVFZF_ROOT="${HOME}/.venvs" | |
VENVFZF_VENV_OPTIONS="" | |
VENVFZF_FZF="fzf" | |
if [[ -n ${TMUX} ]] && [[ $(which fzf-tmux) ]]; then | |
VENVFZF_FZF="fzf-tmux" | |
fi | |
# Main functions | |
# -------------------------------------------------------- | |
function __venv_fzf_active_fzf() { | |
local _fzf_prompt="VENV> " | |
v=$( __venv_fzf_list_venv | ${VENVFZF_FZF} --prompt="${_fzf_prompt}" | cut -d" " -f1) && __venv_fzf_active $v | |
} | |
function __venv_fzf_mk_venv() { | |
local _fzf_prompt="PYTHON USED FOR MAKE VENV> " | |
local venv_name=${1:-"$(pwd | sed 's;/;_;g')"} | |
read _venv_name\?"Input a venv name you wants [default: ${venv_name}]> " | |
if [[ ! -z ${_venv_name} ]] ; then | |
venv_name=${_venv_name} | |
fi | |
local expected_venv=${VENVFZF_ROOT}/${venv_name} | |
if [[ -d ${expected_venv} ]] ; then | |
echo "${expected_venv} is Already exist !!" | |
return | |
fi | |
local python_interpreters=(${(@f)$(__venv_fzf_list_pythons)}) | |
local _i=1 | |
for p in ${python_interpreters}; do | |
echo "${_i}. ${p}" | |
_i=$((_i+1)) | |
done | |
read _py_number\?"Choise a number you wants use python > " | |
local _py=${python_interpreters[$_py_number]} | |
echo "$_py -m venv ${expected_venv}" | |
$_py -m venv ${expected_venv} && \ | |
__venv_fzf_active ${expected_venv} | |
} | |
# Utility functions | |
# -------------------------------------------------------- | |
function __venv_fzf_list_venv(){ | |
local venvs=(${(@f)$(find ${VENVFZF_ROOT} -maxdepth 1 -mindepth 1 -type d)}) | |
for venv in ${venvs} ; do | |
echo "${venv} ($(__venv_fzf_show_venv_python_version ${venv}))" | |
done | |
} | |
function __venv_fzf_show_venv_python_version() { | |
local _venv=$1 | |
cat ${_venv}/pyvenv.cfg \ | |
| tr '\n' '\,' \ | |
| sed 's/ //g' \ | |
| cut -d, -f3 | |
echo | |
} | |
function __venv_fzf_active() { | |
local venv_path=$1 | |
source ${venv_path}/bin/activate | |
} | |
function __venv_fzf_list_pythons() { | |
find $(echo $PATH | sed 's/:/ /g') -maxdepth 1 -mindepth 1 2> /dev/null \ | |
| grep -v ${VENVFZF_FZF} \ | |
| sort \ | |
| uniq \ | |
| grep -P 'python(3(\.?[0-9])?)?$' | |
} | |
# -------------------------------------------- | |
# zle -N __venv_fzf_mk_venv_fzf | |
zle -N __venv_fzf_active_fzf | |
alias mkvenv=__venv_fzf_mk_venv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment