Pass leading and trailing tab characters in paragraph formatting.

This commit is contained in:
Dominik Pantůček 2023-03-28 22:25:19 +02:00
parent 5ceec258f9
commit 19daaf7706

View file

@ -149,25 +149,41 @@
(irregex-replace/all (irregex "\x1b\\[[0-9;]*[^0-9;]" 'u) str "")) (irregex-replace/all (irregex "\x1b\\[[0-9;]*[^0-9;]" 'u) str ""))
;; Formats string as paragraph of maximum given width while removing ;; Formats string as paragraph of maximum given width while removing
;; all ANSI CSI SGR from it. ;; all ANSI CSI SGR from it. If the first character is \t, align
;; right, if both first and last characters are \t, align center. The
;; alignment is not done here, but the \t are added to all lines
;; accordingly.
(define (ansi-paragraph-format str width) (define (ansi-paragraph-format str width)
(let loop ((words (string-split (let* ((strl (string->list str))
(ansi-remove str))) (first-char (car strl))
(res '(""))) (last-char (car (reverse strl)))
(if (null? words) (first-tab (eq? first-char #\tab))
(string-intersperse (reverse res) "\n") (last-tab (eq? last-char #\tab)))
(let* ((word (car words)) (let loop ((words (string-split
(wlen (ansi-string-length word)) (ansi-remove str)))
(llen (ansi-string-length (car res)))) (res '("")))
(loop (cdr words) (if (null? words)
(if (> (+ llen wlen 1) width) (string-intersperse
(cons word res) (reverse
(cons (string-append (car res) (map
(if (eq? (string-length (car res)) 0) (lambda (line)
"" (string-append (if first-tab "\t" "")
" ") line
word) (if last-tab "\t" "")))
(cdr res)))))))) res))
"\n")
(let* ((word (car words))
(wlen (ansi-string-length word))
(llen (ansi-string-length (car res))))
(loop (cdr words)
(if (> (+ llen wlen 1) width)
(cons word res)
(cons (string-append (car res)
(if (eq? (string-length (car res)) 0)
""
" ")
word)
(cdr res)))))))))
;; Returns a concatenation of all ANSI styles specified by this ;; Returns a concatenation of all ANSI styles specified by this
;; module ;; module