From f90041a0ff8f57b5c189992e1199ba7b9228948c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Tue, 14 Mar 2023 11:18:26 +0100 Subject: [PATCH] Actual ANSI color and bold attribute generation. --- ansi.scm | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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") )) )