From 0f9543225efcebd5a3bce5481779b9c73211d23a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20La=C3=ADn?= Date: Thu, 4 Apr 2024 10:20:12 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(fish):=20auto=20activate=20pyt?= =?UTF-8?q?hon=20env?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/fish/conf.d/venv.fish | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .config/fish/conf.d/venv.fish diff --git a/.config/fish/conf.d/venv.fish b/.config/fish/conf.d/venv.fish new file mode 100644 index 00000000..4e73535e --- /dev/null +++ b/.config/fish/conf.d/venv.fish @@ -0,0 +1,30 @@ +# Based on https://gist.github.com/bastibe/c0950e463ffdfdfada7adf149ae77c6f +# Changes: +# * Instead of overriding cd, we detect directory change. This allows the script to work +# for other means of cd, such as z. +# * Update syntax to work with new versions of fish. +# * Handle virtualenvs that are not located in the root of a git directory. + +function __auto_source_venv --on-variable PWD --description "Activate/Deactivate virtualenv on directory change" + status --is-command-substitution; and return + + # Check if we are inside a git directory + if git rev-parse --show-toplevel &>/dev/null + set gitdir (realpath (git rev-parse --show-toplevel)) + set cwd (pwd -P) + # While we are still inside the git directory, find the closest + # virtualenv starting from the current directory. + while string match "$gitdir*" "$cwd" &>/dev/null + if test -e "$cwd/.venv/bin/activate.fish" + source "$cwd/.venv/bin/activate.fish" &>/dev/null + return + else + set cwd (path dirname "$cwd") + end + end + end + # If virtualenv activated but we are not in a git directory, deactivate. + if test -n "$VIRTUAL_ENV" + deactivate + end +end