1 ! Copyright (C) 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences splitting strings wrap ;
8 : wrap-split-line ( string -- elements )
9 dup [ " \t" member? not ] find drop 0 or
10 [ f swap ] [ cut ] if-zero
11 " \t" split harvest [ dup length 1 <element> ] map!
12 swap [ 0 over length <element> prefix ] when* ;
14 : wrap-split-lines ( string -- elements-lines )
15 split-lines [ wrap-split-line ] map! ;
17 : join-elements ( wrapped-lines -- lines )
22 : wrap-lines ( string width -- newlines )
23 [ wrap-split-lines ] dip '[ _ wrap join-elements ] map! concat ;
25 : wrap-string ( string width -- newstring )
26 wrap-lines join-lines ;
30 : make-indent ( indent -- indent' )
31 dup string? [ CHAR: \s <string> ] unless ; inline
35 : wrap-indented-string ( string width indent -- newstring )
36 make-indent [ length - wrap-lines ] keep
37 over empty? [ nip ] [ '[ _ prepend ] map! join-lines ] if ;