2017-06-08 11:47:56 +02:00
|
|
|
;;; core/autoload/message.el -*- lexical-binding: t; -*-
|
2017-04-12 10:51:29 -04:00
|
|
|
|
2017-04-15 03:14:03 -04:00
|
|
|
(defconst doom-message-fg
|
2018-05-20 11:44:06 +02:00
|
|
|
'((black 30 term-color-black)
|
|
|
|
(red 31 term-color-red)
|
|
|
|
(green 32 term-color-green)
|
|
|
|
(yellow 33 term-color-yellow)
|
|
|
|
(blue 34 term-color-blue)
|
|
|
|
(magenta 35 term-color-magenta)
|
|
|
|
(cyan 36 term-color-cyan)
|
|
|
|
(white 37 term-color-white))
|
2017-04-12 10:51:29 -04:00
|
|
|
"List of text colors.")
|
|
|
|
|
2017-04-15 03:14:03 -04:00
|
|
|
(defconst doom-message-bg
|
2018-05-20 11:44:06 +02:00
|
|
|
'((on-black 40 term-color-black)
|
|
|
|
(on-red 41 term-color-red)
|
|
|
|
(on-green 42 term-color-green)
|
|
|
|
(on-yellow 43 term-color-yellow)
|
|
|
|
(on-blue 44 term-color-blue)
|
|
|
|
(on-magenta 45 term-color-magenta)
|
|
|
|
(on-cyan 46 term-color-cyan)
|
|
|
|
(on-white 47 term-color-white))
|
2017-04-12 10:51:29 -04:00
|
|
|
"List of colors to draw text on.")
|
|
|
|
|
2017-04-15 03:14:03 -04:00
|
|
|
(defconst doom-message-fx
|
2018-05-20 11:44:06 +02:00
|
|
|
'((bold 1 :weight bold)
|
|
|
|
(dark 2)
|
|
|
|
(italic 3 :slant italic)
|
|
|
|
(underscore 4 :underline t)
|
|
|
|
(blink 5)
|
|
|
|
(rapid 6)
|
|
|
|
(contrary 7)
|
|
|
|
(concealed 8)
|
|
|
|
(strike 9 :strike-through t))
|
2017-04-12 10:51:29 -04:00
|
|
|
"List of styles.")
|
|
|
|
|
2018-02-14 03:21:33 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom-ansi-apply (code message &rest args)
|
2018-05-20 11:44:06 +02:00
|
|
|
"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 ((text (apply #'format message args)))
|
|
|
|
(if noninteractive
|
|
|
|
(format "\e[%dm%s\e[%dm"
|
|
|
|
(cadr
|
|
|
|
(or (assq code doom-message-fg)
|
|
|
|
(assq code doom-message-bg)
|
|
|
|
(assq code doom-message-fx)))
|
|
|
|
text 0)
|
|
|
|
(require 'term) ; piggyback on term's color faces
|
|
|
|
(propertize
|
|
|
|
text 'face
|
|
|
|
(let (spec)
|
|
|
|
(cond ((setq spec (caddr (assq code doom-message-fg)))
|
|
|
|
`(:foreground ,(face-foreground spec)))
|
|
|
|
((setq spec (caddr (assq code doom-message-bg)))
|
|
|
|
`(:background ,(face-background spec)))
|
|
|
|
((cddr (assq code doom-message-fx)))))))))
|
2018-02-14 03:21:33 -05:00
|
|
|
|
2017-04-12 10:51:29 -04:00
|
|
|
;;;###autoload
|
2017-04-15 01:29:24 -04:00
|
|
|
(defmacro format! (message &rest args)
|
2018-05-20 11:44:06 +02:00
|
|
|
"An alternative to `format' that understands (color ...) and converts them
|
|
|
|
into faces or ANSI codes depending on the type of sesssion we're in."
|
2017-04-12 10:51:29 -04:00
|
|
|
`(cl-flet*
|
2017-06-08 11:47:56 +02:00
|
|
|
(,@(cl-loop for rule
|
|
|
|
in (append doom-message-fg doom-message-bg doom-message-fx)
|
|
|
|
collect
|
|
|
|
`(,(car rule)
|
|
|
|
(lambda (message &rest args)
|
|
|
|
(apply #'doom-ansi-apply ',(car rule) message args))))
|
2017-06-23 13:06:28 +02:00
|
|
|
(color
|
|
|
|
(lambda (code format &rest args)
|
|
|
|
(apply #'doom-ansi-apply code format args))))
|
2017-04-12 10:51:29 -04:00
|
|
|
(format ,message ,@args)))
|
|
|
|
|
2018-05-20 11:44:06 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defmacro print! (message &rest args)
|
|
|
|
"Uses `message' in interactive sessions and `princ' otherwise (prints to
|
|
|
|
standard out).
|
|
|
|
|
|
|
|
Can be colored using (color ...) blocks:
|
|
|
|
|
|
|
|
(print! \"Hello %s %s\" (bold (blue \"How are you?\")))
|
|
|
|
(print! \"Hello %s %s\" (red \"World\"))
|
|
|
|
(print! (green \"Great %s!\" \"success\"))
|
|
|
|
|
|
|
|
Uses faces in interactive sessions and ANSI codes otherwise."
|
|
|
|
`(if (not noninteractive)
|
|
|
|
(message (format! ,message ,@args))
|
|
|
|
;; princ prints to stdout, message to stderr
|
|
|
|
(princ (format! ,message ,@args))
|
2018-05-24 18:30:36 +02:00
|
|
|
(terpri)))
|