2008-09-10 23:11:40 -04:00
|
|
|
USING: sequences kernel namespaces make splitting math math.order ;
|
2008-01-31 12:51:38 -05:00
|
|
|
IN: wrap
|
|
|
|
|
|
|
|
! Very stupid word wrapping/line breaking
|
|
|
|
! This will be replaced by a Unicode-aware method,
|
|
|
|
! which works with variable-width fonts
|
|
|
|
|
|
|
|
SYMBOL: width
|
|
|
|
|
|
|
|
: line-chunks ( string -- words-lines )
|
2008-05-14 00:36:55 -04:00
|
|
|
"\n" split [ " \t" split harvest ] map ;
|
2008-01-31 12:51:38 -05:00
|
|
|
|
|
|
|
: (split-chunk) ( words -- )
|
|
|
|
-1 over [ length + 1+ dup width get > ] find drop nip
|
2008-02-01 17:43:12 -05:00
|
|
|
[ 1 max cut-slice swap , (split-chunk) ] [ , ] if* ;
|
2008-01-31 12:51:38 -05:00
|
|
|
|
|
|
|
: split-chunk ( words -- lines )
|
|
|
|
[ (split-chunk) ] { } make ;
|
|
|
|
|
2008-02-01 17:43:12 -05:00
|
|
|
: join-spaces ( words-seqs -- lines )
|
|
|
|
[ [ " " join ] map ] map concat ;
|
|
|
|
|
2008-01-31 12:51:38 -05:00
|
|
|
: broken-lines ( string width -- lines )
|
|
|
|
width [
|
2008-02-01 17:43:12 -05:00
|
|
|
line-chunks [ split-chunk ] map join-spaces
|
2008-01-31 12:51:38 -05:00
|
|
|
] with-variable ;
|
|
|
|
|
|
|
|
: line-break ( string width -- newstring )
|
|
|
|
broken-lines "\n" join ;
|
|
|
|
|
|
|
|
: indented-break ( string width indent -- newstring )
|
2008-03-19 20:15:32 -04:00
|
|
|
[ length - broken-lines ] keep [ prepend ] curry map "\n" join ;
|