! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
-USING: fry kernel math sequences splitting strings wrap ;
+USING: kernel math sequences splitting strings wrap ;
IN: wrap.strings
<PRIVATE
-: split-lines ( string -- elements-lines )
- string-lines [
- " \t" split harvest
- [ dup length 1 <element> ] map!
- ] map! ;
+: wrap-split-line ( string -- elements )
+ dup [ " \t" member? not ] find drop 0 or
+ [ f swap ] [ cut ] if-zero
+ " \t" split harvest [ dup length 1 <element> ] map!
+ swap [ 0 over length <element> prefix ] when* ;
-: join-elements ( wrapped-lines -- lines )
- [ " " join ] map! ;
+: wrap-split-lines ( string -- elements-lines )
+ split-lines [ wrap-split-line ] map! ;
-: join-lines ( strings -- string )
- "\n" join ;
+: join-elements ( wrapped-lines -- lines )
+ [ join-words ] map! ;
PRIVATE>
: wrap-lines ( string width -- newlines )
- [ split-lines ] dip '[ _ dup wrap join-elements ] map! concat ;
+ [ wrap-split-lines ] dip '[ _ wrap join-elements ] map! concat ;
: wrap-string ( string width -- newstring )
wrap-lines join-lines ;
: wrap-indented-string ( string width indent -- newstring )
make-indent [ length - wrap-lines ] keep
- '[ _ prepend ] map! join-lines ;
+ over empty? [ nip ] [ '[ _ prepend ] map! join-lines ] if ;