Add break-lines to unstable/pretty.
This commit is contained in:
parent
c5d46449b7
commit
3c91ae012a
|
@ -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))
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user