author | Stefan Vogel <sv@exept.de> |
Wed, 09 Apr 2008 08:02:33 +0200 | |
changeset 1943 | 63b23152a6d2 |
parent 1941 | 3f0b0b5df9ab |
child 2053 | 7592e3ecdca4 |
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 |
||
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
130 |
(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
|
131 |
"no emphasis information: 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
|
132 |
^ super contents |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
133 |
]. |
340 | 134 |
self closeRun. |
135 |
^ Text string:super contents emphasisCollection:runs |
|
136 |
||
137 |
" |
|
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
138 |
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
|
139 |
(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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
space; |
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:#bold; |
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:'world'; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
146 |
space; |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
147 |
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
|
148 |
contents. |
340 | 149 |
|
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
150 |
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
|
151 |
(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
|
152 |
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
|
153 |
contents. |
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
154 |
|
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
155 |
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
|
156 |
(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
|
157 |
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
|
158 |
contents. |
340 | 159 |
" |
160 |
! |
|
161 |
||
1159 | 162 |
emphasis |
163 |
"return the current emphasis" |
|
164 |
||
165 |
^ currentEmphasis |
|
166 |
! |
|
167 |
||
663 | 168 |
emphasis:newEmphasis |
343 | 169 |
"change the emphasis; all followup elements are appended with |
170 |
that emphasis in effect" |
|
171 |
||
1158 | 172 |
position ~~ ZeroPosition ifTrue:[ |
1899 | 173 |
currentEmphasis ~= newEmphasis ifTrue:[ |
663 | 174 |
self closeRun. |
175 |
] |
|
340 | 176 |
]. |
663 | 177 |
currentEmphasis := newEmphasis |
340 | 178 |
|
179 |
" |
|
180 |
|s| |
|
181 |
||
182 |
s := TextStream on:String new. |
|
343 | 183 |
s emphasis:#italic; |
184 |
nextPutAll:'hello'; |
|
185 |
emphasis:nil; |
|
186 |
space; |
|
187 |
emphasis:#bold; |
|
188 |
nextPutAll:'world'. |
|
1899 | 189 |
s contents. |
190 |
Transcript showCr:s contents. |
|
340 | 191 |
" |
663 | 192 |
|
1899 | 193 |
"Modified: / 15-10-2007 / 16:27:31 / cg" |
873
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
194 |
! |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
195 |
|
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
196 |
stringContents |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
197 |
"return the streams collected string contents" |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
198 |
|
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
199 |
^ super contents |
2582affa063b
added #stringContents (for ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
663
diff
changeset
|
200 |
|
340 | 201 |
! ! |
202 |
||
203 |
!TextStream methodsFor:'private'! |
|
204 |
||
205 |
closeRun |
|
1158 | 206 |
position ~~ ZeroPosition ifTrue:[ |
340 | 207 |
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
|
208 |
runs := RunArray new:position-ZeroPosition withAll:currentEmphasis. |
340 | 209 |
] ifFalse:[ |
1158 | 210 |
runs add:currentEmphasis withOccurrences:(position-ZeroPosition-runs size) |
340 | 211 |
] |
212 |
] |
|
213 |
! ! |
|
214 |
||
215 |
!TextStream class methodsFor:'documentation'! |
|
216 |
||
217 |
version |
|
1943
63b23152a6d2
#contents does not convert the underlying collection to a Text, if there is
Stefan Vogel <sv@exept.de>
parents:
1941
diff
changeset
|
218 |
^ '$Header: /cvs/stx/stx/libbasic2/TextStream.st,v 1.11 2008-04-09 06:02:33 stefan Exp $' |
340 | 219 |
! ! |