retroforth/example/CaptureOutput.forth
crc d0070318d7 new example: capture output to a string
FossilOrigin-Name: 16f71f9d141b18d1f6c04c5f33e46839b9d61852b0ee3458e60f31cacfcbd9b7
2019-01-19 14:22:21 +00:00

23 lines
450 B
Forth

# Capturing Output
By taking advantage of the hook in `c:put`, it's possible to
write a combinator to capture output to a user specified
buffer.
~~~
{{
:capture{ &buffer:add &c:put set-hook ;
:} &c:put unhook ;
---reveal---
:capture-output (qa-)
[ buffer:set capture{ call } ] buffer:preserve ;
}}
~~~
## A Test Case
```
'Output d:create #256 #1024 * n:inc allot
[ d:words ] &Output capture-output
Output s:length n:put nl
```