5 people like it.

Forking console output to a string

Higher order function 'withOutStr' to fork console output to a string. The function temporarily redirects Console.Out to a custom TextWriter

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
open System
open System.Text
open System.IO

type OutStr(sb:StringBuilder, orig:TextWriter) =
    inherit TextWriter()
    override x.Encoding = stdout.Encoding
    override x.Write (s:string) = sb.Append s |> ignore; orig.Write s
    override x.WriteLine (s:string) = sb.AppendLine s |> ignore; orig.WriteLine s
    override x.WriteLine() = sb.AppendLine() |> ignore; orig.WriteLine()
    member x.Value with get() = sb.ToString()
    static member Create() =
        let orig = stdout
        let out = new OutStr(new StringBuilder(), orig)
        Console.SetOut(out)
        out
    interface IDisposable with member x.Dispose() = Console.SetOut(orig)

let withOutStr f a =
    use out = OutStr.Create()
    f(a), out.Value


(* Usage:

let f (a:int) =
    Console.WriteLine("I'm being evaluated to value {0}", a)
    a

let value,outstr = withOutStr f 2

printf "Output: %s" outstr

*)
namespace System
namespace System.Text
namespace System.IO
Multiple items
type OutStr =
  inherit TextWriter
  interface IDisposable
  new : sb:StringBuilder * orig:TextWriter -> OutStr
  override Write : s:string -> unit
  override WriteLine : unit -> unit
  override WriteLine : s:string -> unit
  override Encoding : Encoding
  member Value : string
  static member Create : unit -> OutStr

Full name: Script.OutStr

--------------------
new : sb:StringBuilder * orig:TextWriter -> OutStr
val sb : StringBuilder
Multiple items
type StringBuilder =
  new : unit -> StringBuilder + 5 overloads
  member Append : value:string -> StringBuilder + 18 overloads
  member AppendFormat : format:string * arg0:obj -> StringBuilder + 4 overloads
  member AppendLine : unit -> StringBuilder + 1 overload
  member Capacity : int with get, set
  member Chars : int -> char with get, set
  member Clear : unit -> StringBuilder
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EnsureCapacity : capacity:int -> int
  member Equals : sb:StringBuilder -> bool
  ...

Full name: System.Text.StringBuilder

--------------------
StringBuilder() : unit
StringBuilder(capacity: int) : unit
StringBuilder(value: string) : unit
StringBuilder(value: string, capacity: int) : unit
StringBuilder(capacity: int, maxCapacity: int) : unit
StringBuilder(value: string, startIndex: int, length: int, capacity: int) : unit
val orig : TextWriter
Multiple items
type TextWriter =
  inherit MarshalByRefObject
  member Close : unit -> unit
  member Dispose : unit -> unit
  member Encoding : Encoding
  member Flush : unit -> unit
  member FormatProvider : IFormatProvider
  member NewLine : string with get, set
  member Write : value:char -> unit + 16 overloads
  member WriteLine : unit -> unit + 17 overloads
  static val Null : TextWriter
  static member Synchronized : writer:TextWriter -> TextWriter

Full name: System.IO.TextWriter

--------------------
TextWriter() : unit
TextWriter(formatProvider: IFormatProvider) : unit
val x : OutStr
Multiple items
override OutStr.Encoding : Encoding

Full name: Script.OutStr.Encoding

--------------------
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
val stdout<'T> : TextWriter

Full name: Microsoft.FSharp.Core.Operators.stdout
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
override OutStr.Write : s:string -> unit

Full name: Script.OutStr.Write
val s : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
StringBuilder.Append(value: char []) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: obj) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint64) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint32) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: uint16) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: decimal) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: float) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: float32) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: int64) : StringBuilder
   (+0 other overloads)
StringBuilder.Append(value: int) : StringBuilder
   (+0 other overloads)
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
TextWriter.Write(value: obj) : unit
   (+0 other overloads)
TextWriter.Write(value: string) : unit
   (+0 other overloads)
TextWriter.Write(value: decimal) : unit
   (+0 other overloads)
TextWriter.Write(value: float) : unit
   (+0 other overloads)
TextWriter.Write(value: float32) : unit
   (+0 other overloads)
TextWriter.Write(value: uint64) : unit
   (+0 other overloads)
TextWriter.Write(value: int64) : unit
   (+0 other overloads)
TextWriter.Write(value: uint32) : unit
   (+0 other overloads)
TextWriter.Write(value: int) : unit
   (+0 other overloads)
TextWriter.Write(value: bool) : unit
   (+0 other overloads)
override OutStr.WriteLine : s:string -> unit

Full name: Script.OutStr.WriteLine
StringBuilder.AppendLine() : StringBuilder
StringBuilder.AppendLine(value: string) : StringBuilder
TextWriter.WriteLine() : unit
   (+0 other overloads)
TextWriter.WriteLine(value: obj) : unit
   (+0 other overloads)
TextWriter.WriteLine(value: string) : unit
   (+0 other overloads)
TextWriter.WriteLine(value: decimal) : unit
   (+0 other overloads)
TextWriter.WriteLine(value: float) : unit
   (+0 other overloads)
TextWriter.WriteLine(value: float32) : unit
   (+0 other overloads)
TextWriter.WriteLine(value: uint64) : unit
   (+0 other overloads)
TextWriter.WriteLine(value: int64) : unit
   (+0 other overloads)
TextWriter.WriteLine(value: uint32) : unit
   (+0 other overloads)
TextWriter.WriteLine(value: int) : unit
   (+0 other overloads)
override OutStr.WriteLine : unit -> unit

Full name: Script.OutStr.WriteLine
member OutStr.Value : string

Full name: Script.OutStr.Value
StringBuilder.ToString() : string
StringBuilder.ToString(startIndex: int, length: int) : string
static member OutStr.Create : unit -> OutStr

Full name: Script.OutStr.Create
val out : OutStr
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.SetOut(newOut: TextWriter) : unit
type IDisposable =
  member Dispose : unit -> unit

Full name: System.IDisposable
override OutStr.Dispose : unit -> unit

Full name: Script.OutStr.Dispose
val withOutStr : f:('a -> 'b) -> a:'a -> 'b * string

Full name: Script.withOutStr
val f : ('a -> 'b)
val a : 'a
static member OutStr.Create : unit -> OutStr
property OutStr.Value: string
Raw view Test code New version

More information

Link:http://fssnip.net/9r
Posted:13 years ago
Author:Jonas Avelin
Tags: string , console