64 examples |
64 examples |
65 " |
65 " |
66 pushing the contents of a stream onto another stream |
66 pushing the contents of a stream onto another stream |
67 (here, the Transcript) without a need to read everyting into a |
67 (here, the Transcript) without a need to read everyting into a |
68 buffer or to reinvent the read-loop: |
68 buffer or to reinvent the read-loop: |
69 [exBegin] |
69 [exBegin] |
70 |in pusher| |
70 |in pusher| |
71 |
71 |
72 in := 'Makefile' asFilename readStream. |
72 in := 'Makefile' asFilename readStream. |
73 pusher := FilteringLineStream readingFrom:in writingTo:Transcript. |
73 pusher := FilteringLineStream readingFrom:in writingTo:Transcript. |
74 pusher filterUpToEnd |
74 pusher filterUpToEnd |
75 [exEnd] |
75 [exEnd] |
|
76 |
|
77 |
|
78 collect a command's stdout and stderr, colorizing error output in red and normal output in green: |
|
79 [exBegin] |
|
80 |in out stdoutColorizer errColorizer| |
|
81 |
|
82 out := Transcript. |
|
83 in := OperatingSystem |
|
84 executeCommand:'bc' |
|
85 inputFrom: |
|
86 '4+5 |
|
87 bla bla foo |
|
88 4+5 |
|
89 bar |
|
90 ' readStream |
|
91 outputTo:((FilteringLineStream writingTo:out) filter:[:line | line colorizeAllWith:Color green darkened]) |
|
92 errorTo:((FilteringLineStream writingTo:out) filter:[:line | line colorizeAllWith:Color red]). |
|
93 [exEnd] |
76 |
94 |
77 |
95 |
78 filter through an ActorStream: |
96 filter through an ActorStream: |
79 [exBegin] |
97 [exBegin] |
80 |lineNr in pusher actorPerLine| |
98 |lineNr in pusher actorPerLine| |
81 |
99 |
82 lineNr := 0. |
100 lineNr := 0. |
83 |
101 |
84 actorPerLine := ActorStream new. |
102 actorPerLine := ActorStream new. |
85 actorPerLine nextPutLineBlock:[:line | lineNr := lineNr + 1. Transcript show:lineNr; showCR:line]. |
103 actorPerLine nextPutLineBlock:[:line | lineNr := lineNr + 1. Transcript show:lineNr; showCR:line]. |
86 |
104 |
87 in := 'Makefile' asFilename readStream. |
105 in := 'Makefile' asFilename readStream. |
88 pusher := FilteringLineStream readingFrom:in writingTo:actorPerLine. |
106 pusher := FilteringLineStream readingFrom:in writingTo:actorPerLine. |
89 pusher filterUpToEnd |
107 pusher filterUpToEnd |
90 [exEnd] |
108 [exEnd] |
91 |
109 |
92 |
110 |
93 filter all comments (starting with '#') from a Makefile: |
111 filter all comments (starting with '#') from a Makefile: |
94 [exBegin] |
112 [exBegin] |
95 |in filter| |
113 |in filter| |
96 |
114 |
97 in := 'Makefile' asFilename readStream. |
115 in := 'Makefile' asFilename readStream. |
98 |
116 |
99 filter := FilteringLineStream readingFrom:in. |
117 filter := FilteringLineStream readingFrom:in. |
100 filter filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]]. |
118 filter filter:[:line | (line startsWith:'#') ifTrue:[line] ifFalse:[nil]]. |
101 |
119 |
102 filter outputStream:Transcript. |
120 filter outputStream:Transcript. |
103 |
121 |
104 filter filterUpToEnd |
122 filter filterUpToEnd |
105 [exEnd] |
123 [exEnd] |
106 |
124 |
107 |
125 |
108 the inverse - remove all comments from the Makefile: |
126 the inverse - remove all comments from the Makefile: |
109 [exBegin] |
127 [exBegin] |
110 |in filter| |
128 |in filter| |
111 |
129 |
112 in := 'Makefile' asFilename readStream. |
130 in := 'Makefile' asFilename readStream. |
113 |
131 |
114 filter := FilteringLineStream readingFrom:in. |
132 filter := FilteringLineStream readingFrom:in. |
115 filter filter:[:line | (line startsWith:'#') ifTrue:[nil] ifFalse:[line]]. |
133 filter filter:[:line | (line startsWith:'#') ifTrue:[nil] ifFalse:[line]]. |
116 |
134 |
117 filter outputStream:Transcript. |
135 filter outputStream:Transcript. |
118 |
136 |
119 filter filterUpToEnd |
137 filter filterUpToEnd |
120 [exEnd] |
138 [exEnd] |
121 |
139 |
122 |
140 |
123 feed a second filter from the first filters output; |
141 feed a second filter from the first filters output; |
124 (the remains are all lines starting with '#' and ending with '-'): |
142 (the remains are all lines starting with '#' and ending with '-'): |
125 [exBegin] |
143 [exBegin] |
126 |in filter1 filter2| |
144 |in filter1 filter2| |
127 |
145 |
128 in := 'Makefile' asFilename readStream. |
146 in := 'Makefile' asFilename readStream. |
129 |
147 |
130 filter1 := FilteringLineStream readingFrom:in. |
148 filter1 := FilteringLineStream readingFrom:in. |