Pass leading and trailing tab characters in paragraph formatting.
This commit is contained in:
parent
5ceec258f9
commit
19daaf7706
1 changed files with 34 additions and 18 deletions
52
ansi.scm
52
ansi.scm
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue