Module Fmt
Format
pretty-printer combinators.
Consult naming conventions for your pretty-printers.
References
- The
Format
module documentation. - The required reading
Format
module tutorial.
v0.8.5 - homepage
Formatting
val pf : Stdlib.Format.formatter -> ('a, Stdlib.Format.formatter, unit) Stdlib.Pervasives.format -> 'a
pf
isFormat
.fprintf.
val kpf : (Stdlib.Format.formatter -> 'a) -> Stdlib.Format.formatter -> ('b, Stdlib.Format.formatter, unit, 'a) Stdlib.format4 -> 'b
kpf
isFormat
.kfprintf.
val strf : ('a, Stdlib.Format.formatter, unit, string) Stdlib.format4 -> 'a
strf
isFormat
.asprintf.Note. When using
strf
utf_8
andstyle_renderer
are always respectively set totrue
and`None
. See alsostrf_like
.
val kstrf : (string -> 'a) -> ('b, Stdlib.Format.formatter, unit, 'a) Stdlib.format4 -> 'b
kstrf
is likestrf
but continuation based.
val strf_like : Stdlib.Format.formatter -> ('a, Stdlib.Format.formatter, unit, string) Stdlib.format4 -> 'a
strf_like ppf
is likestrf
except itsutf_8
andstyle_renderer
settings are those ofppf
.
val with_buffer : ?like:Stdlib.Format.formatter -> Stdlib.Buffer.t -> Stdlib.Format.formatter
with_buffer ~like b
is a formatter whoseutf_8
andstyle_renderer
settings are copied from those oflike
(if provided).
Formatting to standard outputs
val stdout : Stdlib.Format.formatter
stdout
is the standard output formatter.
val stderr : Stdlib.Format.formatter
stderr
is the standard error formatter.
val pr : ('a, Stdlib.Format.formatter, unit) Stdlib.format -> 'a
pr
ispf stdout
.
val epr : ('a, Stdlib.Format.formatter, unit) Stdlib.format -> 'a
epr
ispf stderr
.
Formatting exceptions
val failwith : ('a, Stdlib.Format.formatter, unit, 'b) Stdlib.format4 -> 'a
failwith
iskstrf failwith
, raisesPervasives
.Failure with a pretty-printed string argument.
val invalid_arg : ('a, Stdlib.Format.formatter, unit, 'b) Stdlib.format4 -> 'a
invalid_arg
iskstrf invalid_arg
, raisesPervasives
.Invalid_argument with a pretty-printed string argument.
Formatters
type 'a t
= Stdlib.Format.formatter -> 'a -> unit
The type for formatters of values of type
'a
.
val nop : 'a t
nop
formats nothing.
val cut : unit t
cut
isFormat
.pp_print_cut.
val sp : unit t
sp
isFormat
.pp_print_space.
val unit : (unit, Stdlib.Format.formatter, unit) Stdlib.Pervasives.format -> unit t
unit fmt
formats a unit value with the formatfmt
.
val fmt : ('a, Stdlib.Format.formatter, unit) Stdlib.Pervasives.format -> Stdlib.Format.formatter -> 'a
fmt fmt ppf
ispf ppf fmt
. Iffmt
is used with a single non-constant formatting directive, generates a value of typet
.
val always : (unit, Stdlib.Format.formatter, unit) Stdlib.Pervasives.format -> 'a t
always fmt ppf v
formats any value with the constant formatfmt
.
Base type formatters
val bool : bool t
bool
isFormat
.pp_print_bool.
val int : int t
int
ispf ppf "%d"
.
val nativeint : nativeint t
nativeint ppf
ispf ppf "%nd"
.
val int32 : int32 t
int32 ppf
ispf ppf "%ld"
.
val int64 : int64 t
int64 ppf
ispf ppf "%Ld"
.
val uint : int t
uint ppf
ispf ppf "%u"
.
val unativeint : nativeint t
unativeint ppf
ispf ppf "%nu"
.
val uint32 : int32 t
uint32 ppf
ispf ppf "%lu"
.
val uint64 : int64 t
uint64 ppf
ispf ppf "%Lu"
.
val float : float t
float ppf
ispf ppf "%g".
val float_dfrac : int -> float t
float_dfrac d
rounds the float to thed
th decimal fractional digit and formats the result with"%g"
. Ties are rounded towards positive infinity. The result is only defined for0 <= d <= 16
.
val float_dsig : int -> float t
float_dsig d
rounds the normalized decimal significand of the float to thed
th decimal fractional digit and formats the result with"%g"
. Ties are rounded towards positive infinity. The result is NaN on infinities and only defined for0 <= d <= 16
.Warning. The current implementation overflows on large
d
and floats.
val char : char t
char
isFormat
.pp_print_char.
val string : string t
string
isFormat
.pp_print_string.
val buffer : Stdlib.Buffer.t t
buffer
formats aBuffer
.t value's current contents.
val exn : exn t
exn
formats an exception.
val exn_backtrace : (exn * Stdlib.Printexc.raw_backtrace) t
exn_backtrace
formats an exception backtrace.
Polymorphic type formatters
These formatters give full control to the client over the formatting process and do not wrap the formatted structures with boxes. Use the Dump
module to quickly format values for inspection.
val pair : ?sep:unit t -> 'a t -> 'b t -> ('a * 'b) t
pair ~sep pp_fst pp_snd
formats a pair. The first and second projection are formatted usingpp_fst
andpp_snd
and are separated bysep
(defaults tocut
).
val option : ?none:unit t -> 'a t -> 'a option t
option ~none pp_v
formats an optional value. TheSome
case usespp_v
andNone
usesnone
(defaults tonop
).
val result : ok:'a t -> error:'b t -> ('a, 'b) Result.result t
result ~ok ~error
formats a result value usingok
for theOk
case anderror
for theError
case.
val list : ?sep:unit t -> 'a t -> 'a list t
list sep pp_v
formats list elements. Each element of the list is formatted in order withpp_v
. Elements are separated bysep
(defaults tocut
). If the list is empty, this isnop
.
val array : ?sep:unit t -> 'a t -> 'a array t
array sep pp_v
formats array elements. Each element of the array is formatted in order withpp_v
. Elements are separated bysep
(defaults tocut
). If the array is empty, this isnop
.
val hashtbl : ?sep:unit t -> ('a * 'b) t -> ('a, 'b) Stdlib.Hashtbl.t t
hashtbl ~sep pp_binding
formats the bindings of a hash table. Each binding is formatted withpp_binding
and bindings are separated bysep
(defaults tocut
). If the hash table has multiple bindings for a given key, all bindings are formatted, with the most recent binding first. If the hash table is empty, this isnop
.
val queue : ?sep:unit t -> 'a t -> 'a Stdlib.Queue.t t
queue ~sep pp_v
formats queue elements. Each element of the queue is formatted in least recently added order withpp_v
. Elements are separated bysep
(defaults tocut
). If the queue is empty, this isnop
.
val stack : ?sep:unit t -> 'a t -> 'a Stdlib.Stack.t t
stack ~sep pp_v
formats stack elements. Each element of the stack is formatted from top to bottom order withpp_v
. Elements are separated bysep
(defaults tocut
). If the stack is empty, this isnop
.
val iter : ?sep:unit t -> (('a -> unit) -> 'b -> unit) -> 'a t -> 'b t
iter ~sep iter pp_elt
formats the iterations ofiter
over a value usingpp_elt
. Iterations are separated bysep
(defaults tocut
).
val iter_bindings : ?sep:unit t -> (('a -> 'b -> unit) -> 'c -> unit) -> ('a * 'b) t -> 'c t
iter_bindings ~sep iter pp_binding
formats the iterations ofiter
over a value usingpp_binding
. Iterations are separated bysep
(defaults tocut
).
module Dump : sig ... end
Formatters for inspecting OCaml values.
Boxes
val box : ?indent:int -> 'a t -> 'a t
box ~indent pp ppf
wrapspp
in a horizontal or vertical box. Break hints that lead to a new line addindent
to the current indentation (defaults to0
).
val hbox : 'a t -> 'a t
hbox
is likebox
but is a horizontal box: the line is not split in this box (but may be in sub-boxes).
Brackets
Words, paragraphs, text and lines
Note. These functions only work on US-ASCII strings and/or with newlines ('\n'
). If you are dealing with UTF-8 strings or different kinds of line endings you should use the pretty-printers from Uuseg_string
.
White space. White space is one of the following US-ASCII characters: space ' '
(0x20
), tab '\t'
(0x09
), newline '\n'
(0x0A
), vertical tab (0x0B
), form feed (0x0C
), carriage return '\r'
(0x0D
).
val words : string t
words
formats words by suppressing initial and trailing white space and replacing consecutive white space with a singleFormat
.pp_print_space.
val paragraphs : string t
paragraphs
formats paragraphs by suppressing initial and trailing spaces and newlines, replacing blank lines (a line made only of white space) by a twoFormat
.pp_force_newline and remaining consecutive white space with a singleFormat
.pp_print_space.
val text : string t
text
formats text by respectively replacing spaces and newlines in the string withFormat
.pp_print_space andFormat
.pp_force_newline.
val lines : string t
lines
formats lines by replacing newlines ('\n'
) in the string with calls toFormat
.pp_force_newline.
val text_loc : ((int * int) * (int * int)) t
text_loc
formats a line-column text range according to GNU conventions.
Appending
Byte sizes
val byte_size : int t
byte_size
formats a byte size according to its magnitude using SI prefixes up to peta bytes (1015).
val bi_byte_size : int t
bi_byte_size
formats a byte size according to its magnitude using binary prefixes up to pebi bytes (215).
Conditional UTF-8 formatting
Note. Since Format
is not UTF-8 aware using UTF-8 output may derail the pretty printing process. Use the pretty-printers from Uuseg_string
if you are serious about UTF-8 formatting.
val if_utf_8 : 'a t -> 'a t -> 'a t
if_utf_8 pp_u pp ppf v
is:pp_u ppf v
ifutf_8 ppf
istrue
.pp ppf v
otherwise.
val utf_8 : Stdlib.Format.formatter -> bool
utf_8 ppf
istrue
if UTF-8 output is enabled onppf
. Ifset_utf_8
hasn't been called onppf
this istrue
.
val set_utf_8 : Stdlib.Format.formatter -> bool -> unit
set_utf_8 ppf b
enables or disables conditional UTF-8 formatting onppf
.Warning. Using this function replaces any
Format
.tag functions that may be in place.- raises Invalid_argument
if
ppf
isFormat
.str_formatter: it is is always UTF-8 enabled.
Styled formatting
type style
=[
|
`Bold
|
`Underline
|
`Black
|
`Red
|
`Green
|
`Yellow
|
`Blue
|
`Magenta
|
`Cyan
|
`White
|
`None
]
The type for styles.
val styled_unit : style -> (unit, Stdlib.Format.formatter, unit) Stdlib.Pervasives.format -> unit t
styled_unit s fmt
isstyle s (unit fmt)
.
Style rendering control
type style_renderer
=[
|
`Ansi_tty
|
`None
]
The type for style renderers.
`Ansi_tty
, renders styles using ANSI escape sequences.`None
, styled rendering has no effect.
val style_renderer : Stdlib.Format.formatter -> style_renderer
style_renderer ppf
is the style renderer used byppf
. Ifset_style_renderer
has never been called onppf
this is`None
.
val set_style_renderer : Stdlib.Format.formatter -> style_renderer -> unit
set_style_renderer ppf r
sets the style renderer ofppf
tor
.Warning. Using this function replaces any
Format
.tag functions that may be in place.- raises Invalid_argument
if
ppf
isFormat
.str_formatter: its renderer is always`None
.
Converting with string value converters
val of_to_string : ('a -> string) -> 'a t
of_to_string f ppf v
isstring ppf (f v)
.
val to_to_string : 'a t -> 'a -> string
to_to_string pp_v v
isstrf "%a" pp_v v
.
Naming conventions
Given a type ty
use:
pp_ty
for a pretty printer that provides full control to the client and does not wrap the formatted value in an enclosing box. See these examples.dump_ty
for a pretty printer that provides little control over the pretty-printing process, wraps the rendering in an enclosing box and tries as much as possible to respect the OCaml syntax. These pretty-printers should make it easy to inspect and understand values of the given type, they are mainly used for quick printf debugging and/or toplevel interaction. See these examples.
If you are in a situation where making a difference between dump_ty
and pp_ty
doesn't make sense then use pp_ty
.
For a type ty
that is the main type of the module (the "M.t
" convention) drop the suffix, that is simply use M.pp
and M.dump
.