the-sett/elm-pretty-printer/2.1.0

NOTE: the latest version is 3.3.1

Pretty

Pretty printer.

type Doc

The type of documents that can be pretty printed.

Building documents from string data

Creates an empty document.

pretty 10 empty == ""

Creates a document consisting of a single space.

string : String -> Doc

Creates a document from a string.

char : Char -> Doc

Creates a document from a character.

Joining documents together

append : Doc -> Doc -> Doc

Appends two documents together.

a : Doc -> Doc -> Doc

Short hand notation for append. Usefull when appending multiple parts together:

string "Hello"
  |> a space
  |> a "World"
  |> a (char '!')
  |> a line
join : Doc -> List Doc -> Doc

Concatenates a list of documents together interspersed with a separator document.

lines : List Doc -> Doc

Concatenate a list of documents together interspersed with lines. Very convenient when laying out lines after another:

lines
  [ string "Heading"
  , empty
  , words [string "First", string "paragraph"]
  ...
  ]


==


string "Heading"
  |> a line
  |> a line
  |> a (string "First")
  |> a space
  |> a (string "paragraph")
  ...

See also words.

softlines : List Doc -> Doc

Like lines but uses softline instaed.

words : List Doc -> Doc

Concatenate a list of documents together interspersed with spaces. Very convenient when laying out words after another.

See also lines.

fold : (a -> Doc -> Doc) -> List a -> Doc

Fold a list of documents from left to right using a given function.

fold f == List.foldl f empty

Fitting documents onto lines

Tries to fit a document on a single line, replacing line breaks with single spaces where possible to achieve this.

Creates a hard line break. This always creates a new line, with subsequent text at the current indentation level.

Creates a line break that will render to a single space if the documents it separtes can be fitted onto one line, or a line break otherwise.

Indenting and alinging documents

Adds an indent of the current column position to all line breaks in the document. The first line will not be indented, only subsequent nested lines will be.

nest : Int -> Doc -> Doc

Adds an indent of the given number of spaces to all line breakss in the document. The first line will not be indented, only subsequent nested lines will be.

hang : Int -> Doc -> Doc

Adds an indent of the current column position to all line breaks in the document and a further indent of the specified number of columns. The first line will not be indented, only subsequent nested lines will be.

indent : Int -> Doc -> Doc

Indents a whole document by a given number of spaces.

Putting things around documents

surround : Doc -> Doc -> Doc -> Doc

Places a document inside left and right book ends.

pretty 100 (surround (char '\') (char '/') string "hello")
  == "\hello/"

Wraps a document in parnethesese

Wraps a document in braces.

Wraps a document in brackets.

Pretty printing documents

pretty : Int -> Doc -> String

Pretty prints a document trying to fit it as best as possible to the specified column width of the page.