Friday, 30 December 2011

Console output

Reading console history is very easy, just read system/console/hystory, like this:

>> print "Hello, word!"
== "Hello, world!"
>> 2 + 2
== 4
>> probe system/console/history
== [ "2 + 2" {print "Hello, word!"}]


but how to register the console output?
It's easy using one of the most important rule of Rebol, word redefinition.
The only command that send output on the console are prin and print, so we can redefine these commands to store the output in a variable. See this example, where we store all console output in temp:

>> temp: copy []
>> print-bak: get 'print
>> prin-bak: get 'prin
>> print: func [value] [append temp (reform value)  print-bak value]
>> prin: func [value] [append  temp (reform value) prin-bak value]
>> ? print-bak
USAGE:
PRINT-BAK value

DESCRIPTION:
Outputs a value followed by a line break.
PRINT-BAK is a native value.

ARGUMENTS:
value -- The value to print (Type: any)

>> probe temp
== ["USAGE:^/^-" "PRINT-BAK " "value " "" "^/DESCRIPTION:" "^- Outputs a value followed by a line break. " "^- PRINT-BAK is a nati...

No comments:

Post a Comment