mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-01-23 02:35:38 +01:00
➕ Add viper-env plugin.
This commit is contained in:
parent
f8022980a3
commit
0a19871e6f
8 changed files with 1714 additions and 0 deletions
66
plugins/viper-env/README.md
Normal file
66
plugins/viper-env/README.md
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
# 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
|
||||||
|
<!-- [](https://asciinema.org/a/4iMwcKfBS1dc1EgI1FihrDVxT) -->
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Example
|
||||||
|
```zsh
|
||||||
|
> viper-env help
|
||||||
|
|
||||||
|
Description:
|
||||||
|
Automatically activates and deactivates python virtualenv upon cd in and out.
|
||||||
|
|
||||||
|
Dependencies:
|
||||||
|
- zsh
|
||||||
|
- python
|
||||||
|
- `brew install coreutils` (macOS only)
|
||||||
|
|
||||||
|
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
|
||||||
|
```
|
||||||
2
plugins/viper-env/make_animation/.gitignore
vendored
Normal file
2
plugins/viper-env/make_animation/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
node_modules
|
||||||
|
temporary_folder
|
||||||
BIN
plugins/viper-env/make_animation/assets/FiraCodeRetinaNF.ttf
Normal file
BIN
plugins/viper-env/make_animation/assets/FiraCodeRetinaNF.ttf
Normal file
Binary file not shown.
1
plugins/viper-env/make_animation/assets/final.svg
Normal file
1
plugins/viper-env/make_animation/assets/final.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 1.7 MiB |
33
plugins/viper-env/make_animation/package.json
Normal file
33
plugins/viper-env/make_animation/package.json
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
{
|
||||||
|
"name": "make-animation",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Makes viper-env README.md animation",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"phase_one": "svg-term --cast=1J6AsLVqynFFX5JcTLxFDLYh3 --out assets/usage.svg --profile DanielAtKrypton --window --term konsole",
|
||||||
|
"phase_two": "svgembed -i assets/usage.svg -o assets/final.svg -f assets/FiraCodeRetinaNF.ttf -t \"viper-env is cool\" --hover",
|
||||||
|
"all_phases": "yarn phase_one && yarn phase_two && rm assets/usage.svg",
|
||||||
|
"postinstall": "yarn all_phases"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/DanielAtKrypton/viper-env.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"Virtualenv",
|
||||||
|
"Python",
|
||||||
|
"Direnv",
|
||||||
|
"Zsh"
|
||||||
|
],
|
||||||
|
"author": "Daniel Kaminski de Souza",
|
||||||
|
"license": "ISC",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/DanielAtKrypton/viper-env/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/DanielAtKrypton/viper-env#readme",
|
||||||
|
"devDependencies": {
|
||||||
|
"svg-term-cli": "^2.1.1",
|
||||||
|
"svgembed": "https://github.com/miraclx/svgembed.git"
|
||||||
|
}
|
||||||
|
}
|
||||||
61
plugins/viper-env/make_animation/src/record_session.sh
Normal file
61
plugins/viper-env/make_animation/src/record_session.sh
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
record_session(){
|
||||||
|
sudo apt-get install asciinema
|
||||||
|
|
||||||
|
output_folder=assets
|
||||||
|
filename=asciinema-recording.cast
|
||||||
|
|
||||||
|
asciinema rec $output_folder/$filename
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
echo "Global pip packages..."
|
||||||
|
pip list
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
temp_folder=temporary_folder
|
||||||
|
mkdir $temp_folder
|
||||||
|
cd $temp_folder
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Ask for its help
|
||||||
|
viper-env help
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Create virutal environment
|
||||||
|
python -m venv .venv
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Activate it
|
||||||
|
. .venv/bin/activate
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Create direnv file
|
||||||
|
export VIRTUAL_ENV=venv > .envrc
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Allow it
|
||||||
|
direnv allow .
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Save current dir
|
||||||
|
current_dir=$(basename $PWD)
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Exit current directory
|
||||||
|
cd ..
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# Reenter it
|
||||||
|
cd $current_dir
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
echo "Virtualenv pip packages..."
|
||||||
|
pip list
|
||||||
|
python -m pip install upgrade pip
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
echo "Virtualenv pip packages with updated pip now..."
|
||||||
|
pip list
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
exit
|
||||||
|
}
|
||||||
1437
plugins/viper-env/make_animation/yarn.lock
Normal file
1437
plugins/viper-env/make_animation/yarn.lock
Normal file
File diff suppressed because it is too large
Load diff
114
plugins/viper-env/viper-env.plugin.zsh
Normal file
114
plugins/viper-env/viper-env.plugin.zsh
Normal file
|
|
@ -0,0 +1,114 @@
|
||||||
|
# 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
|
||||||
|
# for MacOS insted of precmd chpwd might work more appropriately.
|
||||||
|
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 new project folder${COLOR_NC}
|
||||||
|
${COLOR_GREEN}mkdir${COLOR_NC} new_project
|
||||||
|
${COLOR_BRIGHT_BLACK}# Create virtual environment${COLOR_NC}
|
||||||
|
${COLOR_GREEN}python${COLOR_NC} -m venv .venv
|
||||||
|
${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} new_project
|
||||||
|
"
|
||||||
|
}
|
||||||
|
|
||||||
|
viper-env_runner() {
|
||||||
|
if [[ $@ == "help" || $@ == "h" ]]; then
|
||||||
|
__viper-env_help
|
||||||
|
#elif [[ $@ == "something" || $@ == "alias" ]]; then
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
alias viper-env='viper-env_runner'
|
||||||
Loading…
Add table
Add a link
Reference in a new issue