I was wondering if there is a good way to define a function captureOutputthat takes a function fthat can contain print instructions and return everything that was printed f. eg.
let f x = print "%s" x
let op = captureOutput (f "Hello World")
val op : string = "Hello World"
I thought there might be a good way to do this asynchronously with Console.ReadLine(), but I could not process anything.
Greetings
EDIT:
Based on the comments of Fedor Soikin, the following code does what I want:
let captureOutput f x =
let newOut = new IO.StringWriter()
Console.SetOut(newOut)
f x
Console.SetOut(Console.Out)
newOut.ToString()
source
share