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

Safe HaskellSafe
LanguageHaskell2010

Data.Text.Prettyprint.Doc.Render.Text

Contents

Description

Render an unannotated SimpleDocStream as plain Doc.

Synopsis

Conversion to plain Doc

renderLazy :: SimpleDocStream ann -> Text Source #

(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 Source #

(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 () Source #

(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 Doc representation.

Convenience functions

putDoc :: Doc ann -> IO () Source #

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

>>> putDoc ("hello" <+> "world")
hello world
putDoc = hPutDoc stdout

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

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)