[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
01/01: ui: Add support for colorization.
From: |
Ricardo Wurmus |
Subject: |
01/01: ui: Add support for colorization. |
Date: |
Sat, 26 May 2018 17:17:45 -0400 (EDT) |
rekado pushed a commit to branch wip-sahithi
in repository guix.
commit 0321f49ddcf67530c1843c1816e7f6084356ef38
Author: Sahithi Yarlagadda <address@hidden>
Date: Sat May 26 17:34:23 2018 +0530
ui: Add support for colorization.
* guix/ui.scm (ansi-color-tables): New variable.
(color, colorize-string): New procedures.
Signed-off-by: Ricardo Wurmus <address@hidden>
---
guix/ui.scm | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 54 insertions(+), 1 deletion(-)
diff --git a/guix/ui.scm b/guix/ui.scm
index 8d35160..80f1a4d 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -9,6 +9,8 @@
;;; Copyright © 2015, 2016 Mathieu Lirzin <address@hidden>
;;; Copyright © 2016 Roel Janssen <address@hidden>
;;; Copyright © 2016 Benz Schenk <address@hidden>
+;;; Copyright © 2013, 2014 Free Software Foundation, Inc.
+;;; Copyright © 2018 Sahithi Yarlagadda <address@hidden>
;;;
;;; This file is part of GNU Guix.
;;;
@@ -106,7 +108,8 @@
guix-warning-port
warning
info
- guix-main))
+ guix-main
+ colorize-string))
;;; Commentary:
;;;
@@ -1578,4 +1581,54 @@ and signal handling has already been set up."
(initialize-guix)
(apply run-guix args))
+(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 will
+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