Rewrite doom-doctor

- Use message library instead of reinventing the wheel
- Fix -d/--debug support for `bin/doom doctor`
- Add indent and autofill support to print! and format!
- Add doom-message-backend for forcing format! to use a specific backend
- Phase out anaphoric when! macro in doctor scripts, it was hardly used
This commit is contained in:
Henrik Lissner 2019-04-23 12:41:46 -04:00
parent 51a28249bd
commit 9bea168cc1
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
5 changed files with 144 additions and 103 deletions

View file

@ -31,27 +31,60 @@
(on-white 47 term-color-white))
"TODO")
(defvar doom-message-backend
(if noninteractive 'ansi 'text-properties)
"Determines whether to print colors with ANSI codes or with text properties.
Accepts 'ansi and 'text-properties. nil means don't render colors.")
;;;###autoload
(defun doom-color-apply (style text)
(defun doom-message-indent (width text &rest args)
(with-temp-buffer
(insert (apply #'format text args))
(let ((fill-column 80))
(fill-region (point-min) (point-max))
(indent-rigidly (point-min) (point-max) width))
(when (> width 2)
(goto-char (point-min))
(beginning-of-line-text)
(delete-char -2)
(insert "> "))
(buffer-string)))
;;;###autoload
(defun doom-message-autofill (&rest msgs)
(with-temp-buffer
(let ((fill-column 70))
(dolist (line msgs)
(when line
(insert line)))
(fill-region (point-min) (point-max))
(buffer-string))))
;;;###autoload
(defun doom-color-apply (style text &rest args)
"Apply CODE to formatted MESSAGE with ARGS. CODE is derived from any of
`doom-message-fg', `doom-message-bg' or `doom-message-fx'.
In a noninteractive session, this wraps the result in ansi color codes.
Otherwise, it maps colors to a term-color-* face."
(let ((code (cadr (assq style doom-ansi-alist))))
(if noninteractive
(format "\e[%dm%s\e[%dm"
(cadr (assq style doom-ansi-alist))
text 0)
(require 'term) ; piggyback on term's color faces
(propertize
text 'face
(append (get-text-property 0 'face text)
(cond ((>= code 40)
`(:background ,(caddr (assq style doom-ansi-alist))))
((>= code 30)
`(:foreground ,(face-foreground (caddr (assq style doom-ansi-alist)))))
((cddr (assq style doom-ansi-alist)))))))))
(let ((code (car (cdr (assq style doom-ansi-alist)))))
(pcase doom-message-backend
(`ansi
(format "\e[%dm%s\e[%dm"
(car (cdr (assq style doom-ansi-alist)))
(apply #'format text args) 0))
(`text-properties
(require 'term) ; piggyback on term's color faces
(propertize
(apply #'format text args) 'face
(append (get-text-property 0 'face text)
(cond ((>= code 40)
`(:background ,(caddr (assq style doom-ansi-alist))))
((>= code 30)
`(:foreground ,(face-foreground (caddr (assq style doom-ansi-alist)))))
((cddr (assq style doom-ansi-alist)))))))
(_ (apply #'format text args)))))
(defun doom--short-color-replace (forms)
"Replace color-name functions with calls to `doom-color-apply'."
@ -64,6 +97,10 @@ Otherwise, it maps colors to a term-color-* face."
((eq (car forms) 'color)
(pop forms)
`(doom-color-apply ,(car forms)))
((memq (car forms) '(indent autofill))
(let ((fn (pop forms)))
`(,(intern (format "doom-message-%s" fn))
,(car forms))))
((list (car forms))))
(doom--short-color-replace (cdr forms))
nil))

View file

@ -18,19 +18,19 @@
(/ size 1024))
(explain! "Consider deleting it from your system (manually)"))))
(when! (not (executable-find doom-projectile-fd-binary))
(when (not (executable-find doom-projectile-fd-binary))
(warn! "Couldn't find the `fd' binary; project file searches will be slightly slower"))
(let ((default-directory "~"))
(require 'projectile)
(when! (cl-find-if #'projectile-file-exists-p projectile-project-root-files-bottom-up)
(when (cl-find-if #'projectile-file-exists-p projectile-project-root-files-bottom-up)
(warn! "Your $HOME is recognized as a project root")
(explain! "Doom will disable bottom-up root search, which may reduce the accuracy of project\n"
"detection.")))
;; There should only be one
(when! (and (file-equal-p doom-private-dir "~/.config/doom")
(file-directory-p "~/.doom.d"))
(when (and (file-equal-p doom-private-dir "~/.config/doom")
(file-directory-p "~/.doom.d"))
(warn! "Both %S and '~/.doom.d' exist on your system"
(abbreviate-file-name doom-private-dir))
(explain! "Doom will only load one of these (~/.config/doom takes precedence). Since\n"

View file

@ -3,8 +3,9 @@
(describe "core/autoload/message"
(describe "format!"
:var (noninteractive)
(before-all (setq noninteractive t))
:var (doom-message-backend)
(before-all
(setq doom-message-backend 'ansi))
(it "should be a drop-in replacement for `format'"
(expect (format! "Hello %s" "World")
@ -14,8 +15,8 @@
(expect (format! (red "Hello %s") "World")
:to-equal "Hello World"))
(it "supports faces in interactive sessions"
(let (noninteractive)
(it "supports text properties in interactive sessions"
(let ((doom-message-backend 'text-properties))
(expect (get-text-property 0 'face (format! (red "Hello %s") "World"))
:to-equal (list :foreground (face-foreground 'term-color-red)))))