diff --git a/README.md b/README.md index b250172..d8bc4e2 100644 --- a/README.md +++ b/README.md @@ -4,18 +4,32 @@ ## Installation -Use [zplug](https://github.com/zplug/zplug) to install the plugin: +With [zplug](https://github.com/zplug/zplug): ```bash zplug "Skylor-Tang/auto-venv", use:auto-venv.zsh, from:github, at:main ``` +With [zpm](https://github.com/zpm-zsh/zpm): + +```bash +zpm load Skylor-Tang/auto-venv +``` + ## Usage The plugin automatically detects if there is a `venv` or `.venv` directory in the current directory or its parent directories, and activates the corresponding Python virtual environment. When you change directories, the plugin will automatically detect and activate the virtual environment. If the current directory does not have a virtual environment, but you are currently in an active virtual environment, the plugin will automatically deactivate the current virtual environment. +## Silent Mode + +By default, the plugin will print a message when it activates or deactivates a virtual environment. If you want to suppress these messages, you can set the `AUTO_VENV_SILENT` environment variable to `true`: + +```bash +export AUTO_VENV_SILENT=true +``` + ## How it Works The plugin uses the Zsh `chpwd` hook function to automatically call the `auto_venv` function when you change directories. This function recursively searches the parent directories until it finds a virtual environment directory or reaches the root directory or the user's home directory. diff --git a/auto-venv.zsh b/auto-venv.zsh index eddf434..c465100 100644 --- a/auto-venv.zsh +++ b/auto-venv.zsh @@ -10,7 +10,7 @@ auto_venv() { # If a virtual environment directory is found, activate it if [ -d "${local_venv_dir}" ]; then source "${local_venv_dir}/bin/activate" - [ -t 1 ] && echo -e "\e[32mActivated Python virtual environment: ${local_venv_dir}\e[0m" + [ -t 1 ] && [ -z "$AUTO_VENV_SILENT" ] && echo -e "\e[32mActivated Python virtual environment: ${local_venv_dir}\e[0m" return fi done @@ -23,7 +23,7 @@ auto_venv() { # deactivate the current virtual environment if it exists if [[ "${current_dir}" == "/" || "${current_dir}" == "$HOME" ]]; then if [[ -n "$VIRTUAL_ENV" ]]; then - [ -t 1 ] && echo -e "\e[31mDeactivated Python virtual environment: $VIRTUAL_ENV\e[0m" + [ -t 1 ] && [ -z "$AUTO_VENV_SILENT" ] && echo -e "\e[31mDeactivated Python virtual environment: $VIRTUAL_ENV\e[0m" deactivate fi return