diff --git a/ansi.scm b/ansi.scm index 04fe68a..9c04ad1 100644 --- a/ansi.scm +++ b/ansi.scm @@ -32,6 +32,7 @@ (import scheme (chicken base) + (chicken string) testing) ;; Only basic ANSI colors and bold attribute support. @@ -59,12 +60,30 @@ (loop (cdr lst) res))))) + ;; Returns ANSI sequence changing color and/or bold attribute. (define (ansi . args) - "") + (let ((argsl + (map + (lambda (key-color) + (number->string (cdr key-color))) + (filter + identity + (map (lambda (arg) (assq arg colors)) args))))) + (if (null? argsl) + "" + (string-append "\x1b[" + (string-intersperse argsl ";") + "m")))) + ;; Performs ANSI module self-tests. (define (ansi-tests!) (run-tests ansi + (test-equal? filter (filter odd? '(1 2 3 4)) '(1 3)) + (test-equal? filter (filter odd? '(2 4)) '()) + (test-equal? ansi (ansi #:red) "\x1b[31m") + (test-equal? ansi (ansi #:nonsense) "") + (test-equal? ansi (ansi #:default) "\x1b[0m") )) )