summaryrefslogtreecommitdiff
path: root/src/OutputCapture.kt
blob: 7fc8849c3e2a0c9b99f70803a0b300c864dc9fcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)
    }
}