1 USING: sequences kernel namespaces make splitting
2 math math.order fry assocs accessors ;
5 ! Word wrapping/line breaking -- not Unicode-aware
7 TUPLE: word key width break? ;
15 : break-here? ( column word -- ? )
16 break?>> not [ width get > ] [ drop f ] if ;
18 : find-optimal-break ( words -- n )
19 [ 0 ] dip [ [ width>> + dup ] keep break-here? ] find drop nip ;
22 dup find-optimal-break
23 [ 1 max cut-slice [ , ] [ (wrap) ] bi* ] [ , ] if* ;
25 : intersperse ( seq elt -- seq' )
26 [ '[ _ , ] [ , ] interleave ] { } make ;
28 : split-lines ( string -- words-lines )
31 [ dup length f <word> ] map
32 " " 1 t <word> intersperse
35 : join-words ( wrapped-lines -- lines )
39 [ trim-tail-slice ] bi
43 : join-lines ( strings -- string )
48 : wrap ( words width -- lines )
53 : wrap-lines ( lines width -- newlines )
54 [ split-lines ] dip '[ _ wrap join-words ] map concat ;
56 : wrap-string ( string width -- newstring )
57 wrap-lines join-lines ;
59 : wrap-indented-string ( string width indent -- newstring )
60 [ length - wrap-lines ] keep '[ _ prepend ] map join-lines ;