Add break-lines to unstable/pretty.

This commit is contained in:
Vincent St-Amour 2011-07-12 15:17:37 -04:00
parent c5d46449b7
commit 3c91ae012a
2 changed files with 30 additions and 0 deletions

View File

@ -18,3 +18,22 @@
;; Ryan: There doesn't seem to be any 'format' going on. Perhaps call
;; them pretty-write-to-string, etc?
;; Bleh, just saw pretty-format in racket/pretty :(
;; by stamourv
(provide break-lines)
;; Takes a string, and breaks it into lines.
(define (break-lines s [columns (pretty-print-columns)])
(define res (open-output-string))
(for/fold ([len 0])
([word (in-list (regexp-split #px"[[:blank:]]+" s))])
(let ([new-len (+ len (string-length word) 1)])
(cond [(< new-len columns)
(display (format "~a~a" (if (= len 0) "" " ") word) res)
new-len]
[else ; break the line
(display (format "\n~a" word) res)
(string-length word)])))
(get-output-string res))

View File

@ -61,4 +61,15 @@ symmetry with @racket[pretty-format/write] and @racket[pretty-format/display].
}
@addition{@author+email["Vincent St-Amour" "stamourv@racket-lang.org"]}
@defproc[(break-lines [s string?] [columns exact-nonnegative-integer? (pretty-print-columns)])
string?]{
Splits the string @racket[s] into multiple lines, each of width at most
@racket[columns], splitting only at whitespace boundaries.
@examples[#:eval the-eval
(display (break-lines "This string is more than 80 characters long. It is 98 characters long, nothing more, nothing less."))
]
}
@(close-eval the-eval)