From a20bffdf67f00910fbe31dd3591c93ce9ac0dee6 Mon Sep 17 00:00:00 2001 From: bretello Date: Sat, 11 Nov 2023 18:54:40 +0100 Subject: [PATCH] zsh: python: cleanup mkvenv --- functions/python.zsh | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/functions/python.zsh b/functions/python.zsh index a332700..13c2e94 100644 --- a/functions/python.zsh +++ b/functions/python.zsh @@ -8,28 +8,35 @@ mkvenv(){ esac done - echo "force=$force_create" - return shift $(($OPTIND-1)) if [[ -n $1 ]]; then venv_name=$1 shift + else + venv_name=.venv fi # any other arguments are passed on to the `venv` module as flags/arguments - echo ${venv_name} + + if [[ -e ${venv_name} ]]; then if [[ -n ${force_create} ]]; then if [[ -n $VIRTUAL_ENV ]]; then # deactivate venv if enabled - deactivate && info "Deactivated existing virtualenv" || red "Could not run deactivate"; + deactivate && warning "Deactivated existing virtualenv" || red "Could not run deactivate"; fi - rm -rf ${venv_name} && info "Deleted existing virtualenv" + rm -rf ${venv_name} && warning "Deleted existing virtualenv." else - echo "$(color $RED Error:) $(color $BOLD .venv) already exists. Run with -f to recreate it." >&2 + error "$(color $BOLD .venv) already exists. Run with -f to recreate it." >&2 return 1 fi fi - info "Creating virtualenv ${venv_name}... " && python -m virtualenv $@ ${venv_name}|| echo -e "Failed to create virtualenv. Is virtualenv installed? Try:\n $ pip install virtualenv" + info "Creating virtualenv ${venv_name}... " + + if ! python -m virtualenv $@ ${venv_name} ; then + error "Failed to create virtualenv. Is virtualenv installed? Try:\n $ pip install virtualenv" + return 1 + fi + if [[ -z ${source_venv} ]]; then echo -n " done. Enable? [Y/n] " read source_venv @@ -38,7 +45,8 @@ mkvenv(){ fi if [[ $source_venv != "n" ]]; then - source ${venv_name}/bin/activate && echo -e " → $(color $BOLD Enabled!) 🐍 $(color $BOLD$PURPLE $(python --version | cut -d " " -f2 )) ($(color $BOLD$GREEN $(pip --version | cut -d " " -f -2)))" + source ${venv_name}/bin/activate && \ + echo -e " → $(color $BOLD Enabled!) 🐍 $(color $BOLD$PURPLE $(python --version | cut -d " " -f2 )) ($(color $BOLD$GREEN $(pip --version | cut -d " " -f -2)))" fi }