1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs colors colors.constants delegate
4 delegate.protocols destructors fry hashtables io
5 io.streams.plain io.streams.string kernel make math.order
6 namespaces present sequences splitting strings strings.tables
10 GENERIC: stream-format ( str style stream -- )
11 GENERIC: make-span-stream ( style stream -- stream' )
12 GENERIC: make-block-stream ( style stream -- stream' )
13 GENERIC: make-cell-stream ( style stream -- stream' )
14 GENERIC: stream-write-table ( table-cells style stream -- )
16 PROTOCOL: formatted-output-stream-protocol
17 stream-format make-span-stream make-block-stream
18 make-cell-stream stream-write-table ;
20 : format ( str style -- ) output-stream get stream-format ;
22 : tabular-output ( style quot -- )
23 swap [ { } make ] dip output-stream get stream-write-table ; inline
25 : with-row ( quot -- )
28 : with-cell ( quot -- )
29 H{ } output-stream get make-cell-stream
30 [ swap with-output-stream ] keep , ; inline
32 : write-cell ( str -- )
33 [ write ] with-cell ; inline
35 : with-style ( style quot -- )
36 swap dup assoc-empty? [
39 output-stream get make-span-stream swap with-output-stream
42 : with-nesting ( style quot -- )
43 [ output-stream get make-block-stream ] dip
44 with-output-stream ; inline
46 TUPLE: filter-writer stream ;
48 CONSULT: output-stream-protocol filter-writer stream>> ;
50 CONSULT: formatted-output-stream-protocol filter-writer stream>> ;
52 M: filter-writer stream-element-type stream>> stream-element-type ;
54 M: filter-writer dispose stream>> dispose ;
56 TUPLE: ignore-close-stream < filter-writer ;
58 M: ignore-close-stream dispose drop ;
60 C: <ignore-close-stream> ignore-close-stream
62 TUPLE: style-stream < filter-writer style ;
64 : do-nested-style ( style style-stream -- style stream )
65 [ style>> swap assoc-union ] [ stream>> ] bi ; inline
67 C: <style-stream> style-stream
69 M: style-stream stream-format
70 do-nested-style stream-format ;
72 M: style-stream stream-write
73 [ style>> ] [ stream>> ] bi stream-format ;
75 M: style-stream stream-write1
76 [ 1string ] dip stream-write ;
78 M: style-stream make-span-stream
79 do-nested-style make-span-stream ;
81 M: style-stream make-block-stream
82 [ do-nested-style make-block-stream ] [ style>> ] bi
85 M: style-stream make-cell-stream
86 [ do-nested-style make-cell-stream ] [ style>> ] bi
89 M: style-stream stream-write-table
90 [ [ [ stream>> ] map ] map ] [ ] [ stream>> ] tri*
93 M: plain-writer stream-format
96 M: plain-writer make-span-stream
97 swap <style-stream> <ignore-close-stream> ;
99 M: plain-writer make-block-stream
100 nip <ignore-close-stream> ;
102 M: plain-writer stream-write-table
105 [ [ >string ] map ] map format-table
106 [ nl ] [ write ] interleave
107 ] with-output-stream* ;
109 M: plain-writer make-cell-stream 2drop <string-writer> ;
143 CONSTANT: standard-table-style
145 { table-gap { 5 5 } }
146 { table-border T{ rgba f 0.8 0.8 0.8 1.0 } }
150 TUPLE: input string ;
154 M: input present string>> ;
160 [ % ] [ "..." "" ? % ] bi*
163 : write-object ( str obj -- ) presented associate format ;
165 : write-image ( image -- ) [ "" ] dip image associate format ;
167 SYMBOL: stack-effect-style
169 { foreground COLOR: FactorDarkGreen }
171 } stack-effect-style set-global