commit 275f1bb6d29641b4091632ca6abe3f4773e10108 Author: linus björnstam Date: Thu Apr 2 11:34:24 2026 +0200 first commit diff --git a/Library.fs b/Library.fs new file mode 100644 index 0000000..c41e5e8 --- /dev/null +++ b/Library.fs @@ -0,0 +1,29 @@ +namespace rprint + +module Rprint = + let printer = new System.Threading.AsyncLocal unit>() + let _ = + printer.Value <- System.Console.Write + + let rprint format = + // This is a new friend of mine. kprintf takes a format string and a continuation. + // it returns a function that takes enough values to satisfy the format string. + // in the end it produces a string that is passed to the continuation. + Printf.kprintf (fun s -> printer.Value s) format + + let rprintn format = + Printf.kprintf (fun s -> printer.Value (s + System.Environment.NewLine)) format + + // Takes a thunk. All output printed through rprint gets redirected to a stringBuilder + // for all calls to rprint within the extent of withOutputToString + let withOutputToString f = + let sb = System.Text.StringBuilder() + let original = printer.Value + + // Bind om till StringBuilder + printer.Value <- fun s -> sb.AppendLine s |> ignore + try + f () + sb.ToString().TrimEnd() + finally + printer.Value <- original diff --git a/rprint.fsproj b/rprint.fsproj new file mode 100644 index 0000000..1ae3ffe --- /dev/null +++ b/rprint.fsproj @@ -0,0 +1,12 @@ + + + + net10.0 + true + + + + + + +