summaryrefslogtreecommitdiff
path: root/src/OutputCapture.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/OutputCapture.kt')
-rw-r--r--src/OutputCapture.kt23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/OutputCapture.kt b/src/OutputCapture.kt
new file mode 100644
index 0000000..7fc8849
--- /dev/null
+++ b/src/OutputCapture.kt
@@ -0,0 +1,23 @@
+package moe.nea.lisp
+
+object OutputCapture {
+ data class CapturedOutput(
+ internal var string: StringBuilder,
+ ) {
+ val asString get() = string.toString()
+ }
+
+ object Meta : StackFrame.MetaKey<CapturedOutput>
+
+ fun captureOutput(stackFrame: StackFrame): CapturedOutput {
+ val output = CapturedOutput(StringBuilder())
+ stackFrame.setMeta(Meta, output)
+ return output
+ }
+
+ fun print(stackFrame: StackFrame, text: String) {
+ val output = stackFrame.getMeta(Meta)
+ output?.string?.append(text)
+ print(text)
+ }
+} \ No newline at end of file