1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: hashtables io io.streams.plain io.streams.string
4 colors summary make accessors splitting math.order
5 kernel namespaces assocs destructors strings sequences ;
8 GENERIC: stream-format ( str style stream -- )
9 GENERIC: make-span-stream ( style stream -- stream' )
10 GENERIC: make-block-stream ( style stream -- stream' )
11 GENERIC: make-cell-stream ( style stream -- stream' )
12 GENERIC: stream-write-table ( table-cells style stream -- )
14 : format ( str style -- ) output-stream get stream-format ;
16 : tabular-output ( style quot -- )
17 swap [ { } make ] dip output-stream get stream-write-table ; inline
19 : with-row ( quot -- )
22 : with-cell ( quot -- )
23 H{ } output-stream get make-cell-stream
24 [ swap with-output-stream ] keep , ; inline
26 : write-cell ( str -- )
27 [ write ] with-cell ; inline
29 : with-style ( style quot -- )
30 swap dup assoc-empty? [
33 output-stream get make-span-stream swap with-output-stream
36 : with-nesting ( style quot -- )
37 [ output-stream get make-block-stream ] dip
38 with-output-stream ; inline
40 TUPLE: filter-writer stream ;
42 M: filter-writer stream-format
43 stream>> stream-format ;
45 M: filter-writer stream-write
46 stream>> stream-write ;
48 M: filter-writer stream-write1
49 stream>> stream-write1 ;
51 M: filter-writer make-span-stream
52 stream>> make-span-stream ;
54 M: filter-writer make-block-stream
55 stream>> make-block-stream ;
57 M: filter-writer make-cell-stream
58 stream>> make-cell-stream ;
60 M: filter-writer stream-flush
61 stream>> stream-flush ;
63 M: filter-writer stream-nl
66 M: filter-writer stream-write-table
67 stream>> stream-write-table ;
69 M: filter-writer dispose
72 TUPLE: ignore-close-stream < filter-writer ;
74 M: ignore-close-stream dispose drop ;
76 C: <ignore-close-stream> ignore-close-stream
78 TUPLE: style-stream < filter-writer style ;
80 : do-nested-style ( style style-stream -- style stream )
81 [ style>> swap assoc-union ] [ stream>> ] bi ; inline
83 C: <style-stream> style-stream
85 M: style-stream stream-format
86 do-nested-style stream-format ;
88 M: style-stream stream-write
89 [ style>> ] [ stream>> ] bi stream-format ;
91 M: style-stream stream-write1
92 [ 1string ] dip stream-write ;
94 M: style-stream make-span-stream
95 do-nested-style make-span-stream ;
97 M: style-stream make-block-stream
98 [ do-nested-style make-block-stream ] [ style>> ] bi
101 M: style-stream make-cell-stream
102 [ do-nested-style make-cell-stream ] [ style>> ] bi
105 M: style-stream stream-write-table
106 [ [ [ stream>> ] map ] map ] [ ] [ stream>> ] tri*
109 M: plain-writer stream-format
112 M: plain-writer make-span-stream
113 swap <style-stream> <ignore-close-stream> ;
115 M: plain-writer make-block-stream
116 nip <ignore-close-stream> ;
118 : format-column ( seq ? -- seq )
120 [ 0 [ length max ] reduce ] keep
121 swap [ CHAR: \s pad-tail ] curry map
124 : map-last ( seq quot -- seq )
125 [ dup length <reversed> ] dip [ 0 = ] prepose 2map ; inline
127 : format-table ( table -- seq )
128 flip [ format-column ] map-last
129 flip [ " " join ] map ;
131 M: plain-writer stream-write-table
132 [ drop format-table [ print ] each ] with-output-stream* ;
134 M: plain-writer make-cell-stream 2drop <string-writer> ;
151 SYMBOL: presented-path
152 SYMBOL: presented-printer
166 : standard-table-style ( -- style )
168 { table-gap { 5 5 } }
169 { table-border T{ rgba f 0.8 0.8 0.8 1.0 } }
173 TUPLE: input string ;
180 string>> "\n" split1 swap %
184 : write-object ( str obj -- ) presented associate format ;