⬆️ Update viper-env to version 1.1.1.

This commit is contained in:
DanielAtKrypton 2025-08-06 19:09:03 +01:00
commit e9b268617e
No known key found for this signature in database
GPG key ID: F687BC913B85A4F9

View file

@ -20,7 +20,7 @@ export COLOR_NC=$'\033[0m' # No Color
# Configuration file for persistent settings. # Configuration file for persistent settings.
_VIPER_ENV_CONFIG_FILE="$HOME/.viper-env.conf" _VIPER_ENV_CONFIG_FILE="$HOME/.viper-env.conf"
_VIPER_ENV_VERSION="v1.1.0" _VIPER_ENV_VERSION="v1.1.1"
# This variable will hold the path of the environment managed by this script. # This variable will hold the path of the environment managed by this script.
# This prevents us from deactivating environments managed by other tools (e.g., conda, poetry). # This prevents us from deactivating environments managed by other tools (e.g., conda, poetry).
@ -38,27 +38,25 @@ __viper-env_discover_venv() {
# Use `emulate` to ensure a predictable environment and `dotglob` to find hidden venvs. # Use `emulate` to ensure a predictable environment and `dotglob` to find hidden venvs.
emulate -L zsh emulate -L zsh
setopt local_options dotglob setopt local_options dotglob
local search_dir="$PWD" local search_dir="${PWD%/}" # Removes trailing slash, if any
# Loop upwards from the current directory to the root. # Loop upwards from the current directory to the root.
while :; do while :; do
# Mode 1: Semi-automatic via `.viper-env` file # Mode 1: Check for .viper-env file
# This allows using virtual environments located outside the project directory. if [[ -s "$search_dir/.viper-env" ]]; then
if [[ -f "$search_dir/.viper-env" ]]; then
local external_venv_path local external_venv_path
# Read the path from the file, handling potential trailing newlines. # Read the path from the file, handling potential trailing newlines.
# Using 'read -r' is more robust for reading a single line from a file. # Using 'read -r' is more robust for reading a single line from a file.
read -r external_venv_path < "$search_dir/.viper-env" read -r external_venv_path < "$search_dir/.viper-env"
# Remove trailing carriage return if file has Windows line endings. # Remove trailing carriage return if file has Windows line endings.
external_venv_path=${external_venv_path%$'\r'}
# Perform tilde expansion on the path to support paths like ~/.virtualenvs/... # Perform tilde expansion on the path to support paths like ~/.virtualenvs/...
external_venv_path=${(~)external_venv_path} external_venv_path=${(~)external_venv_path%$'\r'}
# The path in the file can be either the venv root or the full path to the activate script. # The path in the file can be either the venv root or the full path to the activate script.
# This logic handles both cases robustly. # This logic handles both cases robustly.
local venv_root_path="" local venv_root_path=""
if [[ "$external_venv_path" == */bin/activate && -f "$external_venv_path" ]]; then if [[ "$external_venv_path" == */bin/activate && -f "$external_venv_path" ]]; then
# Case 1: Path is the full path to the activate script. # Case 1: Path is the full path to the activate script.
venv_root_path="$(dirname "$(dirname "$external_venv_path")")" venv_root_path="${external_venv_path%/*/*}"
elif [[ -f "$external_venv_path/bin/activate" ]]; then elif [[ -f "$external_venv_path/bin/activate" ]]; then
# Case 2: Path is the root of the virtual environment. # Case 2: Path is the root of the virtual environment.
venv_root_path="$external_venv_path" venv_root_path="$external_venv_path"
@ -78,7 +76,7 @@ __viper-env_discover_venv() {
fi fi
fi fi
# Pattern 1: Check if the search_dir itself is a venv root. # Pattern 1: Check if current dir is a venv root
if [[ -f "$search_dir/bin/activate" ]]; then if [[ -f "$search_dir/bin/activate" ]]; then
echo "$search_dir" echo "$search_dir"
return 0 return 0
@ -88,13 +86,13 @@ __viper-env_discover_venv() {
# The `(N[1])` glob qualifier finds the first match and prevents errors if none exist. # The `(N[1])` glob qualifier finds the first match and prevents errors if none exist.
local activate_script=($search_dir/*/bin/activate(N[1])) local activate_script=($search_dir/*/bin/activate(N[1]))
if [[ -n "$activate_script" ]]; then if [[ -n "$activate_script" ]]; then
echo "$(dirname $(dirname "$activate_script"))" echo "${activate_script%/*/*}"
return 0 return 0
fi fi
# Exit if we've reached the root directory, otherwise, go up one level. # Exit if root, otherwise go up one level
[[ "$search_dir" == "/" ]] && break [[ -z "$search_dir" ]] && break
search_dir=$(dirname "$search_dir") search_dir="${search_dir%/*}" # Removes the last path component
done done
return 1 return 1
} }