colors.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014 Free Software Foundation, Inc.
  3. ;;; Copyright © 2018 Sahithi Yarlagadda <sahi@swecha.net>
  4. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  5. ;;; Copyright © 2017, 2018, 2019, 2022 Ludovic Courtès <ludo@gnu.org>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix colors)
  22. #:use-module (guix memoization)
  23. #:use-module (srfi srfi-1)
  24. #:use-module (srfi srfi-9)
  25. #:use-module (srfi srfi-9 gnu)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 regex)
  28. #:autoload (web uri) (encode-and-join-uri-path)
  29. #:export (color
  30. color?
  31. coloring-procedure
  32. colorize-string
  33. highlight
  34. highlight/warn
  35. dim
  36. colorize-full-matches
  37. color-rules
  38. color-output?
  39. isatty?*
  40. supports-hyperlinks?
  41. file-hyperlink
  42. hyperlink))
  43. ;;; Commentary:
  44. ;;;
  45. ;;; This module provides tools to produce colored output using ANSI escapes.
  46. ;;;
  47. ;;; Code:
  48. ;; Record type for "colors", which are actually lists of color attributes.
  49. (define-record-type <color>
  50. (make-color symbols ansi)
  51. color?
  52. (symbols color-symbols)
  53. (ansi color-ansi))
  54. (define (print-color color port)
  55. (format port "#<color ~a>"
  56. (string-join (map symbol->string
  57. (color-symbols color)))))
  58. (set-record-type-printer! <color> print-color)
  59. (define-syntax define-color-table
  60. (syntax-rules ()
  61. "Define NAME as a macro that builds a list of color attributes."
  62. ((_ name (color escape) ...)
  63. (begin
  64. (define-syntax color-codes
  65. (syntax-rules (color ...)
  66. ((_)
  67. '())
  68. ((_ color rest (... ...))
  69. `(escape ,@(color-codes rest (... ...))))
  70. ...))
  71. (define-syntax-rule (name colors (... ...))
  72. "Return a list of color attributes that can be passed to
  73. 'colorize-string'."
  74. (make-color '(colors (... ...))
  75. (color-codes->ansi (color-codes colors (... ...)))))))))
  76. (define-color-table color
  77. (CLEAR "0")
  78. (RESET "0")
  79. (BOLD "1")
  80. (DARK "2")
  81. (UNDERLINE "4")
  82. (UNDERSCORE "4")
  83. (BLINK "5")
  84. (REVERSE "6")
  85. (CONCEALED "8")
  86. (BLACK "30")
  87. (RED "31")
  88. (GREEN "32")
  89. (YELLOW "33")
  90. (BLUE "34")
  91. (MAGENTA "35")
  92. (CYAN "36")
  93. (WHITE "37")
  94. (ON-BLACK "40")
  95. (ON-RED "41")
  96. (ON-GREEN "42")
  97. (ON-YELLOW "43")
  98. (ON-BLUE "44")
  99. (ON-MAGENTA "45")
  100. (ON-CYAN "46")
  101. (ON-WHITE "47"))
  102. (define (color-codes->ansi codes)
  103. "Convert CODES, a list of color attribute codes, to a ANSI escape string."
  104. (match codes
  105. (()
  106. "")
  107. (_
  108. (string-append (string #\esc #\[)
  109. (string-join codes ";" 'infix)
  110. "m"))))
  111. (define %reset
  112. (color RESET))
  113. (define (colorize-string str color)
  114. "Return a copy of STR colorized using ANSI escape sequences according to
  115. COLOR. At the end of the returned string, the color attributes are reset such
  116. that subsequent output will not have any colors in effect."
  117. (string-append (color-ansi color)
  118. str
  119. (color-ansi %reset)))
  120. (define isatty?*
  121. (mlambdaq (port)
  122. "Return true if PORT is a tty. Memoize the result."
  123. (isatty? port)))
  124. (define (color-output? port)
  125. "Return true if we should write colored output to PORT."
  126. (and (not (getenv "NO_COLOR"))
  127. (isatty?* port)))
  128. (define (coloring-procedure color)
  129. "Return a procedure that applies COLOR to the given string."
  130. (lambda* (str #:optional (port (current-output-port)))
  131. "Return STR with extra ANSI color attributes if PORT supports it."
  132. (if (color-output? port)
  133. (colorize-string str color)
  134. str)))
  135. (define highlight (coloring-procedure (color BOLD)))
  136. (define highlight/warn (coloring-procedure (color BOLD MAGENTA)))
  137. (define dim (coloring-procedure (color DARK)))
  138. (define (colorize-full-matches rules)
  139. "Return a procedure that, given a string, colorizes according to RULES.
  140. RULES must be a list of regexp/color pairs; the whole match of a regexp is
  141. colorized with the corresponding color."
  142. (define proc
  143. (lambda (str)
  144. (if (string-index str #\nul)
  145. str
  146. (let loop ((rules rules))
  147. (match rules
  148. (()
  149. str)
  150. (((regexp . color) . rest)
  151. (match (regexp-exec regexp str)
  152. (#f (loop rest))
  153. (m (string-append (proc (match:prefix m))
  154. (colorize-string (match:substring m)
  155. color)
  156. (proc (match:suffix m)))))))))))
  157. proc)
  158. (define (colorize-matches rules)
  159. "Return a procedure that, when passed a string, returns that string
  160. colorized according to RULES. RULES must be a list of tuples like:
  161. (REGEXP COLOR1 COLOR2 ...)
  162. where COLOR1 specifies how to colorize the first submatch of REGEXP, and so
  163. on."
  164. (lambda (str)
  165. (if (string-index str #\nul)
  166. str
  167. (let loop ((rules rules))
  168. (match rules
  169. (()
  170. str)
  171. (((regexp . colors) . rest)
  172. (match (regexp-exec regexp str)
  173. (#f (loop rest))
  174. (m (let loop ((n 1)
  175. (colors colors)
  176. (result (list (match:prefix m))))
  177. (match colors
  178. (()
  179. (string-concatenate-reverse
  180. (cons (match:suffix m) result)))
  181. ((first . tail)
  182. (loop (+ n 1)
  183. tail
  184. (cons (colorize-string (match:substring m n)
  185. first)
  186. result)))))))))))))
  187. (define-syntax color-rules
  188. (syntax-rules ()
  189. "Return a procedure that colorizes the string it is passed according to
  190. the given rules. Each rule has the form:
  191. (REGEXP COLOR1 COLOR2 ...)
  192. where COLOR1 specifies how to colorize the first submatch of REGEXP, and so
  193. on."
  194. ((_ (regexp colors ...) ...)
  195. (colorize-matches `((,(make-regexp regexp) ,(color colors) ...)
  196. ...)))))
  197. ;;;
  198. ;;; Hyperlinks.
  199. ;;;
  200. (define (hyperlink uri text)
  201. "Return a string that denotes a hyperlink using an OSC escape sequence as
  202. documented at
  203. <https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda>."
  204. (string-append "\x1b]8;;" uri "\x1b\\"
  205. text "\x1b]8;;\x1b\\"))
  206. (define* (supports-hyperlinks? #:optional (port (current-output-port)))
  207. "Return true if PORT is a terminal that supports hyperlink escapes."
  208. ;; Note that terminals are supposed to ignore OSC escapes they don't
  209. ;; understand (this is the case of xterm as of version 349, for instance.)
  210. ;; However, Emacs comint as of 26.3 does not ignore it and instead lets it
  211. ;; through, hence the 'INSIDE_EMACS' special case below.
  212. (and (isatty?* port)
  213. (not (getenv "INSIDE_EMACS"))))
  214. (define* (file-hyperlink file #:optional (text file))
  215. "Return TEXT with escapes for a hyperlink to FILE."
  216. (hyperlink (string-append "file://" (gethostname)
  217. (encode-and-join-uri-path
  218. (string-split file #\/)))
  219. text))