Skip to content

Quieting FSI output

December 19, 2013

This post is further enhancement of my suggestion on Stack Overflow upon an approach to solving the following problem: how to limit output to F# Interactive window to just user’s own?

At first I thought that such mode doesn’t make sense as FSI outputs gazillion of useful bits and pieces, but on further consideration found it may be of certain use sometimes.

So, the initial approach attempt was to somehow use –quiet setting upon starting FSI session. But unfortunately this is an “all or none” option, being set it suppresses FSI’s output to the stdout stream altogether. Thinking along this line I recalled that stderr stream continues to be active regardless of quieting stdout, hence using eprintf/eprintfn in place of printf/printfn must do the trick. And it does, indeed. This is where I stopped on Stack Overflow.

Further thinking was along the lines that having two different code versions for quiet and normal FSI modes is an inconvenience, if not too much burden. This is where F# shadowing in concert with –use: come into play. Using the following FSI script Quiet.fsx below that basically redirects stdout output into stderr will do the trick:

#if INTERACTIVE
let printf fmt =
    Microsoft.FSharp.Core.Printf.eprintf fmt
let printfn fmt =
    Microsoft.FSharp.Core.Printf.eprintfn fmt
#endif

The only matter left is providing both FSI options on startup. I’ll show how to do this for FSI fired within Visual studio. Similarly, for autonomous FSI run these options should be placed on command line.

Thinking of placing Quiet.fsi script I finally chose my user directory, which has permanent relative location against FSI default startup directory; if you prefer another arrangement, then adjust accordingly:

  • on Visual Studio menu click Tools -> Options -> F# Tools -> F# Interactive
  • on Misc tab append –quiet –use:”../../../Quiet.fsx” to the contents of F# Interactive options
  • Now, in normal mode the following script

    let a = 1
    printfn "The value of a is %d" a
    

    produces quite verbose output:

    Microsoft (R) F# Interactive version 11.0.60610.1
    Copyright (c) Microsoft Corporation. All Rights Reserved.

    For help type #help;;

    >
    The value of a is 1

    val a : int = 1
    val it : unit = ()

    >

    In quiet mode the same script’s output will be as terse as:

    The value of a is 1

    Advertisements

    From → F#

    One Comment

    Trackbacks & Pingbacks

    1. F# Weekly #51, 2013 | Sergey Tihon's Blog

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: