From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48404) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fxDLB-0001p6-NK for guix-patches@gnu.org; Tue, 04 Sep 2018 11:34:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fxDL8-0005gj-6K for guix-patches@gnu.org; Tue, 04 Sep 2018 11:34:05 -0400 Received: from debbugs.gnu.org ([208.118.235.43]:40915) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fxDL8-0005gX-1q for guix-patches@gnu.org; Tue, 04 Sep 2018 11:34:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1fxDL7-0003Wd-Sx for guix-patches@gnu.org; Tue, 04 Sep 2018 11:34:01 -0400 Subject: [bug#32634] [PATCH 1/2] ui: Add support for colorization. References: <877ek1z3gg.fsf@elephly.net> In-Reply-To: <877ek1z3gg.fsf@elephly.net> Resent-Message-ID: From: Ricardo Wurmus Message-ID: <20180904153226.17555-1-rekado@elephly.net> Date: Tue, 4 Sep 2018 17:32:26 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+kyle=kyleam.com@gnu.org Sender: "Guix-patches" To: 32634@debbugs.gnu.org Cc: Sahithi Yarlagadda , Ricardo Wurmus From: Sahithi Yarlagadda * guix/ui.scm (ansi-color-tables): New variable. (color, colorize-string): New procedures. Signed-off-by: Ricardo Wurmus --- guix/ui.scm | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/guix/ui.scm b/guix/ui.scm index 29c0b2b9c..f8f2cad69 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -10,6 +10,8 @@ ;;; Copyright =C2=A9 2016 Roel Janssen ;;; Copyright =C2=A9 2016 Benz Schenk ;;; Copyright =C2=A9 2018 Kyle Meyer +;;; Copyright =C2=A9 2013, 2014 Free Software Foundation, Inc. +;;; Copyright =C2=A9 2018 Sahithi Yarlagadda ;;; ;;; This file is part of GNU Guix. ;;; @@ -115,7 +117,8 @@ guix-warning-port warning info - guix-main)) + guix-main + colorize-string)) =20 ;;; Commentary: ;;; @@ -1622,4 +1625,54 @@ and signal handling has already been set up." (initialize-guix) (apply run-guix args)) =20 +(define color-table + `((CLEAR . "0") + (RESET . "0") + (BOLD . "1") + (DARK . "2") + (UNDERLINE . "4") + (UNDERSCORE . "4") + (BLINK . "5") + (REVERSE . "6") + (CONCEALED . "8") + (BLACK . "30") + (RED . "31") + (GREEN . "32") + (YELLOW . "33") + (BLUE . "34") + (MAGENTA . "35") + (CYAN . "36") + (WHITE . "37") + (ON-BLACK . "40") + (ON-RED . "41") + (ON-GREEN . "42") + (ON-YELLOW . "43") + (ON-BLUE . "44") + (ON-MAGENTA . "45") + (ON-CYAN . "46") + (ON-WHITE . "47"))) + +(define (color . lst) + "Return a string containing the ANSI escape sequence for producing the +requested set of attributes in LST. Unknown attributes are ignored." + (let ((color-list + (remove not + (map (lambda (color) (assq-ref color-table color)) + lst)))) + (if (null? color-list) + "" + (string-append + (string #\esc #\[) + (string-join color-list ";" 'infix) + "m")))) + +(define (colorize-string str . color-list) + "Return a copy of STR colorized using ANSI escape sequences according to= the +attributes STR. At the end of the returned string, the color attributes w= ill +be reset such that subsequent output will not have any colors in effect." + (string-append + (apply color color-list) + str + (color 'RESET))) + ;;; ui.scm ends here --=20 2.18.0