1 USING: kernel sequences math arrays locals fry accessors splitting
2 make combinators.short-circuit namespaces grouping splitting.monotonic ;
7 ! black is the text length, white is the whitespace length
8 TUPLE: word contents black white ;
11 : word-length ( word -- n )
12 [ black>> ] [ white>> ] bi + ;
14 TUPLE: cons cdr car ; ! This order works out better
17 : >cons< ( cons -- cdr car )
18 [ cdr>> ] [ car>> ] bi ;
20 : list-each ( list quot -- )
22 [ [ car>> ] dip call ]
23 [ [ cdr>> ] dip list-each ] 2bi
24 ] [ 2drop ] if ; inline recursive
26 : singleton? ( list -- ? )
27 { [ ] [ cdr>> not ] } 1&& ;
29 : <singleton> ( elt -- list )
32 : list>array ( list -- array )
33 [ [ , ] list-each ] { } make ;
35 : lists>arrays ( lists -- arrays )
36 [ [ list>array , ] list-each ] { } make ;
38 TUPLE: paragraph lines head-width tail-cost ;
39 C: <paragraph> paragraph
44 : deviation ( length -- n )
47 : top-fits? ( paragraph -- ? )
49 [ lines>> singleton? line-ideal line-max ? get ] bi <= ;
51 : fits? ( paragraph -- ? )
52 ! Make this not count spaces at end
53 { [ lines>> car>> singleton? ] [ top-fits? ] } 1|| ;
55 :: min-by ( seq quot -- elt )
56 f 1.0/0.0 seq [| key value new |
57 new quot call :> newvalue
58 newvalue value < [ new newvalue ] [ key value ] if
61 : paragraph-cost ( paragraph -- cost )
62 [ head-width>> deviation ]
63 [ tail-cost>> ] bi + ;
65 : min-cost ( paragraphs -- paragraph )
66 [ paragraph-cost ] min-by ;
68 : new-line ( paragraph word -- paragraph )
69 [ [ lines>> ] [ <singleton> ] bi* <cons> ]
71 [ drop paragraph-cost ] 2tri
74 : glue ( paragraph word -- paragraph )
75 [ [ lines>> >cons< ] dip <cons> <cons> ]
76 [ [ head-width>> ] [ word-length ] bi* + ]
77 [ drop tail-cost>> ] 2tri
80 : wrap-step ( paragraphs word -- paragraphs )
82 [ [ min-cost ] dip new-line ]
86 : 1paragraph ( word -- paragraph )
87 [ <singleton> <singleton> ]
91 : post-process ( paragraph -- array )
93 [ [ contents>> ] map ] map ;
95 : initialize ( words -- words paragraph )
96 <reversed> unclip-slice 1paragraph 1array ;
98 : wrap ( words line-max line-ideal -- paragraph )
110 TUPLE: element key width break? ;
115 : elements-length ( elements -- length )
116 [ width>> ] map sum ;
118 : make-word ( whites blacks -- word )
119 [ append ] [ [ elements-length ] bi@ ] 2bi <word> ;
121 : ?first2 ( seq -- first/f second/f )
125 : split-elements ( seq -- half-words )
126 [ [ break?>> ] bi@ = ] monotonic-split ;
128 : ?first-break ( seq -- newseq f/word )
129 dup first first break?>>
130 [ unclip-slice f swap make-word ]
133 : make-words ( seq f/word -- words )
134 [ 2 <groups> [ ?first2 make-word ] map ] dip
137 : elements>words ( seq -- newseq )
138 split-elements ?first-break make-words ;
142 : wrap-elements ( elements line-max line-ideal -- lines )
143 [ elements>words ] 2dip wrap [ concat ] map ;
147 : split-lines ( string -- words-lines )
150 [ dup length 1 <word> ] map
153 : join-words ( wrapped-lines -- lines )
156 : join-lines ( strings -- string )
161 : wrap-lines ( lines width -- newlines )
162 [ split-lines ] dip '[ _ dup wrap join-words ] map concat ;
164 : wrap-string ( string width -- newstring )
165 wrap-lines join-lines ;
167 : wrap-indented-string ( string width indent -- newstring )
168 [ length - wrap-lines ] keep '[ _ prepend ] map join-lines ;