darcs-2.16.1: a distributed, interactive, smart revision control system

Darcs.Util.Printer

Description

Darcs pretty printing library

The combinator names are taken from HughesPJ, although the behaviour of the two libraries is slightly different.

This code was made generic in the element type by Juliusz Chroboczek.

Synopsis

Doc type and structural combinators

newtype Doc Source #

A Doc is a bit of enriched text. Docs are concatenated using <> from class Monoid, which is right-associative.

Constructors

 Doc FieldsunDoc :: St -> Document
Instances
 Source # Together with the language extension OverloadedStrings, this allows to use string literals where a Doc is expected. Instance detailsDefined in Darcs.Util.Printer Methods Source # Instance detailsDefined in Darcs.Util.Printer Methods(<>) :: Doc -> Doc -> Doc #stimes :: Integral b => b -> Doc -> Doc # Source # mappend (<>) is concatenation, mempty is the empty Doc Instance detailsDefined in Darcs.Util.Printer Methodsmappend :: Doc -> Doc -> Doc #mconcat :: [Doc] -> Doc #

The empty Doc

(<>) :: Semigroup a => a -> a -> a infixr 6 #

An associative operation.

(<?>) :: Doc -> Doc -> Doc Source #

a <?> b is a <> b if a is not empty, else empty

(<+>) :: Doc -> Doc -> Doc infixr 6 Source #

a <+> b is a followed by b with a space in between if both are non-empty

($$) :: Doc -> Doc -> Doc infixr 5 Source # a$$ b is a above b

($+$) :: Doc -> Doc -> Doc infixr 5 Source #

a $+$ b is a above b with an empty line in between if both are non-empty

vcat :: [Doc] -> Doc Source #

Pile Docs vertically

vsep :: [Doc] -> Doc Source #

Pile Docs vertically, with a blank line in between

hcat :: [Doc] -> Doc Source #

Concatenate Docs horizontally

hsep :: [Doc] -> Doc Source #

Concatenate Docs horizontally with a space as separator

A Doc representing a "-"

A Doc representing a newline

A Doc representing a "+"

A Doc representing a space (" ")

A Doc representing a "\"

A Doc that represents "("

A Doc that represents ")"

parens d = lparen <> d <> rparen

Turn a Doc into a sentence. This appends a ".".

Constructing Docs

text creates a Doc from a String, using printable.

hiddenText creates a Doc containing hidden text from a String

invisibleText creates a Doc containing invisible text from a String

wrapText n s is a Doc representing s line-wrapped at n characters

Quote a string for screen output

formatText :: Int -> [String] -> Doc Source #

Given a list of Strings representing the words of a paragraph, format the paragraphs using wrapText and separate them with an empty line.

formatWords :: [String] -> Doc Source #

A variant of wrapText that takes a list of strings as input. Useful when {--} makes it impossible to use multiline string literals.

pathlist :: [FilePath] -> Doc Source #

Format a list of FilePaths as quoted text. It deliberately refuses to use English.andClauses but rather separates the quoted strings only with a space, because this makes it usable for copy and paste e.g. as arguments to another shell command.

userchunk creates a Doc containing a user chunk from a String

packedString builds a Doc from a ByteString using printable

invisiblePS creates a Doc with invisible text from a ByteString

userchunkPS creates a Doc representing a user chunk from a ByteString.

Rrrright. And what, please is that supposed to mean?

Rendering to String

renders a Doc into a String with control codes for the special features of the Doc.

renders a Doc into a String using a given set of printers. If content is only available as ByteString, decode according to the current locale.

Rendering to ByteString

renders a Doc into ByteString with control codes for the special features of the Doc. See also readerString.

renders a Doc into a ByteString using a given set of printers.

renders a Doc into a list of PackedStrings, one for each line.

renders a Doc into a list of PackedStrings, one for each chunk of text that was added to the Doc, using the given set of printers.

Printers

data Printers' Source #

A set of printers to print different types of text to a handle.

Constructors

 Printers FieldscolorP :: !(Color -> Printer) invisibleP :: !Printer hiddenP :: !Printer userchunkP :: !Printer defP :: !Printer lineColorT :: !(Color -> Doc -> Doc) lineColorS :: !([Printable] -> [Printable])

type Printer = Printable -> St -> Document Source #

simplePrinters is a Printers which uses the set 'simplePriners\'' on any handle.

invisiblePrinter is the Printer for hidden text. It just replaces the document with empty. It's useful to have a printer that doesn't actually do anything because this allows you to have tunable policies, for example, only printing some text if it's to the terminal, but not if it's to a file or vice-versa.

simplePrinter is the simplest Printer: it just concatenates together the pieces of the Doc

Printables

data Printable Source #

A Printable is either a String, a packed string, or a chunk of text with both representations.

Constructors

 S !String PS !ByteString Both !String !ByteString

Creates a Doc from any Printable.

Creates an invisible Doc from any Printable.

Creates a hidden Doc from any Printable.

Creates... WTF is a userchunk???

Constructing colored Docs

data Color Source #

Constructors

 Blue Red Green Cyan Magenta

colorText creates a Doc containing colored text from a String

IO, uses hPut for output

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

hputDoc puts a Doc on the given handle using simplePrinters

hPutDocLn :: Handle -> Doc -> IO () Source #

hputDocLn puts a Doc, followed by a newline on the given handle using simplePrinters.

putDoc :: Doc -> IO () Source #

putDoc puts a Doc on stdout using the simple printer simplePrinters.

putDocLn :: Doc -> IO () Source #

putDocLn puts a Doc, followed by a newline on stdout using simplePrinters

hPutDocWith :: Printers -> Handle -> Doc -> IO () Source #

hputDocWith puts a Doc on the given handle using the given printer.

hPutDocLnWith :: Printers -> Handle -> Doc -> IO () Source #

hputDocLnWith puts a Doc, followed by a newline on the given handle using the given printer.

putDocWith :: Printers -> Doc -> IO () Source #

putDocWith puts a Doc on stdout using the given printer.

putDocLnWith :: Printers -> Doc -> IO () Source #

putDocLnWith puts a Doc, followed by a newline on stdout using the given printer.

hPutDocCompr :: Handle -> Doc -> IO () Source #

like hPutDoc but with compress data before writing

debugDocLn :: Doc -> IO () Source #

Write a Doc to stderr if debugging is turned on.

TODO: It is unclear what is unsafe about these constructors

unsafeText creates a Doc from a String, using simplePrinter directly

unsafeBoth builds a Doc from a String and a ByteString representing the same text, but does not check that they do.

unsafeBothText builds a Doc from a String. The string is stored in the Doc as both a String and a ByteString.

unsafeChar creates a Doc containing just one character.

unsafePackedString builds a Doc from a ByteString using simplePrinter