1 USING: kernel sequences math arrays locals fry accessors
2 lists splitting call make combinators.short-circuit namespaces
3 grouping splitting.monotonic ;
8 ! black is the text length, white is the whitespace length
9 TUPLE: element contents black white ;
12 : element-length ( element -- n )
13 [ black>> ] [ white>> ] bi + ;
15 : swons ( cdr car -- cons )
18 : unswons ( cons -- cdr car )
21 : 1list? ( list -- ? )
22 { [ ] [ cdr +nil+ = ] } 1&& ;
24 : lists>arrays ( lists -- arrays )
25 [ list>seq ] lmap>array ;
27 TUPLE: paragraph lines head-width tail-cost ;
28 C: <paragraph> paragraph
33 : deviation ( length -- n )
36 : top-fits? ( paragraph -- ? )
38 [ lines>> 1list? line-ideal line-max ? get ] bi <= ;
40 : fits? ( paragraph -- ? )
41 ! Make this not count spaces at end
42 { [ lines>> car 1list? ] [ top-fits? ] } 1|| ;
44 :: min-by ( seq quot -- elt )
45 f 1.0/0.0 seq [| key value new |
46 new quot call :> newvalue
47 newvalue value < [ new newvalue ] [ key value ] if
50 : paragraph-cost ( paragraph -- cost )
51 [ head-width>> deviation ]
52 [ tail-cost>> ] bi + ;
54 : min-cost ( paragraphs -- paragraph )
55 [ paragraph-cost ] min-by ;
57 : new-line ( paragraph element -- paragraph )
58 [ [ lines>> ] [ 1list ] bi* swons ]
60 [ drop paragraph-cost ] 2tri
63 : glue ( paragraph element -- paragraph )
64 [ [ lines>> unswons ] dip swons swons ]
65 [ [ head-width>> ] [ element-length ] bi* + ]
66 [ drop tail-cost>> ] 2tri
69 : wrap-step ( paragraphs element -- paragraphs )
71 [ [ min-cost ] dip new-line ]
75 : 1paragraph ( element -- paragraph )
80 : post-process ( paragraph -- array )
82 [ [ contents>> ] map ] map ;
84 : initialize ( elements -- elements paragraph )
85 <reversed> unclip-slice 1paragraph 1array ;
87 : wrap ( elements line-max line-ideal -- paragraph )
99 TUPLE: segment key width break? ;
104 : segments-length ( segments -- length )
105 [ width>> ] map sum ;
107 : make-element ( whites blacks -- element )
108 [ append ] [ [ segments-length ] bi@ ] 2bi <element> ;
110 : ?first2 ( seq -- first/f second/f )
114 : split-segments ( seq -- half-elements )
115 [ [ break?>> ] bi@ = ] monotonic-split ;
117 : ?first-break ( seq -- newseq f/element )
118 dup first first break?>>
119 [ unclip-slice f swap make-element ]
122 : make-elements ( seq f/element -- elements )
123 [ 2 <groups> [ ?first2 make-element ] map ] dip
126 : segments>elements ( seq -- newseq )
127 split-segments ?first-break make-elements ;
131 : wrap-segments ( segments line-max line-ideal -- lines )
132 [ segments>elements ] 2dip wrap [ concat ] map ;
136 : split-lines ( string -- elements-lines )
139 [ dup length 1 <element> ] map
142 : join-elements ( wrapped-lines -- lines )
145 : join-lines ( strings -- string )
150 : wrap-lines ( lines width -- newlines )
151 [ split-lines ] dip '[ _ dup wrap join-elements ] map concat ;
153 : wrap-string ( string width -- newstring )
154 wrap-lines join-lines ;
156 : wrap-indented-string ( string width indent -- newstring )
157 [ length - wrap-lines ] keep '[ _ prepend ] map join-lines ;