package containers
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=92143ceb4785ae5f8a07f3ab4ab9f6f32d31ead0536e9be4fdb818dd3c677e58
sha512=5fa80189d0e177af2302b48e72b70299d51fc36ac2019e1cbf389ff6a7f4705b10089405b5a719b3e4845b0d1349a47a967f865dc2e4e3f0d5a0167ef6c31431
doc/containers.pp/Containers_pp/index.html
Module Containers_pp
Source
Pretty printing of documents.
A document is a structured tree of text with formatting instructions.
It can be rendered into a string ("pretty printed"), see Pretty
.
This follows Wadler's paper "A prettier printer", but with some changes in the rendering part because we can't rely on lazyness to make the algebraic implementation efficient.
Some general considerations: the type t
is the type of documents, a tree with text leaves that is pretty printed within a given width.
Layout is controlled via the combination of a few primitives:
newline
will either print a space or a newline. It is similar toFormat
's"@ "
in that sense. A big difference withFormat
is that by defaultnewline
is actually a newline. It only becomes a space if it's in agroup
small enough to fit in the remainder of the current line.group d
tries to writed
on a single line if there's room. If not, it has no effect.nest n d
increases the indentation level insided
. Any newline that is rendered as a new line is indented byn
more spaces (which are cumulative with surroundingnest
calls).append a b
(ora ^ b
) just printsa
followed byb
.fill d
is a bit likegroup
but it will try to cram as much as possible on each line. It is not all-or-nothing likegroup
.
Core
The type of documents
Text, with a Printf
-compatible format.
For example, textpf "%d-%d" 4 2
is like text "4-2"
.
nest n d
increases indentation by n
inside d
. If current indentation is m
, then every newline inside d
will be followed by n + m
leading spaces.
Group the documents inside this.
Newlines immediately inside this group will either render as new lines or as spaces, depending on the width available.
newline_or_spaces n
either prints a newline (respecting indentation), or prints n
spaces. newline
is basically newline_or_spaces 1
.
fill sep l
resembles group (append_l ~sep l)
, except it tries to put as many items of l
as possible on each line.
In terms of Format
, this is like the hov box.
Output device
Extensibility
ext e v d
wraps d
with value v
.
It is a document that has the same shape (and size) as d
, except that additional data will be output when it is rendered using extension e
.
When this is rendered, first e.pre out v
is called; then d
is printed; then e.post out v
is called. Here out
is the output buffer/stream for rendering.
Pretty print and rendering
Pretty-print, using Pretty
and an unspecified margin.
Convenience functions
append_l ?sep l
is the concatenation of elements of l
, separated by sep
(default nil
)
append_sp l
is the concatenation of elements of l
, separated by ' '
of_list f l
maps each element of l
to a document and concatenates them.
bracket2 l d r
groups d
, indented by 2, between brackets l
and r
sexp_apply a l
is the S-expr "(text a …l)"
, pretty-printed
Simple colors in terminals