author | Claus Gittinger <cg@exept.de> |
Thu, 17 Feb 2011 14:13:22 +0100 | |
changeset 2537 | d186547b3459 |
parent 2525 | 4d6a0a5905e7 |
child 3001 | 314f68c6ad13 |
permissions | -rw-r--r-- |
340 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1002
ff7c1f0de146
code now independent of actual zeroPosition
Claus Gittinger <cg@exept.de>
parents:
873
diff
changeset
|
12 |
"{ Package: 'stx:libbasic2' }" |
ff7c1f0de146
code now independent of actual zeroPosition
Claus Gittinger <cg@exept.de>
parents:
873
diff
changeset
|
13 |
|
1941
3f0b0b5df9ab
Inherit from CharacterWriteStream, in order to support Unicode
Stefan Vogel <sv@exept.de>
parents:
1899
diff
changeset
|
14 |
CharacterWriteStream subclass:#TextStream |
340 | 15 |
instanceVariableNames:'runs currentEmphasis' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Streams' |
|
19 |
! |
|
20 |
||
21 |
!TextStream class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1996 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
562 | 40 |
a textStream is much like a regular writeStream; |
340 | 41 |
however, in addition to collecting characters, it keeps |
42 |
track of any change of the emphasis, and returns a Text instance |
|
43 |
as its contents (in contrast to a String instance). |
|
44 |
Can be used to collect up attributed text. |
|
45 |
||
46 |
[author:] |
|
47 |
Claus Gittinger |
|
48 |
||
49 |
[see also:] |
|
50 |
WriteStream |
|
51 |
Text String |
|
52 |
" |
|
53 |
||
54 |
! |
|
55 |
||
56 |
examples |
|
57 |
" |
|
58 |
[exBegin] |
|
59 |
|s| |
|
60 |
||
61 |
s := TextStream on:''. |
|
62 |
s emphasis:#italic; |
|
63 |
nextPutAll:'hello'; |
|
64 |
emphasis:nil; |
|
65 |
space; |
|
66 |
emphasis:#bold; |
|
1941
3f0b0b5df9ab
Inherit from CharacterWriteStream, in order to support Unicode
Stefan Vogel <sv@exept.de>
parents:
1899
diff
changeset
|
67 |
nextPutAll:'world '; |
3f0b0b5df9ab
Inherit from CharacterWriteStream, in order to support Unicode
Stefan Vogel <sv@exept.de>
parents:
1899
diff
changeset
|
68 |
nextPut:(Character codePoint:16r3C7). |
340 | 69 |
s contents inspect |
70 |
[exEnd] |
|
71 |
||
72 |
[exBegin] |
|
73 |
|s| |
|
74 |
||
75 |
s := TextStream on:''. |
|
76 |
s emphasis:#italic; |
|
77 |
nextPutAll:'hello'; |
|
78 |
emphasis:nil; |
|
79 |
space; |
|
80 |
emphasis:#bold; |
|
81 |
nextPutAll:'world'. |
|
82 |
||
562 | 83 |
Transcript nextPutAll:(s contents) |
84 |
[exEnd] |
|
85 |
||
86 |
[exBegin] |
|
87 |
|s| |
|
88 |
||
89 |
s := TextStream on:''. |
|
90 |
s emphasis:#italic; |
|
91 |
nextPutAll:'hello'; |
|
92 |
emphasis:nil; |
|
93 |
space; |
|
94 |
emphasis:#bold; |
|
95 |
nextPutAll:'world'. |
|
96 |
||
340 | 97 |
Dialog |
98 |
warn:(s contents) |
|
99 |
[exEnd] |
|
100 |
||
101 |
[exBegin] |
|
102 |
|s1 s2 flipFlop| |
|
103 |
||
104 |
s1 := PipeStream readingFrom:'ls -l'. |
|
105 |
s2 := TextStream on:''. |
|
106 |
||
107 |
flipFlop := true. |
|
108 |
[s1 atEnd] whileFalse:[ |
|
109 |
flipFlop ifTrue:[ |
|
110 |
s2 emphasis:(#color->Color red) |
|
111 |
] ifFalse:[ |
|
112 |
s2 emphasis:nil |
|
113 |
]. |
|
114 |
flipFlop := flipFlop not. |
|
115 |
s2 nextPutAll:(s1 nextLine). |
|
116 |
s2 cr. |
|
117 |
]. |
|
118 |
s1 close. |
|
119 |
||
120 |
(EditTextView new contents:s2 contents) open |
|
121 |
[exEnd] |
|
122 |
" |
|
123 |
! ! |
|
124 |
||
125 |
!TextStream methodsFor:'accessing'! |
|
126 |
||
127 |
contents |
|
128 |
"return the streams collected contents" |
|
129 |
||
2525 | 130 |
|stringContents| |
131 |
||
132 |
stringContents := super contents. |
|
133 |
||
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
134 |
(currentEmphasis isNil and:[runs isEmptyOrNil]) ifTrue:[ |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
135 |
"no emphasis information: answer a string" |
2525 | 136 |
^ stringContents |
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
137 |
]. |
340 | 138 |
self closeRun. |
2525 | 139 |
^ Text string:stringContents emphasisCollection:runs |
340 | 140 |
|
141 |
" |
|
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
142 |
Answer a Text containing unicode characters: |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
143 |
(TextStream on:'') |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
144 |
emphasis:#italic; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
145 |
nextPutAll:'hello'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
146 |
emphasis:nil; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
147 |
space; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
148 |
emphasis:#bold; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
149 |
nextPutAll:'world'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
150 |
space; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
151 |
nextPut:Character euro; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
152 |
contents. |
340 | 153 |
|
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
154 |
Answer a String: |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
155 |
(TextStream on:'') |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
156 |
nextPutAll:'hello'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
157 |
contents. |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
158 |
|
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
159 |
Answer a Text: |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
160 |
(TextStream on:Text new) |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
161 |
nextPutAll:'hello'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
162 |
contents. |
340 | 163 |
" |
2525 | 164 |
|
165 |
"Modified: / 28-01-2011 / 13:46:48 / cg" |
|
340 | 166 |
! |
167 |
||
1159 | 168 |
emphasis |
169 |
"return the current emphasis" |
|
170 |
||
171 |
^ currentEmphasis |
|
172 |
! |
|
173 |
||
663 | 174 |
emphasis:newEmphasis |
343 | 175 |
"change the emphasis; all followup elements are appended with |
176 |
that emphasis in effect" |
|
177 |
||
1158 | 178 |
position ~~ ZeroPosition ifTrue:[ |
1899 | 179 |
currentEmphasis ~= newEmphasis ifTrue:[ |
663 | 180 |
self closeRun. |
181 |
] |
|
340 | 182 |
]. |
663 | 183 |
currentEmphasis := newEmphasis |
340 | 184 |
|
185 |
" |
|
186 |
|s| |
|
187 |
||
188 |
s := TextStream on:String new. |
|
343 | 189 |
s emphasis:#italic; |
190 |
nextPutAll:'hello'; |
|
191 |
emphasis:nil; |
|
192 |
space; |
|
193 |
emphasis:#bold; |
|
194 |
nextPutAll:'world'. |
|
1899 | 195 |
s contents. |
2537
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
196 |
Transcript showCR:s contents. |
340 | 197 |
" |
663 | 198 |
|
2537
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
199 |
" |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
200 |
|s| |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
201 |
|
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
202 |
s := TextStream on:String new. |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
203 |
s emphasis:#italic; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
204 |
nextPutAll:'hello'; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
205 |
emphasis:nil; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
206 |
space; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
207 |
emphasis:{#bold. #color->Color red}; |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
208 |
nextPutAll:'world'. |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
209 |
s contents. |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
210 |
Transcript showCR:s contents. |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
211 |
" |
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
212 |
|
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
213 |
"Modified: / 17-02-2011 / 14:13:07 / cg" |
873
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
214 |
! |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
215 |
|
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
216 |
stringContents |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
217 |
"return the streams collected string contents" |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
218 |
|
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
219 |
^ super contents |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
220 |
|
340 | 221 |
! ! |
222 |
||
223 |
!TextStream methodsFor:'private'! |
|
224 |
||
225 |
closeRun |
|
1158 | 226 |
position ~~ ZeroPosition ifTrue:[ |
340 | 227 |
runs isNil ifTrue:[ |
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
228 |
runs := RunArray new:position-ZeroPosition withAll:currentEmphasis. |
340 | 229 |
] ifFalse:[ |
1158 | 230 |
runs add:currentEmphasis withOccurrences:(position-ZeroPosition-runs size) |
340 | 231 |
] |
232 |
] |
|
233 |
! ! |
|
234 |
||
2053 | 235 |
!TextStream methodsFor:'writing'! |
236 |
||
237 |
nextPutAllText:aText |
|
238 |
"write some text to the stream and keep the emphasis" |
|
239 |
||
240 |
|string pos oldEmphasis| |
|
241 |
||
242 |
string := aText string. |
|
243 |
pos := 1. |
|
244 |
||
245 |
oldEmphasis := currentEmphasis. |
|
246 |
aText emphasis runsDo:[:len :emphasis | |
|
247 |
|nextPos| |
|
248 |
||
249 |
nextPos := pos + len. |
|
250 |
||
251 |
self emphasis:emphasis. |
|
252 |
self nextPutAll:string startingAt:pos to:nextPos - 1. |
|
253 |
pos := nextPos |
|
254 |
]. |
|
255 |
self emphasis:oldEmphasis |
|
256 |
! ! |
|
257 |
||
340 | 258 |
!TextStream class methodsFor:'documentation'! |
259 |
||
260 |
version |
|
2537
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
261 |
^ '$Header: /cvs/stx/stx/libbasic2/TextStream.st,v 1.14 2011-02-17 13:13:22 cg Exp $' |
2525 | 262 |
! |
263 |
||
264 |
version_CVS |
|
2537
d186547b3459
comment/format in: #emphasis:
Claus Gittinger <cg@exept.de>
parents:
2525
diff
changeset
|
265 |
^ '$Header: /cvs/stx/stx/libbasic2/TextStream.st,v 1.14 2011-02-17 13:13:22 cg Exp $' |
340 | 266 |
! ! |