Copyright | (c) 2022 Composewell Technologies |
---|---|
License | BSD-style |
Maintainer | harendra.kumar@gmail.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Documentation
The abstract data type Doc
represents pretty documents.
More specifically, a value of type Doc
represents a non-empty set of
possible renderings of a document. The rendering functions select one of
these possibilities.
Doc
is an instance of the Show
class. (show doc)
pretty
prints document doc
with a page width of 80 characters and a
ribbon width of 32 characters.
show (text "hello" <$> text "world")
Which would return the string "hello\nworld", i.e.
hello world
(<+>) :: Doc -> Doc -> Doc infixr 6 #
The document (x <+> y)
concatenates document x
and y
with a
space
in between. (infixr 6)
The document (vcat xs)
concatenates all documents xs
vertically with (<$$>)
. If a group
undoes the line breaks
inserted by vcat
, all documents are directly concatenated.
The document (fill i x)
renders document x
. It than appends
space
s until the width is equal to i
. If the width of x
is
already larger, nothing is appended. This combinator is quite
useful in practice to output a list of bindings. The following
example demonstrates this.
types = [("empty","Doc") ,("nest","Int -> Doc -> Doc") ,("linebreak","Doc")] ptype (name,tp) = fill 6 (text name) <+> text "::" <+> text tp test = text "let" <+> align (vcat (map ptype types))
Which is layed out as:
let empty :: Doc nest :: Int -> Doc -> Doc linebreak :: Doc
The document (indent i x)
indents document x
with i
spaces.
test = indent 4 (fillSep (map text (words "the indent combinator indents these words !")))
Which lays out with a page width of 20 as:
the indent combinator indents these words !
The document (text s)
contains the literal string s
. The
string shouldn't contain any newline ('n'
) characters. If the
string contains newline characters, the function string
should be
used.
The action (putDoc doc)
pretty prints document doc
to the
standard output, with a page width of 80 characters and a ribbon
width of 32 characters.
main :: IO () main = do{ putDoc (text "hello" <+> text "world") }
Which would output
hello world
Any ANSI colorisation in doc
will be output.