1 ! Copyright (C) 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: fry kernel math sequences splitting strings wrap ;
8 : 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 : split-lines ( string -- elements-lines )
15 lines [ split-line ] map! ;
17 : join-elements ( wrapped-lines -- lines )
20 : join-lines ( strings -- string )
25 : wrap-lines ( string width -- newlines )
26 [ split-lines ] dip '[ _ wrap join-elements ] map! concat ;
28 : wrap-string ( string width -- newstring )
29 wrap-lines join-lines ;
33 : make-indent ( indent -- indent' )
34 dup string? [ CHAR: \s <string> ] unless ; inline
38 : wrap-indented-string ( string width indent -- newstring )
39 make-indent [ length - wrap-lines ] keep
40 over empty? [ nip ] [ '[ _ prepend ] map! join-lines ] if ;