colors.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. color-rules
  37. color-output?
  38. isatty?*
  39. supports-hyperlinks?
  40. file-hyperlink
  41. hyperlink))
  42. ;;; Commentary:
  43. ;;;
  44. ;;; This module provides tools to produce colored output using ANSI escapes.
  45. ;;;
  46. ;;; Code:
  47. ;; Record type for "colors", which are actually lists of color attributes.
  48. (define-record-type <color>
  49. (make-color symbols ansi)
  50. color?
  51. (symbols color-symbols)
  52. (ansi color-ansi))
  53. (define (print-color color port)
  54. (format port "#<color ~a>"
  55. (string-join (map symbol->string
  56. (color-symbols color)))))
  57. (set-record-type-printer! <color> print-color)
  58. (define-syntax define-color-table
  59. (syntax-rules ()
  60. "Define NAME as a macro that builds a list of color attributes."
  61. ((_ name (color escape) ...)
  62. (begin
  63. (define-syntax color-codes
  64. (syntax-rules (color ...)
  65. ((_)
  66. '())
  67. ((_ color rest (... ...))
  68. `(escape ,@(color-codes rest (... ...))))
  69. ...))
  70. (define-syntax-rule (name colors (... ...))
  71. "Return a list of color attributes that can be passed to
  72. 'colorize-string'."
  73. (make-color '(colors (... ...))
  74. (color-codes->ansi (color-codes colors (... ...)))))))))
  75. (define-color-table color
  76. (CLEAR "0")
  77. (RESET "0")
  78. (BOLD "1")
  79. (DARK "2")
  80. (UNDERLINE "4")
  81. (UNDERSCORE "4")
  82. (BLINK "5")
  83. (REVERSE "6")
  84. (CONCEALED "8")
  85. (BLACK "30")
  86. (RED "31")
  87. (GREEN "32")
  88. (YELLOW "33")
  89. (BLUE "34")
  90. (MAGENTA "35")
  91. (CYAN "36")
  92. (WHITE "37")
  93. (ON-BLACK "40")
  94. (ON-RED "41")
  95. (ON-GREEN "42")
  96. (ON-YELLOW "43")
  97. (ON-BLUE "44")
  98. (ON-MAGENTA "45")
  99. (ON-CYAN "46")
  100. (ON-WHITE "47"))
  101. (define (color-codes->ansi codes)
  102. "Convert CODES, a list of color attribute codes, to a ANSI escape string."
  103. (match codes
  104. (()
  105. "")
  106. (_
  107. (string-append (string #\esc #\[)
  108. (string-join codes ";" 'infix)
  109. "m"))))
  110. (define %reset
  111. (color RESET))
  112. (define (colorize-string str color)
  113. "Return a copy of STR colorized using ANSI escape sequences according to
  114. COLOR. At the end of the returned string, the color attributes are reset such
  115. that subsequent output will not have any colors in effect."
  116. (string-append (color-ansi color)
  117. str
  118. (color-ansi %reset)))
  119. (define isatty?*
  120. (mlambdaq (port)
  121. "Return true if PORT is a tty. Memoize the result."
  122. (isatty? port)))
  123. (define (color-output? port)
  124. "Return true if we should write colored output to PORT."
  125. (and (not (getenv "NO_COLOR"))
  126. (isatty?* port)))
  127. (define (coloring-procedure color)
  128. "Return a procedure that applies COLOR to the given string."
  129. (lambda* (str #:optional (port (current-output-port)))
  130. "Return STR with extra ANSI color attributes if PORT supports it."
  131. (if (color-output? port)
  132. (colorize-string str color)
  133. str)))
  134. (define highlight (coloring-procedure (color BOLD)))
  135. (define highlight/warn (coloring-procedure (color BOLD MAGENTA)))
  136. (define dim (coloring-procedure (color DARK)))
  137. (define (colorize-matches rules)
  138. "Return a procedure that, when passed a string, returns that string
  139. colorized according to RULES. RULES must be a list of tuples like:
  140. (REGEXP COLOR1 COLOR2 ...)
  141. where COLOR1 specifies how to colorize the first submatch of REGEXP, and so
  142. on."
  143. (lambda (str)
  144. (if (string-index str #\nul)
  145. str
  146. (let loop ((rules rules))
  147. (match rules
  148. (()
  149. str)
  150. (((regexp . colors) . rest)
  151. (match (regexp-exec regexp str)
  152. (#f (loop rest))
  153. (m (let loop ((n 1)
  154. (colors colors)
  155. (result (list (match:prefix m))))
  156. (match colors
  157. (()
  158. (string-concatenate-reverse
  159. (cons (match:suffix m) result)))
  160. ((first . tail)
  161. (loop (+ n 1)
  162. tail
  163. (cons (colorize-string (match:substring m n)
  164. first)
  165. result)))))))))))))
  166. (define-syntax color-rules
  167. (syntax-rules ()
  168. "Return a procedure that colorizes the string it is passed according to
  169. the given rules. Each rule has the form:
  170. (REGEXP COLOR1 COLOR2 ...)
  171. where COLOR1 specifies how to colorize the first submatch of REGEXP, and so
  172. on."
  173. ((_ (regexp colors ...) ...)
  174. (colorize-matches `((,(make-regexp regexp) ,(color colors) ...)
  175. ...)))))
  176. ;;;
  177. ;;; Hyperlinks.
  178. ;;;
  179. (define (hyperlink uri text)
  180. "Return a string that denotes a hyperlink using an OSC escape sequence as
  181. documented at
  182. <https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda>."
  183. (string-append "\x1b]8;;" uri "\x1b\\"
  184. text "\x1b]8;;\x1b\\"))
  185. (define* (supports-hyperlinks? #:optional (port (current-output-port)))
  186. "Return true if PORT is a terminal that supports hyperlink escapes."
  187. ;; Note that terminals are supposed to ignore OSC escapes they don't
  188. ;; understand (this is the case of xterm as of version 349, for instance.)
  189. ;; However, Emacs comint as of 26.3 does not ignore it and instead lets it
  190. ;; through, hence the 'INSIDE_EMACS' special case below.
  191. (and (isatty?* port)
  192. (not (getenv "INSIDE_EMACS"))))
  193. (define* (file-hyperlink file #:optional (text file))
  194. "Return TEXT with escapes for a hyperlink to FILE."
  195. (hyperlink (string-append "file://" (gethostname)
  196. (encode-and-join-uri-path
  197. (string-split file #\/)))
  198. text))