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
6 present fry strings.tables delegate delegate.protocols ;
9 GENERIC: stream-format ( str style stream -- )
10 GENERIC: make-span-stream ( style stream -- stream' )
11 GENERIC: make-block-stream ( style stream -- stream' )
12 GENERIC: make-cell-stream ( style stream -- stream' )
13 GENERIC: stream-write-table ( table-cells style stream -- )
15 PROTOCOL: formatted-output-stream-protocol
16 stream-format make-span-stream make-block-stream
17 make-cell-stream stream-write-table ;
19 : format ( str style -- ) output-stream get stream-format ;
21 : tabular-output ( style quot -- )
22 swap [ { } make ] dip output-stream get stream-write-table ; inline
24 : with-row ( quot -- )
27 : with-cell ( quot -- )
28 H{ } output-stream get make-cell-stream
29 [ swap with-output-stream ] keep , ; inline
31 : write-cell ( str -- )
32 [ write ] with-cell ; inline
34 : with-style ( style quot -- )
35 swap dup assoc-empty? [
38 output-stream get make-span-stream swap with-output-stream
41 : with-nesting ( style quot -- )
42 [ output-stream get make-block-stream ] dip
43 with-output-stream ; inline
45 TUPLE: filter-writer stream ;
47 CONSULT: output-stream-protocol filter-writer stream>> ;
49 CONSULT: formatted-output-stream-protocol filter-writer stream>> ;
51 M: filter-writer stream-element-type stream>> stream-element-type ;
53 M: filter-writer dispose stream>> dispose ;
55 TUPLE: ignore-close-stream < filter-writer ;
57 M: ignore-close-stream dispose drop ;
59 C: <ignore-close-stream> ignore-close-stream
61 TUPLE: style-stream < filter-writer style ;
63 : do-nested-style ( style style-stream -- style stream )
64 [ style>> swap assoc-union ] [ stream>> ] bi ; inline
66 C: <style-stream> style-stream
68 M: style-stream stream-format
69 do-nested-style stream-format ;
71 M: style-stream stream-write
72 [ style>> ] [ stream>> ] bi stream-format ;
74 M: style-stream stream-write1
75 [ 1string ] dip stream-write ;
77 M: style-stream make-span-stream
78 do-nested-style make-span-stream ;
80 M: style-stream make-block-stream
81 [ do-nested-style make-block-stream ] [ style>> ] bi
84 M: style-stream make-cell-stream
85 [ do-nested-style make-cell-stream ] [ style>> ] bi
88 M: style-stream stream-write-table
89 [ [ [ stream>> ] map ] map ] [ ] [ stream>> ] tri*
92 M: plain-writer stream-format
95 M: plain-writer make-span-stream
96 swap <style-stream> <ignore-close-stream> ;
98 M: plain-writer make-block-stream
99 nip <ignore-close-stream> ;
101 M: plain-writer stream-write-table
104 [ [ >string ] map ] map format-table
105 [ nl ] [ write ] interleave
106 ] with-output-stream* ;
108 M: plain-writer make-cell-stream 2drop <string-writer> ;
142 CONSTANT: standard-table-style
144 { table-gap { 5 5 } }
145 { table-border T{ rgba f 0.8 0.8 0.8 1.0 } }
149 TUPLE: input string ;
153 M: input present string>> ;
159 [ % ] [ "..." "" ? % ] bi*
162 : write-object ( str obj -- ) presented associate format ;
164 : write-image ( image -- ) [ "" ] dip image associate format ;