Pretty-Printing

Atomo provides a handy wrapper for the Haskell Text.PrettyPrint system. The documentation here is largely based off of its own.

Pretty  Object

Provides a ton of intelligent pretty-printing functionality.

o pretty  Pretty

Turn an object into a pretty-printed document, which can be used in other pretty-printers. This is defined on Object, so everything has it, but you should override this if possible.

Pretty-printing a value should yield something that looks like the expression that created that value. If it cannot be pretty-printed cleanly, make it obvious, usually via surrounding some description with angle brackets (<>).

Example:

> 1 pretty
1
> (1 -> 2) pretty
1 -> 2
x doc  Pretty
  | x is-a?: Block

Used for writing pretty-printed forms; basically a shortcut for @(Pretty join: x).

Example:

> { char: $c <+> int: 10 } doc
c 10

Documents From Values

Pretty char: c  Pretty
  | c is-a?: Character

A document of height and width 1, containing a literal character.

Example:

> Pretty char: $x
x
Pretty text: s  Pretty
  | s is-a?: String

A document of height 1 containing a literal string.

Satisfies the following laws:

text: s <> text: t == text: (s .. t)
text: "" <> x == x, if x is non-empty

The side condition on the last law is necessary because text "" has height 1, while empty has no height.

Example:

> Pretty text: "hello!"
hello!
Pretty zero-width-text: s  Pretty
  | s is-a?: String

Some text, but without any width. Use for non-printing text such as a HTML or Latex tags.

Example:

> Pretty zero-width-text: "hello!"
hello!
Pretty int: x  Pretty
  | x is-a?: Integer

Shortcut for text: x show.

Pretty integer: x  Pretty
  | x is-a?: Integer

Same as int:.

Pretty float: x  Pretty
  | x is-a?: Double

Shortcut for text: x show.

Pretty double: x  Pretty
  | x is-a?: Double

Same as float:.

Pretty rational: x  Pretty
  | x is-a?: Rational

Shortcut for text: x show.

Simple Shortcuts

Pretty semi  Pretty

Shortcut for char: $;.

Pretty comma  Pretty

Shortcut for char: $,.

Pretty colon  Pretty

Shortcut for char: $:.

Pretty space  Pretty

Shortcut for char: $ .

Pretty equals  Pretty

Shortcut for char: $=.

Pretty lparen  Pretty

Shortcut for char: $(.

Pretty rparen  Pretty

Shortcut for char: $).

Pretty lbrack  Pretty

Shortcut for char: $[.

Pretty rbrack  Pretty

Shortcut for char: $].

Pretty lbrace  Pretty

Shortcut for char: ${.

Pretty rbrace  Pretty

Shortcut for char: $}.

Wrapping Documents

Pretty parens: p  Pretty
  | p is-a?: Pretty

Wrap p in (...).

Example:

> { parens: (text: "hi!") } doc
(hi!)
Pretty brackets: p  Pretty
  | p is-a?: Pretty

Wrap p in [...].

Example:

> { brackets: (text: "hi!") } doc
[hi!]
Pretty braces: p  Pretty
  | p is-a?: Pretty

Wrap p in {...}.

Example:

> { braces: (text: "hi!") } doc
{hi!}
Pretty quotes: p  Pretty
  | p is-a?: Pretty

Wrap p in '...'.

Example:

> { quotes: (text: "hi!") } doc
'hi!'
Pretty double-quotes: p  Pretty
  | p is-a?: Pretty

Wrap p in "...".

Example:

> { double-quotes: (text: "hi!") } doc
"hi!"

Combining Documents

Pretty empty  Pretty

The empty document, with no height and no width. empty is the identity for <>, <+>, \\ and \+\ and anywhere in the argument list for sep:, hcat:, hsep:, vcat:, fcat:, etc.

Example:

> Pretty empty

(Just a blank line.)

p empty?  Boolean
  | p is-a?: Pretty

Is the document p empty?

a <> b  Pretty
  | a is-a?: Pretty
  | b is-a?: Pretty

Beside. <> is associative, with identity empty.

Example:

> { (char: $e) <> (integer: 42) } doc
e42
a <+> b  Pretty
  | a is-a?: Pretty
  | b is-a?: Pretty

Beside, separated by a space, unless one of the arguments is empty. <+> is associative, with identity empty.

Example:

> { (char: $e) <+> (integer: 42) } doc
e 42
Pretty hcat: ps  Pretty
  | ps is-a?: List

List version of <>.

Pretty hsep: ps  Pretty
  | ps is-a?: List

List version of <+>.

a \\ b  Pretty
  | a is-a?: Pretty
  | b is-a?: Pretty

Position a above b, except that if the last line of the first argument stops at least one position before the first line of the second begins, these two lines are overlapped.

Example:

> { text: "hi" \\ (text: "there") nest: 1 } doc
hi
 there
> { text: "hi" \\ (text: "there") nest: 5 } doc
hi   there

\\ is associative, with identity empty, and also satisfies:

(x \\ y) <> z == x $$ (y <> z), if y is non-empty
a \+\ b  Pretty
  | a is-a?: Pretty
  | b is-a?: Pretty

Position a above b, with no overlapping. \+\ is associative, with identity empty.

Example:

> { text: "hi" \+\ (text: "there") nest: 1 } doc
hi
 there
> { text: "hi" \+\ (text: "there") nest: 5 } doc
hi
     there
Pretty vcat: ps  Pretty
  | ps is-a?: List

List version of \\.

Pretty sep: ps  Pretty
  | ps is-a?: List

Either hsep: or vcat:.

Pretty cat: ps  Pretty
  | ps is-a?: List

Either hcat: or vcat:.

Pretty fsep: ps  Pretty
  | ps is-a?: List

"Paragraph fill" version of sep:.

Pretty fcat: ps  Pretty
  | ps is-a?: List

"Paragraph fill" version of cat:.

p nest: n  Pretty
  | p is-a?: Pretty
  | n is-a?: Integer

Nest (or indent) a document by a given number of positions (which may also be negative). nest: satisfies the laws:

x nest: 0 == x
(x nest: a) nest: b == x nest: (a + b)
(x <> y) nest: a == x nest: a <> y nest: a
(x \\ y) nest: a == x nest: a \\ y nest: a
Pretty empty nest: a == Pretty empty
x <> y nest: a = x <> y, if x is non-empty

The side condition on the last law is needed because empty is a left identity for <>.

Example:

> { (text: "hello!") nest: 4 } doc
    hello!
a hang: b indented: n  Pretty
  | a is-a?: Pretty
  | b is-a?: Pretty
  | n is-a?: Integer

Shortcut for Pretty sep: [a, b nest: n].

Example:

> { (text: "one") hang: (text: "two" \+\ text: "three") indented: 1 } doc
one
 two
 three
delim punctuate: ps  Pretty
  | delim is-a?: Pretty
  | ps is-a?: List

Intersperse delim through ps.

That is, x punctuate: [p1, ... pn] is [p1 <> x, p2 <> x, ... pn-1 <> x, pn].

Example:

> { semi punctuate: [text: "a", text: "b", char: $c] } doc
[a;, b;, c]

Rendering

p render &mode: @page &line-length: 100 &ribbons-per-line: 1.5  String
  | p is-a?: Pretty

Renders the document as a string.

Valid settings for mode:

@page

normal

@zig-zag

with zig-zag cuts

@left

no indentation; infinitely long lines

@one-line

all on one line

Example:

> (1 -> $1) pretty render
"1 -> $1"