TextStream.st
author Claus Gittinger <cg@exept.de>
Fri, 22 Aug 1997 15:16:15 +0200
changeset 562 e41abf509444
parent 343 01f88d90c385
child 663 64bf2f5fec3c
permissions -rw-r--r--
comments

"
 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 regular 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'.

     Transcript nextPutAll:(s contents)
                                                                [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
    "change the emphasis; all followup elements are appended with
     that emphasis in effect"

    position ~~ 1 ifTrue:[
        self closeRun.
    ].
    currentEmphasis := aSymbol

    "
     |s|

     s := TextStream on:String new.
     s emphasis:#italic;
       nextPutAll:'hello';
       emphasis:nil;
       space;
       emphasis:#bold;
       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.3 1997-08-22 13:16:15 cg Exp $'
! !