Add module stuff

This commit is contained in:
Alexander Rosenberg 2024-01-31 00:50:08 -08:00
parent d80493cd63
commit 5296e74f6a
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

View File

@ -7,60 +7,98 @@
(require 'eshell) (require 'eshell)
(require 'cl-lib) (require 'cl-lib)
(defun eshell-starship--file-names-present-p (&rest names) (defvar eshell-starship-modules nil
"Test if any of NAMES (wildcards supported) exist in default directory." "List of modules used by eshell-starship.
(catch 'found This should be an alist of (name function). The macro
(dolist (name names) `eshell-starship-defmodule' can help modify this list.")
(when (seq-filter #'(lambda (name)
(not (string-prefix-p "." name)))
(file-expand-wildcards name))
(throw 'found t)))))
(cl-defmacro eshell-starship-defmodule (name &body body (cl-defmacro eshell-starship-defmodule (name &key pred files dirs exts icon color
&key files allow-remote action)
icon color allow-remote) "Define an eshell-starship module called NAME.
"Define a starship module called NAME. The module will be added to `eshell-starship-modules'.
FILES s used to detect weather to call the module. It is a list of wildcard :PRED - a function that should return t if the module should be run
expressions. SYMBOL and COLOR control the output of the module. BODY should :FILES - a list of file names and wildcard expansions that will be used to
return the string for the module, or nil if it could not be found." determine if the module should be run
:EXTS - save as FILES but mach any file with the given extensions
:DIRS - same as FILES, but for directories
:ICON - this is the string to print before the modules text
:COLOR - the color to print the modules text in
:ALLOW-REMOTE - weather to allow the module to run on remote machines
:ACTION - a function that will return the module text, or nil"
(declare (indent defun)) (declare (indent defun))
`(defun ,(intern (format "eshell-starship--module-%s-func" `(setf (alist-get ',name eshell-starship-modules)
name)) () ,(list 'list pred files dirs exts icon color allow-remote action)))
,(format "Module function for eshell-starship module %s." name)
(or (when (and ,(if allow-remote t (cl-defmacro eshell-starship-find-version-function (command pattern
'(not (file-remote-p default-directory))) &rest format)
(eshell-starship--file-names-present-p ,files)) "Return a lambda that calls COMMAND.
(if-let (res (progn ,body)) COMMAND is in the form of (exec args...). The temp buffer that was used to run
(concat " via " (propertize (concat ,icon " " res) COMMAND will then have `re-search-forward' run with PATTERN and FORMAT should
'face (:foreground arguments to pass to `concat' to format the output."
,color))))) (declare (indent defun))
""))) `(lambda ()
(with-temp-buffer
(when (zerop (process-file ,(car command) nil t nil ,@(cdr command)))
(goto-char (point-min))
(when (re-search-forward ,pattern nil t)
(concat ,@format))))))
(eshell-starship-defmodule cc (eshell-starship-defmodule cc
:files '("*.c" "*.h") :exts '("c" "h")
:icon "C" :icon "C"
:color "green yellow") :color "green yellow"
:allow-remote nil
:action (eshell-starship-find-version-function
("cc" "-v")
"^\\([-a-zA-Z]+\\) version \\([0-9]+\\.[0-9]+\\.[0-9]+\\)"
"v" (match-string 2) "-" (match-string 1)))
(defun eshell-starship--cc-status () (eshell-starship-defmodule cmake
"Return the current CC version if C files exist in `default-directory'. :files '("CMakeLists.txt" "CMakeCache.txt")
Returns an empty string if no C .c or .h files exist or if current dir is :icon "󰔶"
remote." :color "blue"
(or (unless (file-remote-p default-directory) :allow-remote nil
(when (eshell-starship--file-names-exist-p "*.h" "*.c") :action (eshell-starship-find-version-function
(with-temp-buffer ("cmake" "--version")
(when (zerop (process-file "cc" nil t nil "-v")) "cmake version \\([0-9]+\\.[0-9]+\\.[0-9]+\\)"
(goto-char (point-min)) "v" (match-string 1)))
(when (re-search-forward
"^\\([-a-zA-Z]+\\) version \\([0-9]+\\.[0-9]+\\.[0-9]+\\)" (defun eshell-starship--exts-exist-p (&rest exts)
nil t) "Test if any files with EXTS at the end of their name exist in default dir."
(concat " via " (catch 'found
(propertize (concat "C v" (dolist (ext exts)
(match-string 2) (when (seq-filter #'(lambda (name)
"-" (not (string-prefix-p "." name)))
(match-string 1)) (file-expand-wildcards (concat "*." ext)))
'face (throw 'found t)))))
'(:foreground "green yellow")))
))))) "")) (defun eshell-starship--files-exist-p (&rest names)
"Test if any of NAMES exists and are files in default directory."
(catch 'found
(dolist (name names)
(when (file-exists-p name)
(throw 'found t)))))
(defun eshell-starship--dirs-exist-p (&rest names)
"Test if any of NAMES exists and are files in default directory."
(catch 'found
(dolist (name names)
(when (file-directory-p name)
(throw 'found t)))))
(defun eshell-starship--execute-modules ()
"Execute all the modules in `eshell-starship-modules'."
(cl-loop
for (_ pred files dirs exts icon color allow-remote action)
in eshell-starship-modules
when (and (or allow-remote (not (file-remote-p default-directory)))
(or (and files (apply 'eshell-starship--files-exist-p files))
(and dirs (apply' eshell-starship--dirs-exist-p dirs))
(and exts (apply' eshell-starship--exts-exist-p exts))
(and pred (funcall pred))))
concat (if-let (result (funcall action))
(concat " via " (propertize (concat icon " " result)
'face `(:foreground ,color))))))
(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 ~."
@ -210,7 +248,7 @@ For example, a revert. If there is no current operation, return nil."
(when (zerop (vc-git-command t nil nil "status" "--porcelain=v2" (when (zerop (vc-git-command t nil nil "status" "--porcelain=v2"
"--branch" "--show-stash")) "--branch" "--show-stash"))
(goto-char (point-min)) (goto-char (point-min))
(cl-destructuring-bind (oid head upstream ahead behind stash) (cl-destructuring-bind (oid head _upstream ahead behind stash)
(eshell-starship--git-parse-status-headers) (eshell-starship--git-parse-status-headers)
(let ((file-status (eshell-starship--git-file-status stash ahead (let ((file-status (eshell-starship--git-file-status stash ahead
behind)) behind))
@ -277,11 +315,13 @@ END-TIME is the time when the command finished executing."
(dir (eshell-starship--get-current-dir)) (dir (eshell-starship--get-current-dir))
(prompt (concat (prompt (concat
"\n" "\n"
(if (file-remote-p default-directory)
(propertize "🌐 " 'face '(:foreground "light blue")))
(propertize dir 'face '(:foreground "dark turquoise")) (propertize dir 'face '(:foreground "dark turquoise"))
(unless (file-writable-p dir) (unless (file-writable-p dir)
"") "")
(eshell-starship--vc-status) (eshell-starship--vc-status)
(eshell-starship--cc-status) (eshell-starship--execute-modules)
(eshell-starship--last-command-time end-time) (eshell-starship--last-command-time end-time)
(propertize "\n" 'read-only t 'rear-nonsticky t) (propertize "\n" 'read-only t 'rear-nonsticky t)
(propertize (propertize