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 ""))
;; 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)
(let loop ((words (string-split
(ansi-remove str)))
(res '("")))
(if (null? words)
(string-intersperse (reverse 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))))))))
(let* ((strl (string->list str))
(first-char (car strl))
(last-char (car (reverse strl)))
(first-tab (eq? first-char #\tab))
(last-tab (eq? last-char #\tab)))
(let loop ((words (string-split
(ansi-remove str)))
(res '("")))
(if (null? words)
(string-intersperse
(reverse
(map
(lambda (line)
(string-append (if first-tab "\t" "")
line
(if last-tab "\t" "")))
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
;; module