Implement nice ansi formatting wrapper.

This commit is contained in:
Dominik Pantůček 2023-03-24 15:42:31 +01:00
parent 802a642965
commit cf86180222

View file

@ -38,6 +38,7 @@
a:highlight
ansi-string-length
ansi-paragraph-format
ansi-string
ansi-tests!
)
@ -45,6 +46,7 @@
(chicken base)
(chicken string)
(chicken irregex)
(chicken keyword)
testing
utils)
@ -135,6 +137,29 @@
word)
(cdr res))))))))
;; Returns a concatenation of all ANSI styles specified by this
;; module
(define (ansi-string . args)
(apply string-append
(let loop ((args args)
(kws '())
(res '()))
(if (null? args)
(let ((rres (if (null? kws)
res
(cons (apply ansi kws) res))))
(reverse rres))
(let ((arg (car args)))
(loop (cdr args)
(if (keyword? arg)
(cons arg kws)
'())
(if (keyword? arg)
res
(if (null? kws)
(cons arg res)
(cons arg (cons (apply ansi (reverse kws)) res))))))))))
;; Performs ANSI module self-tests.
(define (ansi-tests!)
(run-tests
@ -154,6 +179,12 @@
(test-equal? ansi-paragraph-format
(ansi-paragraph-format "Formats string as paragraph of maximum given width" 20)
"Formats string as\nparagraph of maximum\ngiven width")
(test-equal? ansi-string
(ansi-string "Hello" #:bold "World")
"Hello\x1b[1mWorld")
(test-equal? ansi-string
(ansi-string "Hello" #:bold #:red "World")
"Hello\x1b[1;31mWorld")
))
)