(use-modules (ice-9 match) ; need this for “match-lambda” (srfi srfi-1) (srfi srfi-26)) ; need this for “any” (define str "phase foo failed after 100 seconds") ;; Each list item in “patterns” is a regular expression followed by a ;; number of colours. (let ((patterns '(("^(starting phase )(.*)" BLUE GREEN) ("^(phase)(.*)(succeeded after)(.*)(seconds)" GREEN BLUE GREEN BLUE GREEN) ("^(phase)(.*)(failed after)(.*)(seconds)" RED BLUE RED BLUE RED)))) ;; See if “any” of the patterns matches, i.e. returns a string and ;; not just #f. We use “match-lambda” to bind the pattern to the ;; variable “pattern” and the list of colours to the variable ;; “colors”. (or (any (match-lambda ((pattern . colors) ;; TODO: use string-match, match:count, match:substring, ;; colorize-string, and=>, etc to see if a pattern matches ;; and to transform the string according to “colors”. ;; If the pattern does not match return #f to ;; automatically try the next, thanks to “any”. ;;(colorize-string (match:substring (match:count (string-match pattern str))) colors) (and=> (string-match pattern str) (lambda (m) ; (colorize-string (map match:substring m (iota (- (match:count m) 1) 1) colors))) ;(map (cut match:substring m <>) (iota (- (match:count m) 1) 1)))) (string-join (map (lambda (n color) (colorize-string (match:substring m n) color)) (iota (- (match:count m) 1) 1) colors)))) )) patterns) ;; Nothing matched, so return the string without changes. str))