Refactor elisp/eshell-starship.el

This commit is contained in:
Alexander Rosenberg 2024-01-18 03:14:00 -08:00
parent 3d84c322bb
commit 7310a981e7
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

View File

@ -62,49 +62,83 @@ STATUS-LINE is the first line of output from \"git status --porcelain=v1 -b\"."
(defun eshell-starship--prompt-git-has-stash () (defun eshell-starship--prompt-git-has-stash ()
"Return t if the current git directory has a stash, nil otherwise." "Return t if the current git directory has a stash, nil otherwise."
(= (process-file vc-git-program nil nil nil (zerop (process-file vc-git-program nil nil nil
"rev-parse" "--verify" "refs/stash") 0)) "rev-parse" "--verify" "refs/stash")))
(defun eshell-starship--prompt-git-state-chars () (defun eshell-starship--prompt-git-state-chars ()
"Get chars, like + and ✘ for `eshell-starship--prompt-function'." "Get chars, like + and ✘ for `eshell-starship--prompt-function'."
(let* ((lines (eshell-starship--git-process-lines "status" "--porcelain=v1" "-b")) (with-temp-buffer
(branch-status (eshell-starship--prompt-current-branch-status (when (zerop (vc-git-command t nil nil "status" "--porcelain=v1" "-b"))
(car lines))) (goto-char (point-min))
(status-arr)) (cl-loop with command-error-function = nil
(dolist (line (cdr lines)) with status-arr = nil
(cl-loop with fields = (string-split line " " t " *") with first-line = (buffer-substring-no-properties
with status-str = (car-safe fields) (point) (pos-eol))
for status-char across status-str ;; account for newline at end
with line-count = (car (buffer-line-statistics))
with cur-buf = (current-buffer)
do (forward-line)
for x_status = (char-after)
for y_status = (char-after (1+ (point)))
until (> (line-number-at-pos) line-count)
do do
(cond ((or (= status-char ?M) (= status-char ?T)) (cond
(push ?! status-arr)) ((or (= ?D x_status y_status)
((= status-char ??) (= ?A x_status y_status)
(push ?? status-arr)) (= ?U x_status y_status)
((or (= status-char ?A) (= status-char ?C)) (and (= ?A x_status) (= ?U y_status))
(push ?+ status-arr)) (and (= ?U x_status) (= ?D y_status))
((= status-char ?D) (and (= ?U x_status) (= ?A y_status))
(and (= ?D x_status) (= ?U y_status)))
(push ?= status-arr))
((or (= x_status ?D) (= y_status ?D))
(push ? status-arr)) (push ? status-arr))
((= status-char ?R) ((or (= x_status ?R) (= y_status ?R))
(push status-arr)) (push status-arr))
((= status-char ?U) ((= y_status ?M)
(push ?= status-arr))))) (push ?! status-arr))
((or (= x_status ?A) (= x_status ?M))
(push ?+ status-arr))
((= x_status y_status ??)
(push ?? status-arr)))
finally
(sort status-arr #'(lambda (a b)
(cond
((= a ?=)
t)
((= b ?=)
nil)
(t
(< a b)))))
(when (eshell-starship--prompt-git-has-stash) (when (eshell-starship--prompt-git-has-stash)
(push ?$ status-arr)) (if (= (car status-arr) ?=)
(sort status-arr #'<) (setq status-arr (append '(?= ?$) (cdr status-arr)))
(when branch-status (push ?$ status-arr)))
(when-let (branch-status (eshell-starship--prompt-current-branch-status
first-line))
(push branch-status status-arr)) (push branch-status status-arr))
(apply 'string (seq-uniq status-arr)))) finally return (apply 'string (seq-uniq status-arr))))))
(defun eshell-starship--prompt-git-get-operation () (defun eshell-starship--prompt-git-get-operation ()
"Return the current git operation. For example, a revert." "Return the current git operation. For example, a revert."
(let ((git-dir (expand-file-name ".git" (vc-git-root default-directory)))) (let ((git-dir (expand-file-name ".git" (vc-git-root default-directory))))
(cond (cond
((file-exists-p (expand-file-name "REVERT_HEAD" git-dir)) ((file-exists-p (expand-file-name "rebase-apply/applying" git-dir))
"REVERTING") "AM")
((file-exists-p (expand-file-name "rebase-apply/rebasing" git-dir))
"REBASE")
((file-exists-p (expand-file-name "rebase-apply" git-dir))
"AM/REBASE")
((file-exists-p (expand-file-name "rebase-merge" git-dir)) ((file-exists-p (expand-file-name "rebase-merge" git-dir))
"REBASING") "REBASING")
((file-exists-p (expand-file-name "CHERRY_PICK_HEAD" git-dir))
"CHERRY-PICKING")
((file-exists-p (expand-file-name "MERGE_HEAD" git-dir)) ((file-exists-p (expand-file-name "MERGE_HEAD" git-dir))
"MERGING")))) "MERGING")
((file-exists-p (expand-file-name "BISECT_LOG" git-dir))
"BISECTING")
((file-exists-p (expand-file-name "REVERT_HEAD" git-dir))
"REVERTING"))))
(defun eshell-starship--prompt-git-status () (defun eshell-starship--prompt-git-status ()
"Get git status for `eshell-starship--prompt-function'." "Get git status for `eshell-starship--prompt-function'."