prettyprinter-1.7.1: A modern, easy to use, well-documented, extensible pretty-printer.

Safe Haskell | Safe-Inferred
Language | Haskell2010

Prettyprinter.Render.Text

Contents

- Conversion to plain `Text`
- Render to a `Handle`
  - Convenience functions

Description

Render an unannotated `SimpleDocStream` as plain `Text`.

Synopsis

- renderLazy :: SimpleDocStream ann -> Text
- renderStrict :: SimpleDocStream ann -> Text
- renderIO :: Handle -> SimpleDocStream ann -> IO ()
- putDoc :: Doc ann -> IO ()
- hPutDoc :: Handle -> Doc ann -> IO ()

# Conversion to plain `Text`

renderLazy :: SimpleDocStream ann -> Text #

`(`renderLazy` sdoc)` takes the output `sdoc` from a rendering function and transforms it to lazy text.

    >>> let render = TL.putStrLn . renderLazy . layoutPretty defaultLayoutOptions
    >>> let doc = "lorem" <+> align (vsep ["ipsum dolor", parens "foo bar", "sit amet"])
    >>> render doc
    lorem ipsum dolor
          (foo bar)
          sit amet

renderStrict :: SimpleDocStream ann -> Text #

`(`renderStrict` sdoc)` takes the output `sdoc` from a rendering function and transforms it to strict text.

# Render to a `Handle`

renderIO :: Handle -> SimpleDocStream ann -> IO () #

`(`renderIO` h sdoc)` writes `sdoc` to the file `h`.

    >>> renderIO System.IO.stdout (layoutPretty defaultLayoutOptions "hello\nworld")
    hello
    world

This function is more efficient than ``hPutStr` h (`renderStrict` sdoc)`, since it writes to the handle directly, skipping the intermediate `Text` representation.

## Convenience functions

putDoc :: Doc ann -> IO () #

`(`putDoc` doc)` prettyprints document `doc` to standard output. Uses the `defaultLayoutOptions`.

    >>> putDoc ("hello" <+> "world")
    hello world

    putDoc = hPutDoc stdout

hPutDoc :: Handle -> Doc ann -> IO () #

Like `putDoc`, but instead of using `stdout`, print to a user-provided handle, e.g. a file or a socket. Uses the `defaultLayoutOptions`.

    main = withFile filename (h -> hPutDoc h doc)
      where
        doc = vcat ["vertical", "text"]
        filename = "someFile.txt"

    hPutDoc h doc = renderIO h (layoutPretty defaultLayoutOptions doc)