dynamic-pp-0.2.0: A pretty-print library that employs a dynamic programming algorithm for optimal rendering.

Safe HaskellNone
LanguageHaskell2010

Text.Format

Contents

Description

A pretty printer implementation, based loosely on the Wadler-Leijin pretty printer, but redesigned to facilitate a dynamic programming optimal layout algorithm.

This pretty printer module trades some of the generality of the Wadler-Leijin scheme in order to facilitate an efficient optimizing layout engine. The nesting, column, and width combinators are removed.

Synopsis

Basic Definitions

Types

data Doc Source

Instances

data Graphics Source

Graphics options for ANSI terminals. All options are wrapped in the Maybe datatype, with Nothing meaning "leave this option as-is".

Constructors

Options

Set options on the terminal, or keep the current setting in the case of Nothing.

Fields

consoleIntensity :: !(Maybe ConsoleIntensity)

Console intensity.

underlining :: !(Maybe Underlining)

Underlining.

blinkSpeed :: !(Maybe BlinkSpeed)

Blinking speed.

foreground :: !(Maybe (Color, ColorIntensity))

Foreground color and intensity.

background :: !(Maybe (Color, ColorIntensity))

Background color and intensity.

swapForegroundBackground :: !(Maybe Bool)

Whether or not to swap the foreground and background.

Default

Reset the terminal in this mode.

Type Classes

class Format item where Source

A class representing datatypes that can be formatted as Docs.

Minimal complete definition

format

Methods

format :: item -> Doc Source

Format an item as a Doc

formatList :: [item] -> Doc Source

Format a list of items as a Doc

class Monad m => FormatM m item where Source

A class representing datatypes that can be formatted as Docs inside a monad.

Minimal complete definition

formatM

Methods

formatM :: item -> m Doc Source

Format an item as a Doc inside an m monad

formatListM :: [item] -> m Doc Source

Format a list of items as a Doc inside an m monad

Creating Docs

Constructors

Basic

empty :: Doc Source

An empty Doc.

line :: Doc Source

A Doc consisting of a linebreak, that is not turned into a space when erased by a group.

linebreak :: Doc Source

A Doc consisting of a linebreak, that is turned into a space when erased by a group.

hardline :: Doc Source

A Doc consisting of a linebreak that cannot be erased by a group.

softline :: Doc Source

A Doc consisting of a space character, that can be turned into a linebreak in order to break lines that are too long.

softbreak :: Doc Source

An empty Doc that can be turned into a linebreak in order to break lines that are too long.

From datatypes

char :: Char -> Doc Source

A Doc containing a single character.

string :: String -> Doc Source

Create a Doc containing a string.

bytestring :: ByteString -> Doc Source

Create a Doc containing a bytestring.

lazyBytestring :: ByteString -> Doc Source

Create a Doc containing a lazy bytestring

Literals

lparen :: Doc Source

The character (

rparen :: Doc Source

The character )

lbrack :: Doc Source

The character [

rbrack :: Doc Source

The character ]

lbrace :: Doc Source

The character {

rbrace :: Doc Source

The character }

langle :: Doc Source

The character <

rangle :: Doc Source

The character >

squote :: Doc Source

The character '

dquote :: Doc Source

The character "

backquote :: Doc Source

The character `

comma :: Doc Source

The character ,

semi :: Doc Source

The character ;

colon :: Doc Source

The character :

dot :: Doc Source

The character .

backslash :: Doc Source

The character \

equals :: Doc Source

The character =

space :: Doc Source

A space character.

Derived

nest :: Int -> Doc -> Doc Source

Increase the indentation level of a document by some amount.

indent :: Int -> Doc -> Doc Source

Increase the indentation level of a document by some amount.

align :: Doc -> Doc Source

Set the indentation level to the current column.

squoted :: Doc -> Doc Source

Enclose a Doc in single quotes

dquoted :: Doc -> Doc Source

Enclose a Doc in double quotes

parens :: Doc -> Doc Source

Enclose a Doc in paretheses

brackets :: Doc -> Doc Source

Enclose a Doc in brackets

braces :: Doc -> Doc Source

Enclose a Doc in braces

angles :: Doc -> Doc Source

Enclose a Doc in angles

list :: [Doc] -> Doc Source

Render a list, enclosed in brackets and separated by commas.

Graphics Mode

graphics :: Graphics -> Doc -> Doc Source

Set the graphics mode on a document.

dullWhite :: Doc -> Doc Source

Color a Doc dull white.

dullRed :: Doc -> Doc Source

Color a Doc dull red.

dullYellow :: Doc -> Doc Source

Color a Doc dull yellow.

dullGreen :: Doc -> Doc Source

Color a Doc dull green.

dullBlue :: Doc -> Doc Source

Color a Doc dull blue.

dullCyan :: Doc -> Doc Source

Color a Doc dull cyan.

dullMagenta :: Doc -> Doc Source

Color a Doc dull magenta.

dullBlack :: Doc -> Doc Source

Color a Doc dull black.

vividWhite :: Doc -> Doc Source

Color a Doc vivid white.

vividRed :: Doc -> Doc Source

Color a Doc vivid red.

vividYellow :: Doc -> Doc Source

Color a Doc vivid yellow.

vividGreen :: Doc -> Doc Source

Color a Doc vivid green.

vividBlue :: Doc -> Doc Source

Color a Doc vivid blue.

vividCyan :: Doc -> Doc Source

Color a Doc vivid cyan.

vividMagenta :: Doc -> Doc Source

Color a Doc vivid magenta.

vividBlack :: Doc -> Doc Source

Color a Doc vivid black.

dullWhiteBackground :: Doc -> Doc Source

Color a Docs background dull white.

dullRedBackground :: Doc -> Doc Source

Color a Docs background dull red.

dullYellowBackground :: Doc -> Doc Source

Color a Docs background dull yellow.

dullGreenBackground :: Doc -> Doc Source

Color a Docs background dull green.

dullBlueBackground :: Doc -> Doc Source

Color a Docs background dull blue.

dullCyanBackground :: Doc -> Doc Source

Color a Docs background dull cyan.

dullMagentaBackground :: Doc -> Doc Source

Color a Docs background dull magenta.

dullBlackBackground :: Doc -> Doc Source

Color a Docs background dull black.

vividWhiteBackground :: Doc -> Doc Source

Color a Docs background vivid white.

vividRedBackground :: Doc -> Doc Source

Color a Docs background vivid red.

vividYellowBackground :: Doc -> Doc Source

Color a Docs background vivid yellow.

vividGreenBackground :: Doc -> Doc Source

Color a Docs background vivid green.

vividBlueBackground :: Doc -> Doc Source

Color a Docs background vivid blue.

vividCyanBackground :: Doc -> Doc Source

Color a Docs background vivid cyan.

vividMagentaBackground :: Doc -> Doc Source

Color a Docs background vivid magenta.

vividBlackBackground :: Doc -> Doc Source

Color a Docs background vivid black.

Combining Docs

Basic

beside :: Doc -> Doc -> Doc Source

Joun Docs with no space in between them.

concat :: [Doc] -> Doc Source

Concatenate a list of Docs. This is generally more efficient than repeatedly using beside or <>.

choose :: [Doc] -> Doc Source

A choice of several options. Only one of these will be chosen and used to render the final document.

Derived

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

Join two Docs with no space in between.

(<+>) :: Doc -> Doc -> Doc Source

Join two Docs with a space in between them.

(<!>) :: Doc -> Doc -> Doc Source

Join two Docs with a hardline in between them.

(<$>) :: Doc -> Doc -> Doc Source

Join two Docs with a line in between them.

(<$$>) :: Doc -> Doc -> Doc Source

Join two Docs with a linebreak in between them.

(</>) :: Doc -> Doc -> Doc Source

Join two Docs with a softline in between them.

(<//>) :: Doc -> Doc -> Doc Source

Join two Docs with a softbreak in between them.

hsep :: [Doc] -> Doc Source

Join a list of Docs with spaces in between each. This is generally more efficient than repeatedly using <+>.

hcat :: [Doc] -> Doc Source

Concatenate a list of Docs. This is generally more efficient than repeatedly using beside or <>.

vsep :: [Doc] -> Doc Source

Join a list of Docs with lines in between each. This is generally more efficient than repeatedly using <$$>.

vcat :: [Doc] -> Doc Source

Join a list of Docs with linebreaks in between each. This is generally more efficient than repeatedly using <$>.

sep :: [Doc] -> Doc Source

Join a list of Docs using either hsep or vsep.

cat :: [Doc] -> Doc Source

Join a list of Docs using either hcat or vcat.

fillSep :: [Doc] -> Doc Source

Join a list of Docs with softlines in between each. This is generally more efficient than repeatedly using </>.

fillCat :: [Doc] -> Doc Source

Join a list of Docs with softbreaks in between each. This is generally more efficient than repeatedly using <//>.

enclose :: Doc -> Doc -> Doc -> Doc Source

Enclose a Doc within two other Docs

punctuate :: Doc -> [Doc] -> [Doc] Source

Concatenate a list of Docs into a single doc, with each element separated from the others by a given Doc representing punctuation.

encloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc Source

Enclose a list of Docs, separated by punctuation, and align nesting of the contents to the end of the left enclosing Doc

Transforming Docs

flatten :: Doc -> Maybe Doc Source

Erase all linebreaks in a Doc and replace them with either spaces or nothing, depending on the kind of linebreak.

group :: Doc -> Doc Source

A Doc that chooses between the unmodified argument, or the flattened version of the argument.

Rendering Docs

renderOneLine :: Doc -> ByteString Source

Render the entire Doc to one line. Good for output that will be read only by a machine, where newlines are not important at all

buildOneLine :: Doc -> Builder Source

Produce a Builder that renders the Doc to one line.

putOneLine :: Handle -> Doc -> IO () Source

Output the entire Doc, as rendered by renderOneLine to the given Handle.

renderFast :: Doc -> ByteString Source

Render the entire Doc, preserving newlines, but without any indentation. Good for output that will be read only by machine, but where newlines matter.

buildFast :: Doc -> Builder Source

Produce a Builder that renders the Doc quickly.

putFast :: Handle -> Doc -> IO () Source

Output the entire Doc, as rendered by renderFast to the given Handle.

renderOptimal Source

Arguments

:: Int

The maximum number of columns.

-> Bool

Whether or not to render with ANSI terminal options.

-> Doc

The document to render.

-> ByteString 

Render a Doc as a lazy bytestring using an optimal layout rendering engine. The engine will render the document in the fewest number of lines possible without exceeding the maximum column width.

buildOptimal Source

Arguments

:: Int

The maximum number of columns.

-> Bool

Whether or not to render with ANSI terminal options.

-> Doc

The document to render.

-> Builder 

Produce a Builder that renders the Doc using the optimal layout engine.

putOptimal Source

Arguments

:: Handle

The Handle to which to write output

-> Int

The maximum number of columns.

-> Bool

Whether or not to render with ANSI terminal options.

-> Doc

The document to render.

-> IO () 

Output the entire Doc, as rendered by renderOptimal to the given Handle.