Actual ANSI color and bold attribute generation.

This commit is contained in:
Dominik Pantůček 2023-03-14 11:18:26 +01:00
parent d161233a8a
commit f90041a0ff

View file

@ -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")
))
)