From 275f1bb6d29641b4091632ca6abe3f4773e10108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?linus=20bj=C3=B6rnstam?= Date: Thu, 2 Apr 2026 11:34:24 +0200 Subject: [PATCH] first commit --- Library.fs | 29 +++++++++++++++++++++++++++++ rprint.fsproj | 12 ++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 Library.fs create mode 100644 rprint.fsproj 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 + + + + + + +