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 Ludovic Courtès <ludo@gnu.org>
7 ;;; This file is part of GNU Guix.
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.
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.
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/>.
22 (define-module (guix colors)
23 #:use-module (guix memoization)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-9)
26 #:use-module (srfi srfi-9 gnu)
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 regex)
42 ;;; This module provides tools to produce colored output using ANSI escapes.
46 ;; Record type for "colors", which are actually lists of color attributes.
47 (define-record-type <color>
48 (make-color symbols ansi)
50 (symbols color-symbols)
53 (define (print-color color port)
54 (format port "#<color ~a>"
55 (string-join (map symbol->string
56 (color-symbols color)))))
58 (set-record-type-printer! <color> print-color)
60 (define-syntax define-color-table
62 "Define NAME as a macro that builds a list of color attributes."
63 ((_ name (color escape) ...)
65 (define-syntax color-codes
66 (syntax-rules (color ...)
69 ((_ color rest (... ...))
70 `(escape ,@(color-codes rest (... ...))))
73 (define-syntax-rule (name colors (... ...))
74 "Return a list of color attributes that can be passed to
76 (make-color '(colors (... ...))
77 (color-codes->ansi (color-codes colors (... ...)))))))))
79 (define-color-table color
106 (define (color-codes->ansi codes)
107 "Convert CODES, a list of color attribute codes, to a ANSI escape string."
112 (string-append (string #\esc #\[)
113 (string-join codes ";" 'infix)
119 (define (colorize-string str color)
120 "Return a copy of STR colorized using ANSI escape sequences according to
121 COLOR. At the end of the returned string, the color attributes are reset such
122 that subsequent output will not have any colors in effect."
123 (string-append (color-ansi color)
125 (color-ansi %reset)))
129 "Return true if PORT is a tty. Memoize the result."
132 (define (color-output? port)
133 "Return true if we should write colored output to PORT."
134 (and (not (getenv "INSIDE_EMACS"))
135 (not (getenv "NO_COLOR"))
138 (define (coloring-procedure color)
139 "Return a procedure that applies COLOR to the given string."
140 (lambda* (str #:optional (port (current-output-port)))
141 "Return STR with extra ANSI color attributes if PORT supports it."
142 (if (color-output? port)
143 (colorize-string str color)
146 (define highlight (coloring-procedure (color BOLD)))
147 (define dim (coloring-procedure (color DARK)))
149 (define (colorize-matches rules)
150 "Return a procedure that, when passed a string, returns that string
151 colorized according to RULES. RULES must be a list of tuples like:
153 (REGEXP COLOR1 COLOR2 ...)
155 where COLOR1 specifies how to colorize the first submatch of REGEXP, and so
158 (if (string-index str #\nul)
160 (let loop ((rules rules))
164 (((regexp . colors) . rest)
165 (match (regexp-exec regexp str)
169 (result (list (match:prefix m))))
172 (string-concatenate-reverse
173 (cons (match:suffix m) result)))
177 (cons (colorize-string (match:substring m n)
181 (define-syntax color-rules
183 "Return a procedure that colorizes the string it is passed according to
184 the given rules. Each rule has the form:
186 (REGEXP COLOR1 COLOR2 ...)
188 where COLOR1 specifies how to colorize the first submatch of REGEXP, and so
190 ((_ (regexp colors ...) ...)
191 (colorize-matches `((,(make-regexp regexp) ,(color colors) ...)