diff --git a/elisp/eshell-starship.el b/elisp/eshell-starship.el index ebe0864..fa7b442 100644 --- a/elisp/eshell-starship.el +++ b/elisp/eshell-starship.el @@ -269,6 +269,14 @@ The number is rounded to PLACES before being rendered." (format "%dm" mins)) (format (format "%%.%dfs" places) secs)))) +(defun eshell-starship-clear-cache-for (modules) + "Clear the cache for each of MODULES. +MODULES can also be a single module." + (dolist (module (ensure-list modules)) + (when (symbolp module) (setq module (symbol-name module))) + (when-let ((cur-entry (gethash module eshell-starship--module-cache))) + (setf (car cur-entry) nil)))) + ;;; CWD Module (defun eshell-starship--replace-home-with-tilda (path) @@ -623,8 +631,36 @@ For example, a revert. If there is no current operation, return nil." "v" (match-string 0)) :doc "Your Zig version.") +(defun eshell-starship--current-venv () + "Return the name of the prompt string for the current venv. +This requires pyvenv.el to work. Without it, return nil." + (and (bound-and-true-p pyvenv-virtual-env-name) + (format " (%s)" pyvenv-virtual-env-name))) + +(defun eshell-starship--python-status () + "Return the prompt string for the python module." + (when-let + ((python-exec (or (bound-and-true-p python-interpreter) "python")) + (output (process-lines-ignore-status python-exec "--version")) + ((string-match "^Python \\([0-9.]+\\)" (car output)))) + (concat "v" (match-string 1 (car output)) (eshell-starship--current-venv)))) + +(defvar-local eshell-starship--python-last-pyvenv nil + "The previous `pyvenv-virtual-env' value. +This does not mean anything if pyvenv.el is not installed.") + +(defun eshell-starship--python-postcmd-action () + "The postcmd action for the python module." + (when (and (boundp 'pyvenv-virtual-env) + (not (equal eshell-starship--python-last-pyvenv + pyvenv-virtual-env))) + (setq eshell-starship--python-last-pyvenv pyvenv-virtual-env) + (eshell-starship-clear-cache-for 'python))) + (eshell-starship-defmodule python :extensions '("py" "ipynb") + :predicate (lambda () + (bound-and-true-p pyvenv-virtual-env)) :files '(".python-version" "Pipfile" "__init__.py" "pyproject.toml" "requirements.txt" "setup.py" "tox.ini" "pixi.toml") :prefix "via " @@ -632,10 +668,8 @@ For example, a revert. If there is no current operation, return nil." :color "#CECB00" :allow-remote nil :reload-on 'cwd - :action (eshell-starship-find-version-function - ("python" "--version") - "^Python \\([0-9.]+\\)" - "v" (match-string 1)) + :action #'eshell-starship--python-status + :postcmd-action #'eshell-starship--python-postcmd-action :doc "Your current system-wide Python version.") (eshell-starship-defmodule php @@ -698,11 +732,9 @@ If any of flags is t, clear all caches." (cl-loop with force-clear = (member t flags) for module being the hash-values of eshell-starship-modules do (with-slots (name reload-on) module - (when-let (((or force-clear - (cl-intersection (ensure-list reload-on) flags))) - (cur-entry (gethash name - eshell-starship--module-cache))) - (setf (car cur-entry) nil))))) + (when (or force-clear + (cl-intersection (ensure-list reload-on) flags)) + (eshell-starship-clear-cache-for name))))) (defun eshell-starship--cwd-clear-caches () "Clear caches that should be cleared on cwd for eshell-starship." diff --git a/init.el b/init.el index 20718f3..0b33c22 100644 --- a/init.el +++ b/init.el @@ -1657,6 +1657,8 @@ otherwise, call `bibtex-find-text'." (use-package python-ts-mode :ensure nil :hook (python-ts-mode . trusted-files-eglot-ensure-if-safe)) +;; python virtual environments +(use-package pyvenv) ;; java-ts-mode (use-package java-ts-mode