intitial checkin
authorClaus Gittinger <cg@exept.de>
Tue, 14 May 1996 18:48:26 +0200
changeset 340 dcc2bb30c0c9
parent 339 4d1140308a76
child 341 1e28fd1604ba
intitial checkin
TextStream.st
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextStream.st	Tue May 14 18:48:26 1996 +0200
@@ -0,0 +1,168 @@
+"
+ COPYRIGHT (c) 1996 by Claus Gittinger
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+
+WriteStream subclass:#TextStream
+	instanceVariableNames:'runs currentEmphasis'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Streams'
+!
+
+!TextStream class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1996 by Claus Gittinger
+              All Rights Reserved
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+"
+
+!
+
+documentation
+"
+    a textStream is much like a regulat writeStream;
+    however, in addition to collecting characters, it keeps
+    track of any change of the emphasis, and returns a Text instance
+    as its contents (in contrast to a String instance).
+    Can be used to collect up attributed text.
+
+    [author:]
+        Claus Gittinger
+
+    [see also:]
+        WriteStream
+        Text String
+"
+
+!
+
+examples
+"
+                                                                [exBegin]
+     |s|
+
+     s := TextStream on:''.
+     s emphasis:#italic;
+       nextPutAll:'hello';
+       emphasis:nil;
+       space;
+       emphasis:#bold;
+       nextPutAll:'world'.
+     s contents inspect
+                                                                [exEnd]
+
+                                                                [exBegin]
+     |s|
+
+     s := TextStream on:''.
+     s emphasis:#italic;
+       nextPutAll:'hello';
+       emphasis:nil;
+       space;
+       emphasis:#bold;
+       nextPutAll:'world'.
+
+     Dialog
+        warn:(s contents)
+                                                                [exEnd]
+
+                                                                [exBegin]
+     |s1 s2 flipFlop|
+
+     s1 := PipeStream readingFrom:'ls -l'.
+     s2 := TextStream on:''.
+
+     flipFlop := true.
+     [s1 atEnd] whileFalse:[
+        flipFlop ifTrue:[
+            s2 emphasis:(#color->Color red)
+        ] ifFalse:[
+            s2 emphasis:nil
+        ].
+        flipFlop := flipFlop not.
+        s2 nextPutAll:(s1 nextLine).
+        s2 cr.
+     ].
+     s1 close.
+
+     (EditTextView new contents:s2 contents) open
+                                                                [exEnd]
+"
+! !
+
+!TextStream methodsFor:'accessing'!
+
+contents
+    "return the streams collected contents"
+
+    self closeRun.
+    ^ Text string:super contents emphasisCollection:runs
+
+    "
+     |s|
+
+     s := TextStream on:String new.
+     s emphasis:#italic.
+     s nextPutAll:'hello'.
+     s emphasis:nil.
+     s space.
+     s emphasis:#bold.
+     s nextPutAll:'world'.
+     s contents
+    "
+!
+
+emphasis:aSymbol
+    position ~~ 1 ifTrue:[
+        self closeRun.
+    ].
+    currentEmphasis := aSymbol
+
+    "
+     |s|
+
+     s := TextStream on:String new.
+     s emphasis:#italic.
+     s nextPutAll:'hello'.
+     s emphasis:nil.
+     s space.
+     s emphasis:#bold.
+     s nextPutAll:'world'.
+     s contents
+    "
+! !
+
+!TextStream methodsFor:'private'!
+
+closeRun
+    position ~~ 1 ifTrue:[
+        runs isNil ifTrue:[
+            runs := RunArray new:position-1 withAll:currentEmphasis
+        ] ifFalse:[
+            runs add:currentEmphasis withOccurrences:(position - runs size - 1)
+        ]
+    ]
+! !
+
+!TextStream class methodsFor:'documentation'!
+
+version
+    ^ '$Header: /cvs/stx/stx/libbasic2/TextStream.st,v 1.1 1996-05-14 16:48:26 cg Exp $'
+! !