Skip to main content

Break auto_enable_venv into a separate file

ID
856cc07
date
2023-11-26 02:41:42+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
ba20b05
message
Break auto_enable_venv into a separate file
changed files
2 files, 42 additions, 40 deletions

Changed files

config.fish (3299) → config.fish (2191)

diff --git a/config.fish b/config.fish
index 3aca2b6..c5234a6 100644
--- a/config.fish
+++ b/config.fish
@@ -7,7 +7,6 @@
 set -g -x fish_greeting ''
 
 
-
 # This tells fish to find functions in my "fish_functions" directory.
 #
 # Note that we have to *prepend* the directory in this repo, so we
@@ -19,6 +18,17 @@ set -g -x fish_greeting ''
 set -x fish_function_path ~/repos/scripts/fish_functions $fish_function_path
 
 
+# This tells fish to run a couple of functions as event handlers --
+# that is, to run a function when a variable changes or something similar.
+# These functions can't be autoloaded.
+#
+# See https://fishshell.com/docs/current/language.html#event
+#
+function __auto_enable_venv --on-variable PWD
+    auto_enable_venv
+end
+
+
 # Load macOS-specific utilities
 if [ (uname -s) = Darwin ]
     # Provide a convenient alias for the front URL in both browsers
@@ -30,48 +40,9 @@ if [ (uname -s) = Darwin ]
         _ensure_ssh_key_loaded
         github-clone (furl)
     end
-
-end
-
-
-# Taken from https://gist.github.com/tommyip/cf9099fa6053e30247e5d0318de2fb9e
-#
-# This will automatically enable/disable my virtualenvs when I enter/leave directories.
-#
-# 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)
-        # 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
 
 
-
 # These aliases run scripts in this repo using the virtualenv, rather
 # than running them with system Python.
 #

fish_functions/auto_enable_venv.fish (0) → fish_functions/auto_enable_venv.fish (1133)

diff --git a/fish_functions/auto_enable_venv.fish b/fish_functions/auto_enable_venv.fish
new file mode 100644
index 0000000..adf6c1b
--- /dev/null
+++ b/fish_functions/auto_enable_venv.fish
@@ -0,0 +1,31 @@
+# This tells fish to auto-enable my virtualenvs when I change directories.
+#
+# I have a fairly simple naming convention for my virtualenvs: I put
+# them in the root of the Git repo for each project, and I always
+# name them `~/.venv`.  This means it's pretty easy to work out if
+# a virtualenv exists for the current directory.
+function auto_enable_venv
+    set REPO_ROOT (git rev-parse --show-toplevel 2>/dev/null)
+
+    # If we're not inside a Git repo, there's no virtualenv to activate.
+    #
+    # If we're already in a virtualenv, then we want to deactivate it
+    # (e.g. we've switched from a Git repo to another directory).
+    # Otherwise there's nothing to do.
+    if [ "$REPO_ROOT" = "" ]
+        if test -n "$VIRTUAL_ENV"
+            deactivate
+        end
+    end
+
+    # If we're inside a Git repo, we look for the presence of .venv
+    # in the root.  We may already have the venv activated, in which
+    # case there's nothing to do.
+    if [ "$VIRTUAL_ENV" = "$REPO_ROOT/.venv" ]
+        return
+    end
+
+    if [ test -d "$REPO_ROOT/.venv"]
+        source "$REPO_ROOT/.venv/bin/activate.fish" &>/dev/null
+    end
+end