Pyvenv support

This commit is contained in:
Alexander Rosenberg 2025-01-27 03:31:38 -08:00
parent 4dfd389998
commit b33937f50b
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730
2 changed files with 43 additions and 9 deletions

View File

@ -269,6 +269,14 @@ The number is rounded to PLACES before being rendered."
(format "%dm" mins)) (format "%dm" mins))
(format (format "%%.%dfs" places) secs)))) (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 ;;; CWD Module
(defun eshell-starship--replace-home-with-tilda (path) (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)) "v" (match-string 0))
:doc "Your Zig version.") :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 (eshell-starship-defmodule python
:extensions '("py" "ipynb") :extensions '("py" "ipynb")
:predicate (lambda ()
(bound-and-true-p pyvenv-virtual-env))
:files '(".python-version" "Pipfile" "__init__.py" "pyproject.toml" :files '(".python-version" "Pipfile" "__init__.py" "pyproject.toml"
"requirements.txt" "setup.py" "tox.ini" "pixi.toml") "requirements.txt" "setup.py" "tox.ini" "pixi.toml")
:prefix "via " :prefix "via "
@ -632,10 +668,8 @@ For example, a revert. If there is no current operation, return nil."
:color "#CECB00" :color "#CECB00"
:allow-remote nil :allow-remote nil
:reload-on 'cwd :reload-on 'cwd
:action (eshell-starship-find-version-function :action #'eshell-starship--python-status
("python" "--version") :postcmd-action #'eshell-starship--python-postcmd-action
"^Python \\([0-9.]+\\)"
"v" (match-string 1))
:doc "Your current system-wide Python version.") :doc "Your current system-wide Python version.")
(eshell-starship-defmodule php (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) (cl-loop with force-clear = (member t flags)
for module being the hash-values of eshell-starship-modules for module being the hash-values of eshell-starship-modules
do (with-slots (name reload-on) module do (with-slots (name reload-on) module
(when-let (((or force-clear (when (or force-clear
(cl-intersection (ensure-list reload-on) flags))) (cl-intersection (ensure-list reload-on) flags))
(cur-entry (gethash name (eshell-starship-clear-cache-for name)))))
eshell-starship--module-cache)))
(setf (car cur-entry) nil)))))
(defun eshell-starship--cwd-clear-caches () (defun eshell-starship--cwd-clear-caches ()
"Clear caches that should be cleared on cwd for eshell-starship." "Clear caches that should be cleared on cwd for eshell-starship."

View File

@ -1657,6 +1657,8 @@ otherwise, call `bibtex-find-text'."
(use-package python-ts-mode (use-package python-ts-mode
:ensure nil :ensure nil
:hook (python-ts-mode . trusted-files-eglot-ensure-if-safe)) :hook (python-ts-mode . trusted-files-eglot-ensure-if-safe))
;; python virtual environments
(use-package pyvenv)
;; java-ts-mode ;; java-ts-mode
(use-package java-ts-mode (use-package java-ts-mode