Rename doom-format lib -> doom-output

`format` isn't an appropriate for this library, considering it (and
future additions to it) will be mainly concerned with printing or
capturing output.
This commit is contained in:
Henrik Lissner 2020-05-25 03:32:50 -04:00
parent a706c3527b
commit be889d09e8
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
4 changed files with 64 additions and 57 deletions

View file

@ -1,6 +1,6 @@
;;; core/autoload/format.el -*- lexical-binding: t; -*- ;;; core/autoload/output.el -*- lexical-binding: t; -*-
(defvar doom-format-ansi-alist (defvar doom-output-ansi-alist
'(;; fx '(;; fx
(bold 1 :weight bold) (bold 1 :weight bold)
(dark 2) (dark 2)
@ -34,27 +34,27 @@
This serves as the cipher for converting (COLOR ...) function calls in `print!' This serves as the cipher for converting (COLOR ...) function calls in `print!'
and `format!' into colored output, where COLOR is any car of this list.") and `format!' into colored output, where COLOR is any car of this list.")
(defvar doom-format-class-alist (defvar doom-output-class-alist
`((color . doom--format-color) `((color . doom--output-color)
(class . doom--format-class) (class . doom--output-class)
(indent . doom--format-indent) (indent . doom--output-indent)
(autofill . doom--format-autofill) (autofill . doom--output-autofill)
(success . (lambda (str &rest args) (success . (lambda (str &rest args)
(apply #'doom--format-color 'green (format "✓ %s" str) args))) (apply #'doom--output-color 'green (format "✓ %s" str) args)))
(warn . (lambda (str &rest args) (warn . (lambda (str &rest args)
(apply #'doom--format-color 'yellow (format "! %s" str) args))) (apply #'doom--output-color 'yellow (format "! %s" str) args)))
(error . (lambda (str &rest args) (error . (lambda (str &rest args)
(apply #'doom--format-color 'red (format "x %s" str) args))) (apply #'doom--output-color 'red (format "x %s" str) args)))
(info . (lambda (str &rest args) (info . (lambda (str &rest args)
(concat "- " (if args (apply #'format str args) str)))) (concat "- " (if args (apply #'format str args) str))))
(start . (lambda (str &rest args) (start . (lambda (str &rest args)
(concat "> " (if args (apply #'format str args) str)))) (concat "> " (if args (apply #'format str args) str))))
(debug . (lambda (str &rest args) (debug . (lambda (str &rest args)
(if doom-debug-mode (if doom-debug-p
(if args (apply #'doom--output-color 'dark
(apply #'format str args) (format "- %s" str)
(format "%s" str)) args)
""))) "")))
(path . abbreviate-file-name) (path . abbreviate-file-name)
(symbol . symbol-name) (symbol . symbol-name)
@ -78,14 +78,14 @@ and `format!' into colored output, where COLOR is any car of this list.")
Any of these classes can be called like functions from within `format!' and Any of these classes can be called like functions from within `format!' and
`print!' calls, which will transform their input.") `print!' calls, which will transform their input.")
(defvar doom-format-indent 0 (defvar doom-output-indent 0
"Level to rigidly indent text returned by `format!' and `print!'.") "Level to rigidly indent text returned by `format!' and `print!'.")
(defvar doom-format-indent-increment 2 (defvar doom-output-indent-increment 2
"Steps in which to increment `doom-format-indent' for consecutive levels.") "Steps in which to increment `doom-output-indent' for consecutive levels.")
(defvar doom-format-backend (defvar doom-output-backend
(if noninteractive 'ansi 'text-properties) (if doom-interactive-p 'text-properties 'ansi)
"Determines whether to print colors with ANSI codes or with 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.") Accepts 'ansi and 'text-properties. nil means don't render colors.")
@ -98,21 +98,20 @@ Accepts 'ansi and 'text-properties. nil means don't render colors.")
(defun doom--format (output) (defun doom--format (output)
(if (string-empty-p (string-trim output)) (if (string-empty-p (string-trim output))
"" ""
(concat (make-string doom-format-indent 32) (concat (make-string doom-output-indent 32)
(replace-regexp-in-string (replace-regexp-in-string
"\n" (concat "\n" (make-string doom-format-indent 32)) "\n" (concat "\n" (make-string doom-output-indent 32))
output t t)))) output t t))))
;;;###autoload ;;;###autoload
(defun doom--format-print (output) (defun doom--print (output)
(unless (string-empty-p output) (unless (string-empty-p output)
(princ output) (princ output)
(when (or noninteractive (not (eq standard-output t))) (terpri)
(terpri)) ; newline
t)) t))
;;;###autoload ;;;###autoload
(defun doom--format-indent (width text &optional prefix) (defun doom--output-indent (width 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
(setq text (format "%s" text)) (setq text (format "%s" text))
@ -127,7 +126,7 @@ Accepts 'ansi and 'text-properties. nil means don't render colors.")
(buffer-string))) (buffer-string)))
;;;###autoload ;;;###autoload
(defun doom--format-autofill (&rest msgs) (defun doom--output-autofill (&rest msgs)
"Ensure MSG is split into lines no longer than `fill-column'." "Ensure MSG is split into lines no longer than `fill-column'."
(with-temp-buffer (with-temp-buffer
(let ((fill-column 76)) (let ((fill-column 76))
@ -138,19 +137,19 @@ Accepts 'ansi and 'text-properties. nil means don't render colors.")
(buffer-string)))) (buffer-string))))
;;;###autoload ;;;###autoload
(defun doom--format-color (style format &rest args) (defun doom--output-color (style format &rest args)
"Apply STYLE to formatted MESSAGE with ARGS. "Apply STYLE to formatted MESSAGE with ARGS.
STYLE is a symbol that correlates to `doom-format-ansi-alist'. STYLE is a symbol that correlates to `doom-output-ansi-alist'.
In a noninteractive session, this wraps the result in ansi color codes. In a noninteractive session, this wraps the result in ansi color codes.
Otherwise, it maps colors to a term-color-* face." Otherwise, it maps colors to a term-color-* face."
(let* ((code (cadr (assq style doom-format-ansi-alist))) (let* ((code (cadr (assq style doom-output-ansi-alist)))
(format (format "%s" format)) (format (format "%s" format))
(message (if args (apply #'format format args) format))) (message (if args (apply #'format format args) format)))
(unless code (unless code
(error "%S is an invalid color" style)) (error "%S is an invalid color" style))
(pcase doom-format-backend (pcase doom-output-backend
(`ansi (`ansi
(format "\e[%dm%s\e[%dm" code message 0)) (format "\e[%dm%s\e[%dm" code message 0))
(`text-properties (`text-properties
@ -160,20 +159,20 @@ Otherwise, it maps colors to a term-color-* face."
'face 'face
(append (get-text-property 0 'face format) (append (get-text-property 0 'face format)
(cond ((>= code 40) (cond ((>= code 40)
`(:background ,(caddr (assq style doom-format-ansi-alist)))) `(:background ,(caddr (assq style doom-output-ansi-alist))))
((>= code 30) ((>= code 30)
`(:foreground ,(face-foreground (caddr (assq style doom-format-ansi-alist))))) `(:foreground ,(face-foreground (caddr (assq style doom-output-ansi-alist)))))
((cddr (assq style doom-format-ansi-alist))))))) ((cddr (assq style doom-output-ansi-alist)))))))
(_ message)))) (_ message))))
;;;###autoload ;;;###autoload
(defun doom--format-class (class format &rest args) (defun doom--output-class (class format &rest args)
"Apply CLASS to formatted format with ARGS. "Apply CLASS to formatted format with ARGS.
CLASS is derived from `doom-format-class-alist', and can contain any arbitrary, CLASS is derived from `doom-output-class-alist', and can contain any arbitrary,
transformative logic." transformative logic."
(let (fn) (let (fn)
(cond ((setq fn (cdr (assq class doom-format-class-alist))) (cond ((setq fn (cdr (assq class doom-output-class-alist)))
(if (functionp fn) (if (functionp fn)
(apply fn format args) (apply fn format args)
(error "%s does not have a function" class))) (error "%s does not have a function" class)))
@ -181,20 +180,20 @@ transformative logic."
(format)))) (format))))
;;;###autoload ;;;###autoload
(defun doom--format-apply (forms &optional sub) (defun doom--output-apply (forms &optional sub)
"Replace color-name functions with calls to `doom--format-color'." "Replace color-name functions with calls to `doom--output-color'."
(cond ((null forms) nil) (cond ((null forms) nil)
((listp forms) ((listp forms)
(append (cond ((not (symbolp (car forms))) (append (cond ((not (symbolp (car forms)))
(list (doom--format-apply (car forms)))) (list (doom--output-apply (car forms))))
(sub (sub
(list (car forms))) (list (car forms)))
((assq (car forms) doom-format-ansi-alist) ((assq (car forms) doom-output-ansi-alist)
`(doom--format-color ',(car forms))) `(doom--output-color ',(car forms)))
((assq (car forms) doom-format-class-alist) ((assq (car forms) doom-output-class-alist)
`(doom--format-class ',(car forms))) `(doom--output-class ',(car forms)))
((list (car forms)))) ((list (car forms))))
(doom--format-apply (cdr forms) t) (doom--output-apply (cdr forms) t)
nil)) nil))
(forms))) (forms)))
@ -202,12 +201,12 @@ transformative logic."
(defmacro format! (message &rest args) (defmacro format! (message &rest args)
"An alternative to `format' that understands (color ...) and converts them "An alternative to `format' that understands (color ...) and converts them
into faces or ANSI codes depending on the type of sesssion we're in." into faces or ANSI codes depending on the type of sesssion we're in."
`(doom--format (format ,@(doom--format-apply `(,message ,@args))))) `(doom--format (format ,@(doom--output-apply `(,message ,@args)))))
;;;###autoload ;;;###autoload
(defmacro print-group! (&rest body) (defmacro print-group! (&rest body)
"Indents any `print!' or `format!' output within BODY." "Indents any `print!' or `format!' output within BODY."
`(let ((doom-format-indent (+ doom-format-indent-increment doom-format-indent))) `(let ((doom-output-indent (+ doom-output-indent-increment doom-output-indent)))
,@body)) ,@body))
;;;###autoload ;;;###autoload
@ -223,7 +222,7 @@ Can be colored using (color ...) blocks:
(print! (green \"Great %s!\") \"success\") (print! (green \"Great %s!\") \"success\")
Uses faces in interactive sessions and ANSI codes otherwise." Uses faces in interactive sessions and ANSI codes otherwise."
`(doom--format-print (format! ,message ,@args))) `(doom--print (format! ,message ,@args)))
;;;###autoload ;;;###autoload
(defmacro insert! (message &rest args) (defmacro insert! (message &rest args)
@ -242,3 +241,11 @@ Uses faces in interactive sessions and ANSI codes otherwise."
(defmacro user-error! (message &rest args) (defmacro user-error! (message &rest args)
"Like `user-error', but with the power of `format!'." "Like `user-error', but with the power of `format!'."
`(user-error (format! ,message ,@args))) `(user-error (format! ,message ,@args)))
;; ;;;###autoload
;; (defmacro with-output-to-buffer! (dest &rest body)
;; "Send all output produced in BODY to DEST.
;; DEST can be one or more of `standard-output', a buffer, a file
;; )

View file

@ -165,7 +165,7 @@ in."
(condition-case-unless-debug ex (condition-case-unless-debug ex
(let ((doctor-file (doom-module-path (car key) (cdr key) "doctor.el")) (let ((doctor-file (doom-module-path (car key) (cdr key) "doctor.el"))
(packages-file (doom-module-path (car key) (cdr key) "packages.el"))) (packages-file (doom-module-path (car key) (cdr key) "packages.el")))
(cl-loop with doom-format-indent = 6 (cl-loop with doom-output-indent = 6
for name in (let (doom-packages for name in (let (doom-packages
doom-disabled-packages) doom-disabled-packages)
(load packages-file 'noerror 'nomessage) (load packages-file 'noerror 'nomessage)

View file

@ -5,7 +5,7 @@
(load! "autoload/process") (load! "autoload/process")
(load! "autoload/plist") (load! "autoload/plist")
(load! "autoload/files") (load! "autoload/files")
(load! "autoload/format") (load! "autoload/output")
;; Create all our core directories to quell file errors ;; Create all our core directories to quell file errors
(mapc (doom-rpartial #'make-directory 'parents) (mapc (doom-rpartial #'make-directory 'parents)
@ -311,7 +311,7 @@ original state.")
(terpri) (terpri)
(let* ((options (let* ((options
(cons (lambda () (cons (lambda ()
(let ((doom-format-indent 0)) (let ((doom-output-indent 0))
(terpri) (terpri)
(print! (warn "Aborted"))) (print! (warn "Aborted")))
(kill-emacs 1)) (kill-emacs 1))
@ -329,7 +329,7 @@ original state.")
(funcall (nth answer options))))))))) (funcall (nth answer options)))))))))
(defadvice! doom--straight-respect-print-indent-a (args) (defadvice! doom--straight-respect-print-indent-a (args)
"Indent straight progress messages to respect `doom-format-indent', so we "Indent straight progress messages to respect `doom-output-indent', so we
don't have to pass whitespace to `straight-use-package's fourth argument don't have to pass whitespace to `straight-use-package's fourth argument
everywhere we use it (and internally)." everywhere we use it (and internally)."
:filter-args #'straight-use-package :filter-args #'straight-use-package
@ -338,9 +338,9 @@ everywhere we use it (and internally)."
args args
(list melpa-style-recipe no-clone no-build (list melpa-style-recipe no-clone no-build
(if (and (not cause) (if (and (not cause)
(boundp 'doom-format-indent) (boundp 'doom-output-indent)
(> doom-format-indent 0)) (> doom-output-indent 0))
(make-string (1- (or doom-format-indent 1)) 32) (make-string (1- (or doom-output-indent 1)) 32)
cause) cause)
interactive))) interactive)))

View file

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