From 2d4a335d0262e59784912b6cd922ae9283f17cb4 Mon Sep 17 00:00:00 2001 From: Sahithi Yarlagadda Date: Wed, 13 Jun 2018 02:08:27 +0530 Subject: [PATCH] guix: Add coloring soft port. * guix/ui.scm (handle-string): New procedures. (colorful-build-output-port): New variable. guix: Added colorful-build-output-port to build.scm instead of default --- guix/scripts/build.scm | 4 +- guix/ui.scm | 101 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 75 insertions(+), 30 deletions(-) diff --git a/guix/scripts/build.scm b/guix/scripts/build.scm index 4dd4fbccd..f6924874d 100644 --- a/guix/scripts/build.scm +++ b/guix/scripts/build.scm @@ -733,9 +733,7 @@ needed." ;; Set the build options before we do anything else. (set-build-options-from-command-line store opts) - (parameterize ((current-build-output-port (if quiet? - (%make-void-port "w") - (current-error-port)))) + (parameterize ((current-build-output-port colorful-build-output-port)) (let* ((mode (assoc-ref opts 'build-mode)) (drv (options->derivations store opts)) (urls (map (cut string-append <> "/log") diff --git a/guix/ui.scm b/guix/ui.scm index 80f1a4d77..5ddc7c959 100644 --- a/guix/ui.scm +++ b/guix/ui.scm @@ -109,7 +109,7 @@ warning info guix-main - colorize-string)) + colorful-build-output-port)) ;;; Commentary: ;;; @@ -201,8 +201,8 @@ information, or #f if it could not be found." "Load the user provided Scheme source code FILE." (define (error-string frame args) (call-with-output-string - (lambda (port) - (apply display-error frame port (cdr args))))) + (lambda (port) + (apply display-error frame port (cdr args))))) (define tag (make-prompt-tag "user-code")) @@ -490,17 +490,17 @@ FILE." (augmented-system-error-handler file))))) (set! symlink - ;; We 'set!' the global binding because (gnu build ...) modules and similar - ;; typically don't use (guix ui). - (error-reporting-wrapper symlink (source target) target)) + ;; We 'set!' the global binding because (gnu build ...) modules and similar + ;; typically don't use (guix ui). + (error-reporting-wrapper symlink (source target) target)) (set! copy-file - ;; Note: here we use 'set!', not #:replace, because UIs typically use - ;; 'copy-recursively', which doesn't use (guix ui). - (error-reporting-wrapper copy-file (source target) target)) + ;; Note: here we use 'set!', not #:replace, because UIs typically use + ;; 'copy-recursively', which doesn't use (guix ui). + (error-reporting-wrapper copy-file (source target) target)) (set! canonicalize-path - (error-reporting-wrapper canonicalize-path (file) file)) + (error-reporting-wrapper canonicalize-path (file) file)) (define (make-regexp* regexp . flags) @@ -898,10 +898,10 @@ replacement if PORT is not Unicode-capable." (catch 'encoding-error (lambda () (call-with-output-string - (lambda (port) - (set-port-encoding! port encoding) - (set-port-conversion-strategy! port 'error) - (display arrow port)))) + (lambda (port) + (set-port-encoding! port encoding) + (set-port-conversion-strategy! port 'error) + (display arrow port)))) (lambda (key . args) "->")))) @@ -1086,7 +1086,7 @@ converted to a space; sequences of more than one line break are preserved." ;; 'texi-fragment->stexi' uses a string port so make sure it's a ;; Unicode-capable one (see .) (with-fluids ((%default-port-encoding "UTF-8")) - (stexi->plain-text (texi-fragment->stexi str)))) + (stexi->plain-text (texi-fragment->stexi str)))) (define (package-field-string package field-accessor) "Return a plain-text representation of PACKAGE field." @@ -1338,10 +1338,10 @@ DURATION-RELATION with the current time." ;; Return TIME at midnight by setting nanoseconds, seconds, minutes, and ;; hours to zeros. (let ((d (time-utc->date time))) - (date->time-utc - (make-date 0 0 0 0 - (date-day d) (date-month d) - (date-year d) (date-zone-offset d))))) + (date->time-utc + (make-date 0 0 0 0 + (date-day d) (date-month d) + (date-year d) (date-zone-offset d))))) (define generation-ctime-alist (map (lambda (number) @@ -1532,14 +1532,14 @@ found." (parameterize ((program-name command)) ;; Disable canonicalization so we don't don't stat unreasonably. (with-fluids ((%file-port-name-canonicalization #f)) - (dynamic-wind - (const #f) - (lambda () - (apply command-main args)) - (lambda () - ;; Abuse 'exit-hook' (which is normally meant to be used by the - ;; REPL) to run things like profiling hooks upon completion. - (run-hook exit-hook))))))) + (dynamic-wind + (const #f) + (lambda () + (apply command-main args)) + (lambda () + ;; Abuse 'exit-hook' (which is normally meant to be used by the + ;; REPL) to run things like profiling hooks upon completion. + (run-hook exit-hook))))))) (define (run-guix . args) "Run the 'guix' command defined by command line ARGS. @@ -1631,4 +1631,51 @@ be reset such that subsequent output will not have any colors in effect." str (color 'RESET))) +(define (handle-string str) + "Accepts input string(str) as argument and checks whether it matches with one +of the regular expressions specified. Upon matching, each substring is colorized +with corresponding colors and the modified colored string is returned. If the +input string fails match with the following conditionals it returns back the +unmodified input string." + + (let ((message (or (and=> (string-match "^(starting phase )(.*)" str) + (lambda (m) + (string-append + (colorize-string (match:substring m 1) 'BLUE) + (colorize-string (match:substring m 2) 'GREEN)))) + + (and=> (string-match "^(phase)(.*)(succeeded after)(.*)(seconds)" str) + (lambda (m) + (string-append + (colorize-string (match:substring m 1) 'BLUE) + (colorize-string (match:substring m 2) 'GREEN) + (colorize-string (match:substring m 3) 'BLUE) + (colorize-string (match:substring m 4) 'GREEN) + (colorize-string (match:substring m 5) 'BLUE)))) + + (and=> (string-match "^(phase)(.*)(failed after)(.*)(seconds)" str) + (lambda (m) + (string-append + (colorize-string (match:substring m 1) 'RED) + (colorize-string (match:substring m 2) 'GREEN) + (colorize-string (match:substring m 3) 'RED) + (colorize-string (match:substring m 4) 'GREEN) + (colorize-string (match:substring m 5) 'RED)))) + + ;; Didn’t match with any expression, returns back unmodified string. + str))) + (display message (current-error-port)))) + +(define colorful-build-output-port + (make-soft-port + (vector + (lambda (c) (write c (current-error-port))) + ;; procedure accepting one character for output + handle-string + ;; procedure accepting a string for handle-string procedure + (lambda () (display " " (current-error-port))) + (lambda () (char-upcase (read-char))) + (lambda () (display "@" (current-error-port)))) + "rw")) + ;;; ui.scm ends here -- 2.11.0