From a89ce6da159d4bf3415e975a7f45936d207c927d Mon Sep 17 00:00:00 2001 From: Daniel Kaminski de Souza Date: Mon, 1 Feb 2021 22:05:37 +0000 Subject: [PATCH] :heavy_plus_sign: Add viper-env plugin. --- plugins/viper-env/README.md | 65 ++++++++++ .../viper-env/make_animation/assets/final.svg | 1 + plugins/viper-env/viper-env.plugin.zsh | 113 ++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 plugins/viper-env/README.md create mode 100644 plugins/viper-env/make_animation/assets/final.svg create mode 100644 plugins/viper-env/viper-env.plugin.zsh diff --git a/plugins/viper-env/README.md b/plugins/viper-env/README.md new file mode 100644 index 000000000..6b2db1f7c --- /dev/null +++ b/plugins/viper-env/README.md @@ -0,0 +1,65 @@ +# Viper Env Z-Shel plugin + +Automatically activates and deactivates python virtualenv upon cd in and out. + +## Inspiration + +Based on [blueray](https://stackoverflow.com/users/1772898/blueray)'s [answer](https://stackoverflow.com/a/63955939/11685534), I decided to go one step further and implement it as a Z-Shell plugin. + +## Usage + + +![Alt text](./make_animation/assets/final.svg) + +## Example +```zsh +> viper-env help + +Description: + Automatically activates and deactivates python virtualenv upon cd in and out. + +Dependencies: + - zsh + - python + +Example usage: + # Create virtual environment + python -m venv .venv + # Save current dir + current_dir=$(basename $PWD) + # Exit current directory + cd .. + # Reenter it + cd $current_dir +``` + +## Instalation + +### Oh my Zsh + +Git clone this repository to the Oh my Zsh custom plugins folder. + +Add plugin to plugins directive in `~/.zshrc` +```zsh +plugins=( + # put local oh-my-zsh plugins here + viper-env +) + +source $HOME/.oh-my-zsh/oh-my-zsh.sh +``` + +### Antigen +It is recommended to use a `.antigenrc` file. Then add the following to it: + +```zsh +antigen bundle DanielAtKrypton/viper-env + +# Apply changes +antigen apply +``` + +Make sure in your `.zshrc` Antigen is loading the `.antigenrc` file as follows: +```zsh +antigen init ~/.antigenrc +``` \ No newline at end of file diff --git a/plugins/viper-env/make_animation/assets/final.svg b/plugins/viper-env/make_animation/assets/final.svg new file mode 100644 index 000000000..d0920e523 --- /dev/null +++ b/plugins/viper-env/make_animation/assets/final.svg @@ -0,0 +1 @@ +viper-env is cool╭─░▒▓~/Workspaces/Python▓▒░─────────────────────────────────────────────────────────────────────────░▒▓at04:12:52 ▓▒░─╮╰──╯╭─░▒▓~/Workspaces/Python▓▒░─────────────────────────────────────────────────────────────────────────░▒▓╰─mkdir─╯╰─mkdirnew_project─╯mkdirnew_project╭─░▒▓~/Workspaces/Python▓▒░─────────────────────────────────────────────────────────────────────────░▒▓at04:13:01 ▓▒░─╮╰─c─╯╰─cd..─╯╰─cdnew_project─╯╰─cdnew_project─╯╰─cdnew_project─╯cdnew_project╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────────────────────░▒▓at04:13:07 ▓▒░─╮╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯╰─python-mvenv.venv─╯python-mvenv.venvActivatingvirtualenvironmentnew_project/.venv╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────░▒▓╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────░▒▓new_project at04:13:18 ▓▒░─╮╰─c─╯╰─cdnew_project─╯╰─cd..─╯╰─cd..─╯cd..Deactivatingvirtualenvironmentnew_project/.venv╭─░▒▓~/Workspaces/Python▓▒░─────────────────────────────────────────────────────────────────────────░▒▓at04:13:30 ▓▒░─╮╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────░▒▓new_project at04:13:39 ▓▒░─╮╰─piplist─╯╰─piplist─╯╰─piplist─╯╰─piplist─╯╰─piplist─╯╰─piplist─╯piplistPackageVersion-----------------pip20.2.3setuptools49.2.1WARNING:Youareusingpipversion20.2.3;however,version21.0.1isavailable.Youshouldconsiderupgradingviathe'/home/daniel/Workspaces/Python/new_project/.venv/bin/python-mpipinstall--upgradepip'command.╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────░▒▓new_project at04:13:43 ▓▒░─╮╰─p─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯pipinstall--upgradepipCollectingpipUsingcachedpip-21.0.1-py3-none-any.whl(1.5MB)Installingcollectedpackages:pipAttemptinguninstall:pipFoundexistinginstallation:pip20.2.3Uninstallingpip-20.2.3:Successfullyuninstalledpip-20.2.3Successfullyinstalledpip-21.0.1╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────░▒▓new_project at04:13:55 ▓▒░─╮╰─pipinstall--upgradepip─╯pip21.0.1╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────░▒▓new_project at04:14:00 ▓▒░─╮╭─░▒▓~/Workspaces/Python▓▒░─────────────────────────────────────────────────────────────────────────░▒▓at04:14:07 ▓▒░─╮╰─mkdirnew_project─╯╰─mkdiranother_new_project─╯mkdiranother_new_project╭─░▒▓~/Workspaces/Python▓▒░─────────────────────────────────────────────────────────────────────────░▒▓at04:14:18 ▓▒░─╮╰─cdanother_new_project─╯cdanother_new_project╭─░▒▓~/Workspaces/Python/another_new_project▓▒░─────────────────────────────────────────────────────░▒▓at04:14:26 ▓▒░─╮Activatingvirtualenvironmentanother_new_project/.venv╭─░▒▓~/Workspaces/Python/another_new_project▓▒░─────────────────────────────░▒▓another_new_project at04:14:37 ▓▒░─╮╰─z/home/daniel/Workspaces/Python/─╯╰─znew_project─╯znew_projectDeactivatingvirtualenvironment../another_new_project/.venv╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────░▒▓new_project at04:14:46 ▓▒░─╮╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────░▒▓new_project at04:14:51 ▓▒░─╮╭─░▒▓~/Workspaces/Python▓▒░────────────────────────────────────────────────────────────────────────╰─mvrecording.cast~/.oh-my-zsh/custom/plugins/viper-env/make_animation/assets─╯╰─mk─╯╰─mkd─╯╰─mkdi─╯╰─mkdirn─╯╰─mkdirne─╯╰─mkdirnew─╯╰─mkdirnew_─╯╰─mkdirnew_p─╯╰─mkdirnew_pr─╯╰─mkdirnew_pro─╯╰─mkdirnew_proj─╯╰─mkdirnew_proje─╯╰─mkdirnew_projec─╯╰─cdnew_project─╯╰─cdnew_project─╯╰─cdnew_project─╯╰─cdnew_project─╯╰─cdnew_project─╯╰─cdnew_project─╯╰─cdnew_project─╯╰─cdnew_project─╯╭─░▒▓~/Workspaces/Python/new_project▓▒░─────────────────────────────────────────────────────────────░▒▓╰─python-mvenv.venv─╯╰─pytthon-mvenv.venv─╯╰─cdnew_project/─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─pipinstall--upgradepip─╯╰─m─╯╰─mkdirnew_project─╯╰─mkddirnew_project─╯╰─mkdirnew_project─╯╰─mkdira─╯╰─mkdiran─╯╰─mkdirano─╯╰─mkdiranot─╯╰─mkdiranoth─╯╰─mkdiranothe─╯╰─mkdiranother─╯╰─mkdiranother_─╯╰─mkdiranother_n─╯╰─mkdiranother_ne─╯╰─mkdiranother_new─╯╰─mkdiranother_new_─╯╰─mkdiranother_new_p─╯╰─mkdiranother_new_pr─╯╰─mkdiranother_new_pro─╯╰─mkdiranother_new_proj─╯╰─mkdiranother_new_proje─╯╰─mkdiranother_new_projec─╯╰─cda─╯╰─cdan─╯╰─cdano─╯╰─cdanot─╯╰─cdanoth─╯╰─cdanothe─╯╰─cdanother─╯╰─cdanother_─╯╰─cdanother_n─╯╰─cdanother_ne─╯╰─cdanother_new─╯╰─cdanother_new_─╯╰─cdanother_new_p─╯╰─cdanother_new_pr─╯╰─cdanother_new_pro─╯╰─cdanother_new_proj─╯╰─cdanother_new_proje─╯╰─cdanother_new_projec─╯╭─░▒▓~/Workspaces/Python/another_new_project▓▒░─────────────────────────────────────────────────────░▒▓╰─piplist─╯╰─pyiplist─╯╰─python-mvenv.venv─╯╭─░▒▓~/Workspaces/Python/another_new_project▓▒░─────────────────────────────░▒▓╰─znew_project─╯╰─znew_project─╯╰─znew_project─╯╰─znew_project─╯╰─znew_project─╯╰─znew_project─╯╰─znew_project─╯╰─znew_project─╯╰─znew_project─╯╰─znew_project─╯ \ No newline at end of file diff --git a/plugins/viper-env/viper-env.plugin.zsh b/plugins/viper-env/viper-env.plugin.zsh new file mode 100644 index 000000000..31fb2a336 --- /dev/null +++ b/plugins/viper-env/viper-env.plugin.zsh @@ -0,0 +1,113 @@ +# Colors based on: +# https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#16-colors +export COLOR_BLACK='\033[0;30m' +export COLOR_BRIGHT_BLACK='\u001b[30;1m' +export COLOR_RED='\033[0;31m' +export COLOR_BRIGHT_RED='\u001b[31;1m' +export COLOR_GREEN='\033[0;32m' +export COLOR_BRIGHT_GREEN='\u001b[32;1m' +export COLOR_YELLOW='\033[0;33m' +export COLOR_BRIGHT_YELLOW='\u001b[33;1m' +export COLOR_BLUE='\033[0;34m' +export COLOR_BRIGHT_BLUE='\u001b[34;1m' +export COLOR_VIOLET='\033[0;35m' +export COLOR_BRIGHT_VIOLET='\u001b[35;1m' +export COLOR_CYAN='\033[0;36m' +export COLOR_BRIGHT_CYAN='\u001b[36;1m' +export COLOR_WHITE='\033[0;37m' +export COLOR_NC='\033[0m' # No Color + +function activate_env_execution() { + local virtualenv_directory=$1 + local d=$2 + local relative_env_path=$3 + local full_virtualenv_directory=$d/$virtualenv_directory + + echo Activating virtual environment ${COLOR_BRIGHT_VIOLET}$relative_env_path${COLOR_NC} + source $full_virtualenv_directory/bin/activate +} + +function get_env_path(){ + echo "$(basename "$1")/$2" +} + +function get_envs(){ + local output="$(ls (.*|*)/bin/pip(.x))" &> /dev/null + local candidate_envs_found + if [ $? -eq 0 ]; then + candidate_envs_found=($output) + else + candidate_envs_found=() + fi + envs_found=() + for env in "${candidate_envs_found[@]}" + do + local env_name="$(dirname $env)" + ls $env_name/activate &> /dev/null + if [ $? -eq 0 ]; then + envs_found+=("$(dirname $env_name)") + fi + done +} + +function activate_env(){ + local current_dir=$1 + get_envs + # echo "Envs found: $envs_found" + if [ ${#envs_found[@]} -gt 0 ]; then + # Use first found only! + local env_name="${envs_found[1]}" + # local env_name="$(dirname "$(dirname $env_to_use)")" + local relative_activating_env_path="$(get_env_path $current_dir $env_name)" + activate_env_execution $env_name $current_dir $relative_activating_env_path + fi +} + +function automatically_activate_python_env() { + local current_dir="$PWD" + local env_var="$VIRTUAL_ENV" + if [[ -z $env_var ]] ; then + activate_env $current_dir + else + parentdir="$(dirname $env_var)" + if [[ $current_dir/ != $parentdir/* ]] ; then + local deactivating_relative_env_path="$(realpath --relative-to=$current_dir $env_var)" + echo Deactivating virtual environment ${COLOR_BRIGHT_VIOLET}$deactivating_relative_env_path${COLOR_NC} + deactivate + activate_env $current_dir + fi + fi +} +autoload -Uz add-zsh-hook +add-zsh-hook precmd automatically_activate_python_env + +__viper-env_help () { + printf "Description: + ${COLOR_BRIGHT_BLACK}Automatically activates and deactivates python virtualenv upon cd in and out.${COLOR_NC} + +" + printf "Dependencies: + ${COLOR_BRIGHT_BLACK}- zsh + - python${COLOR_NC} + +" + printf "Example usage: + ${COLOR_BRIGHT_BLACK}# Create virtual environment${COLOR_NC} + ${COLOR_GREEN}python${COLOR_NC} -m env .env + ${COLOR_BRIGHT_BLACK}# Save current dir${COLOR_NC} + current_dir=${COLOR_VIOLET}\$(${COLOR_GREEN}basename ${COLOR_YELLOW}"${COLOR_NC}\$PWD${COLOR_YELLOW}"${COLOR_VIOLET})${COLOR_NC} + ${COLOR_BRIGHT_BLACK}# Exit current directory${COLOR_NC} + ${COLOR_GREEN}cd${COLOR_NC} .. + ${COLOR_BRIGHT_BLACK}# Reenter it${COLOR_NC} + ${COLOR_GREEN}cd${COLOR_NC} \$current_dir +" +} + +viper-env_runner() { + if [[ $@ == "help" || $@ == "h" ]]; then + __viper-env_help + #elif [[ $@ == "something" || $@ == "alias" ]]; then + fi +} + +alias viper-env='viper-env_runner'