author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 11 Oct 2011 16:53:59 +0100 | |
branch | jv |
changeset 17883 | 209190914636 |
parent 17846 | 24edc476ac18 |
child 17892 | d86c8bd5ece3 |
permissions | -rw-r--r-- |
9312 | 1 |
"{ Package: 'stx:libbasic' }" |
2 |
||
2409 | 3 |
WriteStream subclass:#TextCollectorStream |
4 |
instanceVariableNames:'access lineLimit partialLastLine' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'Streams-Misc' |
|
8 |
! |
|
9 |
||
10 |
!TextCollectorStream class methodsFor:'documentation'! |
|
11 |
||
12 |
documentation |
|
13 |
" |
|
14 |
a textCollectorStream is much like a regular writeStream. |
|
15 |
In addition to collecting characters, it does a delayed update |
|
16 |
of its collection, and may be used as a model for a TextView to |
|
17 |
work like a TextCollector. |
|
18 |
||
19 |
Currently ListViews are optimezed for StringCollections as model. |
|
20 |
So stream on a StringCollection for best performance. |
|
21 |
||
22 |
[author:] |
|
23 |
Stefan Vogel |
|
24 |
||
25 |
[see also:] |
|
26 |
WriteStream |
|
27 |
TextCollector StringCollection |
|
28 |
||
29 |
" |
|
30 |
! |
|
31 |
||
32 |
examples |
|
33 |
" |
|
34 |
[exBegin] |
|
35 |
|s| |
|
36 |
s := TextCollectorStream on:StringCollection new. |
|
37 |
s show:'Hello '. |
|
38 |
s showCR:'World'. |
|
39 |
s contents inspect |
|
40 |
[exEnd] |
|
41 |
" |
|
42 |
! |
|
43 |
||
44 |
history |
|
45 |
"Created: 18.2.1997 / 13:18:17 / stefan" |
|
46 |
! ! |
|
47 |
||
48 |
!TextCollectorStream class methodsFor:'defaults'! |
|
49 |
||
50 |
defaultLineLimit |
|
51 |
"the number of lines remembered by default" |
|
52 |
||
53 |
^ 600 |
|
54 |
||
55 |
"Created: 18.2.1997 / 14:41:38 / stefan" |
|
56 |
! ! |
|
57 |
||
58 |
!TextCollectorStream methodsFor:'accessing'! |
|
59 |
||
60 |
access |
|
61 |
"return the value of the instance variable 'access' (automatically generated)" |
|
62 |
||
63 |
^ access |
|
64 |
||
65 |
"Created: 19.2.1997 / 09:01:21 / stefan" |
|
66 |
! |
|
67 |
||
68 |
access:something |
|
69 |
"set the value of the instance variable 'access' (automatically generated)" |
|
70 |
||
71 |
access := something. |
|
72 |
||
73 |
"Created: 19.2.1997 / 09:01:21 / stefan" |
|
74 |
! |
|
75 |
||
76 |
lineLimit |
|
77 |
"return the value of the instance variable 'lineLimit' (automatically generated)" |
|
78 |
||
79 |
^ lineLimit |
|
80 |
||
81 |
"Created: 19.2.1997 / 09:01:21 / stefan" |
|
82 |
! |
|
83 |
||
84 |
lineLimit:something |
|
85 |
"set the value of the instance variable 'lineLimit' (automatically generated)" |
|
86 |
||
87 |
lineLimit := something. |
|
88 |
||
89 |
"Created: 19.2.1997 / 09:01:21 / stefan" |
|
90 |
! ! |
|
91 |
||
92 |
!TextCollectorStream methodsFor:'initialization'! |
|
93 |
||
94 |
initialize |
|
95 |
super initialize. |
|
96 |
||
97 |
"/ access := RecursionLock new. |
|
98 |
lineLimit := self class defaultLineLimit. |
|
99 |
||
100 |
"first line is empty" |
|
101 |
partialLastLine := false. |
|
102 |
||
103 |
"Created: 18.2.1997 / 14:39:50 / stefan" |
|
104 |
"Modified: 19.2.1997 / 11:59:01 / stefan" |
|
105 |
! |
|
106 |
||
107 |
on:aCollection |
|
108 |
self initialize. |
|
109 |
^ super on:aCollection. |
|
110 |
||
111 |
"Created: 19.2.1997 / 08:59:45 / stefan" |
|
112 |
! |
|
113 |
||
114 |
on:aCollection from:start to:end |
|
115 |
self initialize. |
|
116 |
^ super on:aCollection from:start to:end. |
|
117 |
||
118 |
"Created: 19.2.1997 / 09:00:07 / stefan" |
|
119 |
! |
|
120 |
||
121 |
with:aCollection |
|
122 |
self initialize. |
|
123 |
^ super with:aCollection. |
|
124 |
||
125 |
"Created: 19.2.1997 / 09:00:24 / stefan" |
|
126 |
! ! |
|
127 |
||
128 |
!TextCollectorStream methodsFor:'writing'! |
|
129 |
||
130 |
nextPut:anObject |
|
131 |
"an Object may be a Character (cr or nl), or anything |
|
132 |
responding to #printString." |
|
133 |
||
134 |
|string newPartialLastLine size| |
|
135 |
||
136 |
(writeLimit notNil |
|
137 |
and:[position >= writeLimit]) ifTrue:[ |
|
9312 | 138 |
WriteError raiseErrorString:'write beyond writeLimit' |
2409 | 139 |
]. |
140 |
||
141 |
anObject isCharacter ifTrue:[ |
|
142 |
((anObject == Character cr) or:[anObject == Character nl]) ifTrue:[ |
|
143 |
partialLastLine ifFalse:[ |
|
144 |
collection add:''. "add empty line" |
|
145 |
position := position + 1. |
|
146 |
]. |
|
147 |
partialLastLine := false. |
|
148 |
^ anObject. |
|
149 |
]. |
|
150 |
]. |
|
151 |
||
152 |
string := anObject printString. |
|
153 |
(string includes:Character cr) ifTrue:[ |
|
154 |
newPartialLastLine := (string last ~= Character cr). |
|
155 |
string := string asStringCollection. |
|
156 |
partialLastLine ifTrue:[ |
|
9312 | 157 |
string at:1 put:((collection removeLast ? ''), (string at:1)). |
2409 | 158 |
]. |
159 |
size := collection size + string size. |
|
160 |
size > lineLimit ifTrue:[ |
|
161 |
collection removeFirst:(size - lineLimit min:(collection size)). |
|
162 |
position := lineLimit. |
|
163 |
]. |
|
164 |
collection addAll:string. |
|
165 |
position := position + string size. |
|
166 |
partialLastLine := newPartialLastLine. |
|
167 |
] ifFalse:[ |
|
168 |
partialLastLine ifTrue:[ |
|
9312 | 169 |
collection add:((collection removeLast ? ''), string). |
2409 | 170 |
] ifFalse:[ |
171 |
collection add:string. |
|
172 |
position := position + 1. |
|
173 |
]. |
|
174 |
partialLastLine := true. |
|
175 |
]. |
|
176 |
||
177 |
^ anObject |
|
178 |
||
179 |
"Created: 18.2.1997 / 14:29:51 / stefan" |
|
180 |
"Modified: 19.2.1997 / 11:47:37 / stefan" |
|
181 |
! |
|
182 |
||
183 |
show:anObject |
|
184 |
self nextPut:anObject |
|
185 |
||
186 |
"Created: 18.2.1997 / 17:20:53 / stefan" |
|
187 |
! |
|
188 |
||
189 |
showCR:anObject |
|
190 |
self nextPut:anObject; nextPut:Character cr |
|
191 |
||
192 |
"Created: 18.2.1997 / 17:21:12 / stefan" |
|
193 |
! ! |
|
194 |
||
195 |
!TextCollectorStream class methodsFor:'documentation'! |
|
196 |
||
197 |
version |
|
17883
209190914636
svn:keywords property set correctly
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17846
diff
changeset
|
198 |
^ '$Id: TextCollectorStream.st 10717 2011-10-11 15:53:59Z vranyj1 $' |
17846 | 199 |
! ! |