Karya, built on 2018-05-31T02:46:59 (patch 0a1a35479c514820d77330ae8a978975ba22a47a)

Util.Format

Description

This is a library to lay out text with line wrapping and indenting.

The basic theory is that you concatenate text with BreakTypes. In addition, you can increment the indent level with withIndent. When render wraps the text, it will break on the lowest indent level, or as soon as the indent level decreases.

A further wrinkle is that you can mark alternate layouts with shortForm.

Synopsis

# Documentation

data Doc Source #

Instances
 # Instance detailsDefined in Util.Format Methods(==) :: Doc -> Doc -> Bool #(/=) :: Doc -> Doc -> Bool # # Instance detailsDefined in Util.Format MethodsshowsPrec :: Int -> Doc -> ShowS #showList :: [Doc] -> ShowS # # Instance detailsDefined in Util.Format Methods # Instance detailsDefined in Util.Format Methods(<>) :: Doc -> Doc -> Doc #stimes :: Integral b => b -> Doc -> Doc # # Instance detailsDefined in Util.Format Methodsmappend :: Doc -> Doc -> Doc #mconcat :: [Doc] -> Doc # # Instance detailsDefined in Util.Pretty MethodsformatList :: [Doc] -> Doc Source #

The first Doc is the short form, which will be used if it doesn't have to wrap. So you can give a compact form which will render if it can fit without breaking, and then a form with more fancy layout that will be used if it does have to break. For example, [1, 2, 3] for short lists, and [1\n, 2\n, 3\n] for long ones.

Prepending text to a shortForm will distribute over both short and long forms. Otherwise, if you write "prefix " <> x, and x happens to be a shortForm, the long form loses the prefix.

Appending two shortForms will make you lose the long form of the second one. So don't do that. TODO I'd rather both short and long forms be appended, but haven't figured out how to do that yet.

(</>) :: Doc -> Doc -> Doc infixr 5 Source #

Soft break with no space.

(<+/>) :: Doc -> Doc -> Doc infixr 5 Source #

Soft break with a space.

(<//>) :: Doc -> Doc -> Doc infixr 4 Source #

Hard break with a single newline.

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

Join two docs with a space.

Insert a number of newlines.

Consecutive breaks are merged together, and a hard break always wins. Also, multiple hard breaks are merged into one, and ones with greater newlines win other those with fewer. The rationale is that if you are formatting a list of sub-Docs, and you want to put each on its own line, you need a hard break after each one, but if one of them does the same thing, you wind up with two breaks in a row.

unlines :: [Doc] -> Doc Source #

Analogous to unlines, terminate each Doc with a newline.

paragraphs :: [Doc] -> Doc Source #

This is just like unlines, but separate docs with two newlines, and terminate the last with one.

wrap :: [Doc] -> Doc Source #

Increase the indent level for the given Doc. The indent change only takes effect after the first break, so if you want it to take effect immediately, use one of indent, indent_, or indentLine.

The reason indent is delayed is that this way you can do a hanging indent, where the current line is unindented, but it will be indented if it wraps. Otherwise you don't know where to put the indent, since you don't know where the break will happen.

Change the indent level and add a no-space break so it takes effect immediately.

Change the indent level and add a spaced break so it takes effect immediately.

Change the indent level and add a hard break so it takes effect immediately.

type Width = Int Source #

Width of monospace text, in characters.

Render a Doc, wrapping after the given Width.

Render the Doc all on one line, with no newlines.

Merge Texts so the Doc is easier to read.

Reduce the Doc to a flattened easier to read version.