first commit
This commit is contained in:
commit
275f1bb6d2
2 changed files with 41 additions and 0 deletions
29
Library.fs
Normal file
29
Library.fs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
namespace rprint
|
||||
|
||||
module Rprint =
|
||||
let printer = new System.Threading.AsyncLocal<string -> 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
|
||||
12
rprint.fsproj
Normal file
12
rprint.fsproj
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Library.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue