A pretty printing library for F# based on Wadler's and Leijen's work.


Keywords
F#, formatting, fsharp, layout, text
License
MIT
Install
Install-Package PPrint -Version 2.0.0

Documentation

PPrint is a pretty printing combinator library for text documents that can be rendered to a desired maximum width.

Docs

Background

Example

Here is an example of how one could define a function with signature

module Json =
  val pretty: JsonValue -> Doc

for pretty printing JSON values:

open System.Json
open PPrint

let inline (^) x = x

module Json =
  let private prettySeq (l, r) toDoc xs =
    if Seq.isEmpty xs
    then l <^> r
    else let ds = xs |> Seq.map toDoc |> punctuate comma |> vsep
         l <..> ds |> nest 2 <..> r |> group

  let rec pretty (json: JsonValue) =
    match json with
     | :? JsonObject as json ->
       json
       |> prettySeq lrbrace ^ fun kv ->
            pretty ^ JsonPrimitive kv.Key <^> colon <+> pretty kv.Value
     | :? JsonArray as json ->
       json
       |> prettySeq lrbracket pretty
     | null ->
       txt "null"
     | _ ->
       json
       |> string
       |> txt

Given a JSON value

let json = JsonValue.Parse """
{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [],
  "spouse": null
}
"""

we can render it to different desired maximum widths. For example,

json |> Json.pretty |> println ^ Some 40

produces

{
  "address": {
    "city": "New York",
    "postalCode": "10021-3100",
    "state": "NY",
    "streetAddress": "21 2nd Street"
  },
  "age": 25,
  "children": [],
  "firstName": "John",
  "isAlive": true,
  "lastName": "Smith",
  "phoneNumbers": [
    {
      "number": "212 555-1234",
      "type": "home"
    },
    {
      "number": "646 555-4567",
      "type": "office"
    }
  ],
  "spouse": null
}

and

json |> Json.pretty |> println ^ Some 80

produces

{
  "address": {
    "city": "New York",
    "postalCode": "10021-3100",
    "state": "NY",
    "streetAddress": "21 2nd Street"
  },
  "age": 25,
  "children": [],
  "firstName": "John",
  "isAlive": true,
  "lastName": "Smith",
  "phoneNumbers": [
    {"number": "212 555-1234", "type": "home"},
    {"number": "646 555-4567", "type": "office"}
  ],
  "spouse": null
}