diff --git a/collects/unstable/pretty.rkt b/collects/unstable/pretty.rkt index b9fb48a668..1321ecac89 100644 --- a/collects/unstable/pretty.rkt +++ b/collects/unstable/pretty.rkt @@ -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)) diff --git a/collects/unstable/scribblings/pretty.scrbl b/collects/unstable/scribblings/pretty.scrbl index 2d4c35b59d..f394557c5e 100644 --- a/collects/unstable/scribblings/pretty.scrbl +++ b/collects/unstable/scribblings/pretty.scrbl @@ -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)