ui: Colorize diagnostics.

* guix/ui.scm (define-diagnostic): Add 'colors' parameter and pass it to
'print-diagnostic-prefix'.
(warning, info, report-error): Add extra argument.
(%warning-colors, %info-colors, %error-colors): New variables.
(print-diagnostic-prefix): Add #:colors parameter and honor it.
master
Ludovic Courtès 2019-04-10 12:00:55 +02:00
parent 402627714b
commit 9e1e046040
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5
1 changed files with 33 additions and 9 deletions

View File

@ -29,6 +29,7 @@
(define-module (guix ui) (define-module (guix ui)
#:use-module (guix i18n) #:use-module (guix i18n)
#:use-module (guix colors)
#:use-module (guix gexp) #:use-module (guix gexp)
#:use-module (guix sets) #:use-module (guix sets)
#:use-module (guix utils) #:use-module (guix utils)
@ -128,7 +129,7 @@
(syntax-rules () (syntax-rules ()
"Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all "Create a diagnostic macro (i.e., NAME), which will prepend PREFIX to all
messages." messages."
((_ name (G_ prefix)) ((_ name (G_ prefix) colors)
(define-syntax name (define-syntax name
(lambda (x) (lambda (x)
(syntax-case x () (syntax-case x ()
@ -136,7 +137,8 @@ messages."
(and (string? (syntax->datum #'fmt)) (and (string? (syntax->datum #'fmt))
(free-identifier=? #'underscore #'G_)) (free-identifier=? #'underscore #'G_))
#'(begin #'(begin
(print-diagnostic-prefix prefix location) (print-diagnostic-prefix prefix location
#:colors colors)
(format (guix-warning-port) (gettext fmt %gettext-domain) (format (guix-warning-port) (gettext fmt %gettext-domain)
args (... ...)))) args (... ...))))
((name location (N-underscore singular plural n) ((name location (N-underscore singular plural n)
@ -145,7 +147,8 @@ messages."
(string? (syntax->datum #'plural)) (string? (syntax->datum #'plural))
(free-identifier=? #'N-underscore #'N_)) (free-identifier=? #'N-underscore #'N_))
#'(begin #'(begin
(print-diagnostic-prefix prefix location) (print-diagnostic-prefix prefix location
#:colors colors)
(format (guix-warning-port) (format (guix-warning-port)
(ngettext singular plural n %gettext-domain) (ngettext singular plural n %gettext-domain)
args (... ...)))) args (... ...))))
@ -161,26 +164,47 @@ messages."
;; XXX: This doesn't work well for right-to-left languages. ;; XXX: This doesn't work well for right-to-left languages.
;; TRANSLATORS: The goal is to emit "warning:" followed by a short phrase; ;; TRANSLATORS: The goal is to emit "warning:" followed by a short phrase;
;; "~a" is a placeholder for that phrase. ;; "~a" is a placeholder for that phrase.
(define-diagnostic warning (G_ "warning: ")) ;emit a warning (define-diagnostic warning (G_ "warning: ") %warning-colors) ;emit a warning
(define-diagnostic info (G_ "")) (define-diagnostic info (G_ "") %info-colors)
(define-diagnostic report-error (G_ "error: ") %error-colors)
(define-diagnostic report-error (G_ "error: "))
(define-syntax-rule (leave args ...) (define-syntax-rule (leave args ...)
"Emit an error message and exit." "Emit an error message and exit."
(begin (begin
(report-error args ...) (report-error args ...)
(exit 1))) (exit 1)))
(define* (print-diagnostic-prefix prefix #:optional location) (define %warning-colors '(BOLD MAGENTA))
(define %info-colors '(BOLD CYAN))
(define %error-colors '(BOLD RED))
(define* (print-diagnostic-prefix prefix #:optional location
#:key (colors '()))
"Print PREFIX as a diagnostic line prefix." "Print PREFIX as a diagnostic line prefix."
(define color?
(color-output? (guix-warning-port)))
(define location-color
(if color?
(cut colorize-string <> 'BOLD)
identity))
(define prefix-color
(if color?
(lambda (prefix)
(apply colorize-string prefix colors))
identity))
(let ((prefix (if (string-null? prefix) (let ((prefix (if (string-null? prefix)
prefix prefix
(gettext prefix %gettext-domain)))) (gettext prefix %gettext-domain))))
(if location (if location
(format (guix-warning-port) "~a: ~a" (format (guix-warning-port) "~a: ~a"
(location->string location) prefix) (location-color (location->string location))
(prefix-color prefix))
(format (guix-warning-port) "~:[~*~;guix ~a: ~]~a" (format (guix-warning-port) "~:[~*~;guix ~a: ~]~a"
(program-name) (program-name) prefix)))) (program-name) (program-name)
(prefix-color prefix)))))
(define (print-unbound-variable-error port key args default-printer) (define (print-unbound-variable-error port key args default-printer)
;; Print unbound variable errors more nicely, and in the right language. ;; Print unbound variable errors more nicely, and in the right language.