fix(cli): properly overwrite last line of output

Before this, text that extended past the new line's length would
persist, causing garbled output like this:

  > Ensuring packages are installed and built...
    > Updating recipe repos...
    > Cloning org-appear...r emacsmirror-mirror...
    - No packages need attention

Which might lead you to believe org-appear is being cloned from
emacsmirror-mirror, but the full output is actually:

  > Ensuring packages are installed and built...
    > Updating recipe repos...
      > Updating recipes for melpa...
      > Updating recipes for nongnu-elpa...
      > Updating recipes for gnu-elpa-mirror...
      > Updating recipes for el-get...
      > Updating recipes for emacsmirror-mirror...
    > Cloning org-appear...
    - No packages need attention
This commit is contained in:
Henrik Lissner 2024-07-25 22:57:13 -04:00
parent 7c5d8641a1
commit 5852b2ecf5
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
2 changed files with 40 additions and 34 deletions

View file

@ -121,16 +121,16 @@ list remains lean."
nil (mapcar (doom-rpartial #'gethash straight--repo-cache) nil (mapcar (doom-rpartial #'gethash straight--repo-cache)
(mapcar #'symbol-name straight-recipe-repositories))) (mapcar #'symbol-name straight-recipe-repositories)))
(recipe package type local-repo) (recipe package type local-repo)
(let ((esc (unless init-file-debug "\033[1A")) (let ((esc (if init-file-debug "" "\033[1A"))
(ref (straight-vc-get-commit type local-repo)) (ref (straight-vc-get-commit type local-repo))
newref output) newref output)
(print! (start "\033[KUpdating recipes for %s...%s") package esc) (print! (start "\rUpdating recipes for %s...%s") package esc)
(doom-packages--straight-with (straight-vc-fetch-from-remote recipe) (doom-packages--straight-with (straight-vc-fetch-from-remote recipe)
(when .it (when .it
(setq output .output) (setq output .output)
(straight-merge-package package) (straight-merge-package package)
(unless (equal ref (setq newref (straight-vc-get-commit type local-repo))) (unless (equal ref (setq newref (straight-vc-get-commit type local-repo)))
(print! (success "\033[K%s updated (%s -> %s)") (print! (success "\r%s updated (%s -> %s)")
package package
(doom-packages--abbrev-commit ref) (doom-packages--abbrev-commit ref)
(doom-packages--abbrev-commit newref)) (doom-packages--abbrev-commit newref))
@ -214,7 +214,7 @@ list remains lean."
(comp-async-runnings))) (comp-async-runnings)))
while (not (zerop pending)) while (not (zerop pending))
if (/= previous pending) do if (/= previous pending) do
(print! (start "\033[KNatively compiling %d files...\033[1A" pending)) (print! (start "\rNatively compiling %d files...\033[1A" pending))
(setq previous pending (setq previous pending
timer 0) timer 0)
else do else do
@ -362,7 +362,7 @@ list remains lean."
;; and causing a rebuild of those packages each time `doom sync' ;; and causing a rebuild of those packages each time `doom sync'
;; or similar is run, so we clean it up ourselves: ;; or similar is run, so we clean it up ourselves:
(delete-directory (straight--modified-dir) 'recursive) (delete-directory (straight--modified-dir) 'recursive)
(print! (success "\033[KBuilt %d package(s)") (length built))) (print! (success "\rBuilt %d package(s)") (length built)))
(print! (item "No packages need attention")) (print! (item "No packages need attention"))
nil)))) nil))))
@ -378,7 +378,7 @@ list remains lean."
(packages-to-rebuild (make-hash-table :test 'equal)) (packages-to-rebuild (make-hash-table :test 'equal))
(repos-to-rebuild (make-hash-table :test 'equal)) (repos-to-rebuild (make-hash-table :test 'equal))
(total (length recipes)) (total (length recipes))
(esc (unless init-file-debug "\033[1A")) (esc (if init-file-debug "" "\033[1A"))
(i 0)) (i 0))
(if pinned-only-p (if pinned-only-p
(print! (start "Updating pinned packages...")) (print! (start "Updating pinned packages..."))
@ -411,7 +411,7 @@ list remains lean."
output) output)
(or (cond (or (cond
((not (stringp target-ref)) ((not (stringp target-ref))
(print! (start "\033[K(%d/%d) Fetching %s...%s") i total package esc) (print! (start "\r(%d/%d) Fetching %s...%s") i total package esc)
(doom-packages--straight-with (straight-vc-fetch-from-remote recipe) (doom-packages--straight-with (straight-vc-fetch-from-remote recipe)
(when .it (when .it
(straight-merge-package package) (straight-merge-package package)
@ -422,13 +422,13 @@ list remains lean."
(cl-return))))) (cl-return)))))
((doom-packages--same-commit-p target-ref ref) ((doom-packages--same-commit-p target-ref ref)
(print! (item "\033[K(%d/%d) %s is up-to-date...%s") i total package esc) (print! (item "\r(%d/%d) %s is up-to-date...%s") i total package esc)
(cl-return)) (cl-return))
((if (straight-vc-commit-present-p recipe target-ref) ((if (straight-vc-commit-present-p recipe target-ref)
(print! (start "\033[K(%d/%d) Checking out %s (%s)...%s") (print! (start "\r(%d/%d) Checking out %s (%s)...%s")
i total package (doom-packages--abbrev-commit target-ref) esc) i total package (doom-packages--abbrev-commit target-ref) esc)
(print! (start "\033[K(%d/%d) Fetching %s...%s") i total package esc) (print! (start "\r(%d/%d) Fetching %s...%s") i total package esc)
(and (straight-vc-fetch-from-remote recipe) (and (straight-vc-fetch-from-remote recipe)
(straight-vc-commit-present-p recipe target-ref))) (straight-vc-commit-present-p recipe target-ref)))
(straight-vc-check-out-commit recipe target-ref) (straight-vc-check-out-commit recipe target-ref)
@ -437,7 +437,7 @@ list remains lean."
commits (length (split-string output "\n" t)))) commits (length (split-string output "\n" t))))
(doom-packages--same-commit-p target-ref (straight-vc-get-commit type local-repo))) (doom-packages--same-commit-p target-ref (straight-vc-get-commit type local-repo)))
((print! (start "\033[K(%d/%d) Re-cloning %s...") i total local-repo esc) ((print! (start "\r(%d/%d) Re-cloning %s...") i total local-repo esc)
(let ((repo (straight--repos-dir local-repo)) (let ((repo (straight--repos-dir local-repo))
(straight-vc-git-default-clone-depth 'full)) (straight-vc-git-default-clone-depth 'full))
(delete-directory repo 'recursive) (delete-directory repo 'recursive)
@ -448,7 +448,7 @@ list remains lean."
(setq output (doom-packages--commit-log-between ref target-ref) (setq output (doom-packages--commit-log-between ref target-ref)
commits (length (split-string output "\n" t)))))))) commits (length (split-string output "\n" t))))))))
(progn (progn
(print! (warn "\033[K(%d/%d) Failed to fetch %s") (print! (warn "\r(%d/%d) Failed to fetch %s")
i total local-repo) i total local-repo)
(unless (string-empty-p output) (unless (string-empty-p output)
(print-group! (print! (item "%s" output)))) (print-group! (print! (item "%s" output))))
@ -472,7 +472,7 @@ list remains lean."
(add-to-rebuild (cdr tree)))))) (add-to-rebuild (cdr tree))))))
(add-to-rebuild dependents) (add-to-rebuild dependents)
(puthash package t packages-to-rebuild) (puthash package t packages-to-rebuild)
(print! (success "\033[K(%d/%d) %s: %s -> %s%s%s") (print! (success "\r(%d/%d) %s: %s -> %s%s%s")
i total local-repo i total local-repo
(doom-packages--abbrev-commit ref) (doom-packages--abbrev-commit ref)
(doom-packages--abbrev-commit target-ref) (doom-packages--abbrev-commit target-ref)
@ -495,15 +495,14 @@ list remains lean."
(error (error
(signal 'doom-package-error (list package e))))))) (signal 'doom-package-error (list package e)))))))
(print-group! (print-group!
(princ "\033[K")
(if (hash-table-empty-p packages-to-rebuild) (if (hash-table-empty-p packages-to-rebuild)
(ignore (print! (success "All %d packages are up-to-date") total)) (ignore (print! (success "\rAll %d packages are up-to-date") total))
(doom-packages--cli-recipes-update) (doom-packages--cli-recipes-update)
(straight--transaction-finalize) (straight--transaction-finalize)
(let ((default-directory (straight--build-dir))) (let ((default-directory (straight--build-dir)))
(mapc (doom-rpartial #'delete-directory 'recursive) (mapc (doom-rpartial #'delete-directory 'recursive)
(hash-table-keys packages-to-rebuild))) (hash-table-keys packages-to-rebuild)))
(print! (success "Updated %d package(s)") (print! (success "\rUpdated %d package(s)")
(hash-table-count packages-to-rebuild)) (hash-table-count packages-to-rebuild))
(doom-packages-ensure) (doom-packages-ensure)
t)))) t))))
@ -532,22 +531,22 @@ list remains lean."
(error "No repo specified for regrafting")) (error "No repo specified for regrafting"))
(let ((default-directory (straight--repos-dir repo))) (let ((default-directory (straight--repos-dir repo)))
(unless (file-directory-p ".git") (unless (file-directory-p ".git")
(print! (warn "\033[Krepos/%s is not a git repo, skipping" repo)) (print! (warn "\rrepos/%s is not a git repo, skipping" repo))
(cl-return)) (cl-return))
(unless (file-in-directory-p default-directory straight-base-dir) (unless (file-in-directory-p default-directory straight-base-dir)
(print! (warn "\033[KSkipping repos/%s because it is local" repo)) (print! (warn "\rSkipping repos/%s because it is local" repo))
(cl-return)) (cl-return))
(let ((before-size (doom-directory-size default-directory))) (let ((before-size (doom-directory-size default-directory)))
(doom-call-process "git" "reset" "--hard") (doom-call-process "git" "reset" "--hard")
(doom-call-process "git" "clean" "-ffd") (doom-call-process "git" "clean" "-ffd")
(if (not (zerop (car (doom-call-process "git" "replace" "--graft" "HEAD")))) (if (not (zerop (car (doom-call-process "git" "replace" "--graft" "HEAD"))))
(print! (item "\033[Krepos/%s is already compact\033[1A" repo)) (print! (item "\rrepos/%s is already compact\033[1A" repo))
(doom-call-process "git" "reflog" "expire" "--expire=all" "--all") (doom-call-process "git" "reflog" "expire" "--expire=all" "--all")
(doom-call-process "git" "gc" "--prune=now") (doom-call-process "git" "gc" "--prune=now")
(let ((after-size (doom-directory-size default-directory))) (let ((after-size (doom-directory-size default-directory)))
(if (equal after-size before-size) (if (equal after-size before-size)
(print! (success "\033[Krepos/%s cannot be compacted further" repo)) (print! (success "\rrepos/%s cannot be compacted further" repo))
(print! (success "\033[KRegrafted repos/%s (from %0.1fKB to %0.1fKB)") (print! (success "\rRegrafted repos/%s (from %0.1fKB to %0.1fKB)")
repo before-size after-size))))) repo before-size after-size)))))
t)) t))
@ -559,9 +558,9 @@ list remains lean."
(let ((before-size (doom-directory-size (straight--repos-dir)))) (let ((before-size (doom-directory-size (straight--repos-dir))))
(print-group! (print-group!
(prog1 (delq nil (mapcar #'doom-packages--regraft-repo repos)) (prog1 (delq nil (mapcar #'doom-packages--regraft-repo repos))
(princ "\033[K") ;; (princ "\r\033[K")
(let ((after-size (doom-directory-size (straight--repos-dir)))) (let ((after-size (doom-directory-size (straight--repos-dir))))
(print! (success "Finished regrafting. Size before: %0.1fKB and after: %0.1fKB (%0.1fKB)") (print! (success "\rFinished regrafting. Size before: %0.1fKB and after: %0.1fKB (%0.1fKB)")
before-size after-size before-size after-size
(- after-size before-size)))))))) (- after-size before-size))))))))

View file

@ -308,18 +308,25 @@ based on the print level of the message. For example:
(defun doom-print--indent (text &optional prefix) (defun doom-print--indent (text &optional prefix)
"Indent TEXT by WIDTH spaces. If ARGS, format TEXT with them." "Indent TEXT by WIDTH spaces. If ARGS, format TEXT with them."
(with-temp-buffer (with-temp-buffer
(let ((width (let* ((re "^\\( *\\)\r")
(cond ((null prefix) (line-feed (if (stringp text) (string-match-p re text)))
doom-print-indent-increment) (width (cond ((null prefix) doom-print-indent-increment)
((integerp prefix) ((integerp prefix) prefix)
prefix)
((length (ansi-color-filter-apply (format "%s" prefix))))))) ((length (ansi-color-filter-apply (format "%s" prefix)))))))
(insert (format "%s" (or text ""))) (insert
(if text
(replace-regexp-in-string re "\\1\033[K" (format "%s" text))
""))
(indent-rigidly (point-min) (point-max) width) (indent-rigidly (point-min) (point-max) width)
(save-excursion
(when line-feed
(goto-char (point-min))
(insert "\r")))
(save-excursion
(when (stringp prefix) (when (stringp prefix)
(goto-char (point-min)) (goto-char (point-min))
(delete-char width) (delete-char (+ width (if line-feed 1 0)))
(insert prefix)) (insert prefix)))
(buffer-string)))) (buffer-string))))
;;;###autoload ;;;###autoload