At 08:06 AM 10/31/99 -0500, you wrote:
>Hi... Hope some REBOL Guru can answer this one:
>
>What I'd like to do is to set a variable equal to the string that is
>normally sent to the console as a result of executing a command or script.
>The result can still go to the console if necessary, but I'd like to capture
>it for use elsewhere.

Hi Russ,

Here is a quick solution. I need to write more detail in a how-to, but this 
should get you going...

REBOL[
         Title: {Console port redirection example}
         Author: "Jim Goodnow II"
         Date: 31-Oct-1999
         Purpose: {
                 A quick and dirty example to show how to trap output 
normally sent
                 to the console. After executing the following code, the 
'my-port' value
                 can be used to capture output using either the 
system/ports/output
                 or system/ports/echo words. Setting the output word will 
append output
                 to the string port/locals instead of sending it to the 
console.
                 Using the echo word will append output to the string as 
well as
                 sending to the console. For example:

                         system/ports/echo: my-port              ; set the 
echo port

                         ;.... do your stuff here
                         now
                         list-dir %.

                         echo none                               ; disable 
the echo port

                         print my-port/locals                    ; look at 
the saved result
         }
]


port-handler: make object! [
         init: func[ port ][
                 port/locals: make string! 100
         ]
         open: func[ port ][
                 clear port/locals
         ]
         write: func [ port data ][
                 insert/part tail port/locals data port/state/num
                 port/state/num
         ]
         close: func[ port ][]
]

my-port: open/direct [
         scheme: 'my-port
         handler: port-handler
]

Reply via email to