Add module caching for eshell-starship

This commit is contained in:
Alexander Rosenberg 2024-02-01 12:47:49 -08:00
parent 5296e74f6a
commit 508ec41fb4
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

View File

@ -12,8 +12,11 @@
This should be an alist of (name function). The macro This should be an alist of (name function). The macro
`eshell-starship-defmodule' can help modify this list.") `eshell-starship-defmodule' can help modify this list.")
(defvar eshell-starship--module-cache (make-hash-table :test 'eq)
"Hash table to hold module cache for eshell-starship.")
(cl-defmacro eshell-starship-defmodule (name &key pred files dirs exts icon color (cl-defmacro eshell-starship-defmodule (name &key pred files dirs exts icon color
allow-remote action) allow-remote action reload-on)
"Define an eshell-starship module called NAME. "Define an eshell-starship module called NAME.
The module will be added to `eshell-starship-modules'. The module will be added to `eshell-starship-modules'.
:PRED - a function that should return t if the module should be run :PRED - a function that should return t if the module should be run
@ -24,10 +27,12 @@ The module will be added to `eshell-starship-modules'.
:ICON - this is the string to print before the modules text :ICON - this is the string to print before the modules text
:COLOR - the color to print the modules text in :COLOR - the color to print the modules text in
:ALLOW-REMOTE - weather to allow the module to run on remote machines :ALLOW-REMOTE - weather to allow the module to run on remote machines
:ACTION - a function that will return the module text, or nil" :ACTION - a function that will return the module text, or nil
:RELOAD-ON - when to re-run the module. Can be one of `'cwd' or `'always'
(same as nil)"
(declare (indent defun)) (declare (indent defun))
`(setf (alist-get ',name eshell-starship-modules) `(setf (alist-get ',name eshell-starship-modules)
,(list 'list pred files dirs exts icon color allow-remote action))) ,(list 'list pred files dirs exts icon color allow-remote action reload-on)))
(cl-defmacro eshell-starship-find-version-function (command pattern (cl-defmacro eshell-starship-find-version-function (command pattern
&rest format) &rest format)
@ -48,6 +53,7 @@ arguments to pass to `concat' to format the output."
:icon "C" :icon "C"
:color "green yellow" :color "green yellow"
:allow-remote nil :allow-remote nil
:reload-on 'cwd
:action (eshell-starship-find-version-function :action (eshell-starship-find-version-function
("cc" "-v") ("cc" "-v")
"^\\([-a-zA-Z]+\\) version \\([0-9]+\\.[0-9]+\\.[0-9]+\\)" "^\\([-a-zA-Z]+\\) version \\([0-9]+\\.[0-9]+\\.[0-9]+\\)"
@ -58,11 +64,19 @@ arguments to pass to `concat' to format the output."
:icon "󰔶" :icon "󰔶"
:color "blue" :color "blue"
:allow-remote nil :allow-remote nil
:reload-on 'cwd
:action (eshell-starship-find-version-function :action (eshell-starship-find-version-function
("cmake" "--version") ("cmake" "--version")
"cmake version \\([0-9]+\\.[0-9]+\\.[0-9]+\\)" "cmake version \\([0-9]+\\.[0-9]+\\.[0-9]+\\)"
"v" (match-string 1))) "v" (match-string 1)))
(defun eshell-starship--cwd-clear-caches ()
"Clear caches that should be cleared on cwd for eshell-starship."
(dolist (module eshell-starship-modules)
(if-let ((reload-on (nth 9 module))
((eq reload-on 'cwd)))
(puthash (car module) nil eshell-starship--module-cache))))
(defun eshell-starship--exts-exist-p (&rest exts) (defun eshell-starship--exts-exist-p (&rest exts)
"Test if any files with EXTS at the end of their name exist in default dir." "Test if any files with EXTS at the end of their name exist in default dir."
(catch 'found (catch 'found
@ -89,16 +103,21 @@ arguments to pass to `concat' to format the output."
(defun eshell-starship--execute-modules () (defun eshell-starship--execute-modules ()
"Execute all the modules in `eshell-starship-modules'." "Execute all the modules in `eshell-starship-modules'."
(cl-loop (cl-loop
for (_ pred files dirs exts icon color allow-remote action) for (name pred files dirs exts icon color allow-remote action reload-on)
in eshell-starship-modules in eshell-starship-modules
when (and (or allow-remote (not (file-remote-p default-directory))) when (and (or allow-remote (not (file-remote-p default-directory)))
(or (and files (apply 'eshell-starship--files-exist-p files)) (or (and files (apply 'eshell-starship--files-exist-p files))
(and dirs (apply' eshell-starship--dirs-exist-p dirs)) (and dirs (apply' eshell-starship--dirs-exist-p dirs))
(and exts (apply' eshell-starship--exts-exist-p exts)) (and exts (apply' eshell-starship--exts-exist-p exts))
(and pred (funcall pred)))) (and pred (funcall pred))))
concat (if-let (result (funcall action)) concat (if-let (cache-val (gethash name eshell-starship--module-cache))
(concat " via " (propertize (concat icon " " result) cache-val
'face `(:foreground ,color)))))) (if-let ((result (funcall action))
(mod-string (concat " via " (propertize (concat icon " " result)
'face `(:foreground ,color)))))
(unless (or (not reload-on) (eq reload-on 'always))
(puthash name mod-string eshell-starship--module-cache))
result))))
(defun eshell-starship--replace-home-with-tilda (path) (defun eshell-starship--replace-home-with-tilda (path)
"If PATH beings with $HOME (the environment variable), replace it with ~." "If PATH beings with $HOME (the environment variable), replace it with ~."
@ -336,6 +355,9 @@ END-TIME is the time when the command finished executing."
(defvar-local ehsell-starship--restore-state nil (defvar-local ehsell-starship--restore-state nil
"State of various variables set by `eshell-starship-prompt-mode'.") "State of various variables set by `eshell-starship-prompt-mode'.")
;;;###autoload
(add-hook 'eshell-directory-change-hook #'eshell-starship--cwd-clear-caches)
;;;###autoload ;;;###autoload
(define-minor-mode eshell-starship-prompt-mode (define-minor-mode eshell-starship-prompt-mode
"Minor mode to make eshell prompts look like starship (https://starship.rs)." "Minor mode to make eshell prompts look like starship (https://starship.rs)."