Add pyenv support

This commit is contained in:
Alexander Rosenberg 2025-02-09 14:05:12 -08:00
parent 655eb827e1
commit d65948ca41
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730
2 changed files with 28 additions and 9 deletions

View File

@ -633,9 +633,13 @@ For example, a revert. If there is no current operation, return nil."
(defun eshell-starship--current-venv () (defun eshell-starship--current-venv ()
"Return the name of the prompt string for the current venv. "Return the name of the prompt string for the current venv.
This requires pyvenv.el to work. Without it, return nil." This requires pyvenv.el or pyenv-mode to work."
(and (bound-and-true-p pyvenv-virtual-env-name) (concat
(format " (%s)" pyvenv-virtual-env-name))) (and (bound-and-true-p pyvenv-virtual-env-name)
(format " (%s)" pyvenv-virtual-env-name))
(and (fboundp 'pyenv-mode-version)
(when-let ((ver (pyenv-mode-version)))
(format " (%s)" ver)))))
(defun eshell-starship--python-status () (defun eshell-starship--python-status ()
"Return the prompt string for the python module." "Return the prompt string for the python module."
@ -649,18 +653,32 @@ This requires pyvenv.el to work. Without it, return nil."
"The previous `pyvenv-virtual-env' value. "The previous `pyvenv-virtual-env' value.
This does not mean anything if pyvenv.el is not installed.") This does not mean anything if pyvenv.el is not installed.")
(defvar-local eshell-starship--python-last-pyenv nil
"The return value of the last `pyenv-mode-version'.
This does not mean anything if pyenv-mode is not installed.")
(defun eshell-starship--python-postcmd-action () (defun eshell-starship--python-postcmd-action ()
"The postcmd action for the python module." "The postcmd action for the python module."
(when (and (boundp 'pyvenv-virtual-env) (let ((need-clear nil))
(not (equal eshell-starship--python-last-pyvenv (when (and (boundp 'pyvenv-virtual-env)
pyvenv-virtual-env))) (not (equal eshell-starship--python-last-pyvenv
(setq eshell-starship--python-last-pyvenv pyvenv-virtual-env) pyvenv-virtual-env)))
(eshell-starship-clear-cache-for 'python))) (setq eshell-starship--python-last-pyvenv pyvenv-virtual-env
need-clear t))
(when (fboundp 'pyenv-mode-version)
(let ((cur-ver (pyenv-mode-version)))
(when (not (equal eshell-starship--python-last-pyenv cur-ver))
(setq eshell-starship--python-last-pyenv cur-ver
need-clear t))))
(when need-clear
(eshell-starship-clear-cache-for 'python))))
(eshell-starship-defmodule python (eshell-starship-defmodule python
:extensions '("py" "ipynb") :extensions '("py" "ipynb")
:predicate (lambda () :predicate (lambda ()
(bound-and-true-p pyvenv-virtual-env)) (or (bound-and-true-p pyvenv-virtual-env)
(and (fboundp 'pyenv-mode-version)
(pyenv-mode-version))))
: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 "

View File

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