package b0

  1. Overview
  2. Docs

Textual formatters.

Helpers for dealing with Format.

Standard outputs and formatters

val stdout : Format.formatter

stdout outputs to standard output.

val stderr : Format.formatter

stderr outputs to standard error.

val flush : Format.formatter -> unit

Formatting

val pf : Format.formatter -> ('a, Format.formatter, unit) format -> 'a
val pr : ('a, Format.formatter, unit) format -> 'a
val epr : ('a, Format.formatter, unit) format -> 'a
val str : ('a, Format.formatter, unit, string) format4 -> 'a
val kpf : (Format.formatter -> 'a) -> Format.formatter -> ('b, Format.formatter, unit, 'a) format4 -> 'b
val kstr : (string -> 'a) -> ('b, Format.formatter, unit, 'a) format4 -> 'b
val failwith : ('b, Format.formatter, unit, 'a) format4 -> 'b

failwith fmt ... is kstr (fun s -> failwith s) fmt ...

val failwith_notrace : ('b, Format.formatter, unit, 'a) format4 -> 'b

failwith_notrace is like nt but Failure is raised with raise_notrace.

val invalid_arg : ('b, Format.formatter, unit, 'a) format4 -> 'b

invalid_arg fmt ... is kstr (fun s -> invalid_arg s) fmt ...

val error : ('b, Format.formatter, unit, ('a, string) result) format4 -> 'b

error fmt ... is kstr (fun s -> Error s) fmt ...

Formatters

type 'a t = Format.formatter -> 'a -> unit

The type for formatter of values of type 'a.

val nop : 'a t

nop formats nothing.

val unit : (unit, Format.formatter, unit) Pervasives.format -> unit t

unit fmt formats a unit value with the format fmt.

val cut : unit t
val sp : unit t
val comma : unit t

comma is unit ",@ ".

Base type formatters

val bool : bool t
val int : int t
val int32 : int32 t

int32 is pf ppf "%ld".

val int64 : int64 t

int64 is pf ppf "%Ld".

val float : float t

float is pf ppf "%g".

val char : char t
val string : string t
val elided_string : max:int -> string t

elieded_string ~max formats a string using at most max characters, eliding it if it is too long with three consecutive dots which do count towards max.

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 using pp_fst and pp_snd and are separated by sep (defaults to cut).

val list : ?empty:unit t -> ?sep:unit t -> 'a t -> 'a list t

list ~sep pp_v formats list elements. Each element of the list is formatted in order with pp_v. Elements are separated by sep (defaults to cut). If the list is empty, this is empty (defaults to nop).

val array : ?empty:unit t -> ?sep:unit t -> 'a t -> 'a array t

array ~sep pp_v formats array elements. Each element of the array is formatted in in order with pp_v. Elements are seperated by sep (defaults to cut). If the array is empty this is empty (defauls to nop).

val option : ?none:unit t -> 'a t -> 'a option t

option ~none pp_v formats an option. The Some case uses pp_v and None uses none (defaults to nop).

val none : unit t

none is unit "<none>".

val iter : ?sep:unit t -> (('a -> unit) -> 'b -> unit) -> 'a t -> 'b t

iter ~sep iter pp_elt formats the iterations of iter over a value using pp_elt. Iterations are separated by sep (defaults to cut).

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 of iter over a value using pp_binding. Iterations are separated by sep (defaults to cut).

val text : string t
val lines : string t

lines formats lines by replacing newlines ('\n') in the string with calls to Format.pp_force_newline.

val exn : exn t

exn formats an exception.

val exn_backtrace : (exn * Printexc.raw_backtrace) t

exn_backtrace formats an exception backtrace.

val sys_signal : int t

sys_signal formats an OCaml signal number as a C POSIX constant or "SIG(%d)" if the signal number is unknown.

Boxes

val box : ?indent:int -> 'a t -> 'a t

box ~indent pp ppf wraps pp in a horizontal or vertical box. Break hints that lead to a new line add indent to the current indentation (defaults to 0).

val hbox : 'a t -> 'a t

hbox is like box but is a horizontal box: the line is not split in this box (but may be in sub-boxes).

val vbox : ?indent:int -> 'a t -> 'a t

vbox is like box but is a vertical box: every break hint leads to a new line which adds indent to the current indentation (default to 0).

val hvbox : ?indent:int -> 'a t -> 'a t

hvbox is like box but is either hbox if its fits on a single line or vbox otherwise.

Quoting

val squotes : 'a t -> 'a t

squotes pp_v is pf "'%a'" pp_v

val dquotes : 'a t -> 'a t

dquotes pp_v is pf "\"%a\"" pp_v

Brackets

val parens : 'a t -> 'a t

parens pp_v ppf is pf ppf "@[<1>(%a)@]" pp_v.

val brackets : 'a t -> 'a t

brackets pp_v ppf is pf ppf "@[<1>[%a]@]" pp_v.

val braces : 'a t -> 'a t

braces pp_v ppf is pf ppf "@[<1>{%a}@]" pp_v.

ANSI TTY styling

val set_tty_styling_cap : Tty.cap -> unit

set_tty_styling_cap c sets the global styling capabilities to c. Affects the output of tty_str and tty.

val tty_styling_cap : unit -> Tty.cap

tty_styling_cap () is the global styling capability.

val tty_string : Tty.style list -> string t

tty_string styles ppf s prints s on ppf according to styles and the value of tty_styling_cap.

val tty : Tty.style list -> 'a t -> 'a t

tty styles pp_v ppf v prints v with pp_v on ppf according to styles and the value of tty_styling_cap.

Alternatives

val one_of : ?empty:unit t -> 'a t -> 'a list t

one_of ~empty pp_v ppf l formats according to the length of l

  • 0, formats empty (defaults to nop).
  • 1, formats the element with pp_v.
  • 2, formats "either %a or %a" with the list elements
  • n, formats "one of %a, ... or %a" with the list elements
val did_you_mean : ?pre:unit t -> ?post:unit t -> kind:string -> 'a t -> ('a * 'a list) t

did_you_mean ~pre kind ~post pp_v formats a faulty value v and a list of hints that could have been mistaken for v. pre defaults to unit "Unknown", post to nop. Hints are formatted using one_of.

Fields

val field : ?style:Tty.style list -> string -> 'a t -> 'a t

field ~style l pp_v pretty prints a named field with label l styled according to style (defaults to [`Fg `Yellow]), using pp_v to print the value.

Magnitudes

val si_size : scale:int -> string -> int t

si_size ~scale unit formats a non negative integer representing unit unit at scale 10scale * 3, depending on its magnitude, using power of 3 SI prefixes (i.e. all of them except deca, hector, deci and centi). Only US-ASCII characters are used, µ (10-6) is written using u.

scale indicates the scale 10scale * 3 an integer represents, for example -1 for munit (10-3), 0 for unit (100), 1 for kunit (103); it must be in the range [-8;8] or Invalid_argument is raised.

Except at the maximal yotta scale always tries to show three digits of data with trailing fractional zeros omited. Rounds towards positive infinity (over approximates).

val byte_size : int t

byte_size is si_size ~scale:0 "B".

val uint64_ns_span : int64 t

uint64_ns_span formats an unsigned nanosecond time span according to its magnitude using SI prefixes on seconds and accepted non-SI units. Years are counted in Julian years (365.25 SI-accepted days) as defined by the International Astronomical Union (IAU). Only US-ASCII characters are used (us is used for µs).

OCaml

Innovation. Community. Security.