Add UTF-8 support to ansi module.

This commit is contained in:
Dominik Pantůček 2023-03-22 20:00:18 +01:00
parent 471ea24874
commit 4093313e43
2 changed files with 9 additions and 5 deletions

View file

@ -43,6 +43,7 @@
(import scheme
(chicken base)
(chicken string)
(chicken irregex)
testing
utils)
@ -91,20 +92,20 @@
;; 1 - seen escape
;; 2 - CSI started
(define (ansi-string-length str)
(let loop ((lst (string->list str))
(let loop ((lst (irregex-extract (irregex "." 'u) str))
(state 0)
(len 0))
(if (null? lst)
len
(let ((ch (car lst)))
(case state
((0) (if (eq? ch #\escape)
((0) (if (equal? ch "\x1b")
(loop (cdr lst) 1 len)
(loop (cdr lst) 0 (add1 len))))
((1) (if (eq? ch #\[)
((1) (if (equal? ch "[")
(loop (cdr lst) 2 len)
(loop (cdr lst) 0 len)))
((2) (if (eq? ch #\m)
((2) (if (equal? ch "m")
(loop (cdr lst) 0 len)
(loop (cdr lst) 2 len))))))))
@ -118,6 +119,7 @@
(test-eq? ansi-string-length (ansi-string-length "test") 4)
(test-eq? ansi-string-length (ansi-string-length "\x1b[1mtest") 4)
(test-eq? ansi-string-length (ansi-string-length "\x1b[30mtest\x1b[0m") 4)
(test-eq? ansi-string-length (ansi-string-length "\x1b[30mščřž\x1b[0m") 4)
))
)