]> gitweb.factorcode.org Git - factor.git/blob - basis/wrap/wrap.factor
29a8bbf10fa881c3e004d9b329006006cf48eaf6
[factor.git] / basis / wrap / wrap.factor
1 USING: sequences kernel namespaces splitting math math.order ;
2 IN: wrap
3
4 ! Very stupid word wrapping/line breaking
5 ! This will be replaced by a Unicode-aware method,
6 ! which works with variable-width fonts
7
8 SYMBOL: width
9
10 : line-chunks ( string -- words-lines )
11     "\n" split [ " \t" split harvest ] map ;
12
13 : (split-chunk) ( words -- )
14     -1 over [ length + 1+ dup width get > ] find drop nip
15     [ 1 max cut-slice swap , (split-chunk) ] [ , ] if* ;
16
17 : split-chunk ( words -- lines )
18     [ (split-chunk) ] { } make ;
19
20 : join-spaces ( words-seqs -- lines )
21     [ [ " " join ] map ] map concat ;
22
23 : broken-lines ( string width -- lines )
24     width [
25         line-chunks [ split-chunk ] map join-spaces
26     ] with-variable ;
27
28 : line-break ( string width -- newstring )
29     broken-lines "\n" join ;
30
31 : indented-break ( string width indent -- newstring )
32     [ length - broken-lines ] keep [ prepend ] curry map "\n" join ;