B0_std.FmtTextual formatters.
Helpers for dealing with Format.
pf is Format.fprintf.
val kpf :
(Stdlib.Format.formatter -> 'a) ->
Stdlib.Format.formatter ->
('b, Stdlib.Format.formatter, unit, 'a) Stdlib.format4 ->
'bkpf is Format.kfprintf.
kstr is Format.kasprintf.
failwith fmt ... is kstr failwith fmt ...
failwith_notrace is like failwith but Failure is raised with raise_notrace.
failwith_line fmt … is failwith ("%d:" ^^ fmt) ….
invalid_arg fmt ... is kstr invalid_arg fmt ...
error fmt ... is kstr (fun s -> Error s) fmt ...
val flush : 'a tflush has the effect of Format.pp_print_flush.
val flush_nl : 'a tflush_nl has the effect of Format.pp_print_newline.
val nop : 'a tnop formats nothing.
val any : (unit, Stdlib.Format.formatter, unit) Stdlib.format -> 'a tany fmt formats any value with fmt.
val cut : 'a tcut has the effect of Format.pp_print_cut.
val sp : 'a tsp has the effect of Format.pp_print_space.
val sps : int -> 'a tsps n has the effect of Format.pp_print_break n 0.
suffix_lines ~suffix pp adds suffix to the lines formatted by pp. Note. Not very compositional: first formats to a string, cuts the string and then formats the lines.
iter ~sep iter pp_elt formats the iterations of iter over a value using pp_elt. Iterations are separated by sep (defaults to cut).
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).
concat ~sep pps concatenates the formatters pps separating them with sep (defaults to cut).
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).
hbox is like box but is a horizontal box: the line is not split in this box (but may be in sub-boxes).
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).
quote ~mark pp_v ppf is pf "@[<1>@<1>%s%a@<1>%s@]" mark pp_v mark, mark defaults to "\"", it is always counted as spanning as single column (this allows for UTF-8 encoded marks).
field ~label ~sep l prj pp_v pretty prints a labelled field value as pf "@[<1>%a%a%a@]" label l sep () (using prj pp_v). label defaults to st [`Yellow] and sep to any ":@ ".
record ~sep fields pretty-prints a value using the concatenation of fields, separated by sep (defaults to cut) and framed in a vertical box.
Formatters for structures give full control to the client over the formatting process and do not wrap the formatted structures with boxes. If you want to print literal OCaml values use the OCaml module.
module OCaml : sig ... endFormatting OCaml literal values.
val bool : bool tbool is Format.pp_print_bool.
val int : int tint is Format.pp_print_int.
val uint32 : int32 tuint32 is pf ppf "%lu".
val int32 : int32 tint32 is pf ppf "%ld".
val int64 : int64 tint64 is pf ppf "%Ld".
val uint64 : int64 tuint64 is pf ppf "%Lu".
val nativeint : nativeint tnativeint is pf ppf "%nd".
val nativeuint : nativeint tnativeuint is pf ppf "%nu".
val float : float tfloat is pf ppf "%g".
val char : char tchar is Format.pp_print_char.
val string : string tstring is Format.pp_print_string.
val binary_string : string tbinary_string formats strings as ASCII hex. See also Text and lines.
val bytes : bytes tbytes formats bytes as ASCII hex. See also Text and lines.
val sys_signal : int tsys_signal formats an OCaml signal number as a C POSIX constant or "SIG(%d)" if the signal number is unknown.
val backtrace : Stdlib.Printexc.raw_backtrace tbacktrace formats a backtrace.
val exn : exn texn formats an exception using Printexc.to_string.
val exn_backtrace : (exn * Stdlib.Printexc.raw_backtrace) texn_backtrace formats an exception backtrace using exn and the format use by the OCaml runtime system.
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 none : unit tnone is any "<none>".
option ~none pp_v formats an option. The Some case uses pp_v and None uses none (defaults to nop).
either ~left ~right formats an Either.t value. The Left case uses left and the Right cases uses right.
result ~ok ~error formats a result value. The Ok case uses ok and the Error case uses error.
val text : string ttext is Format.pp_print_text. FIXME. replace this by the behaviour of styledtext.
val styled_text : string tstyled_text formats ANSI escape sequences as zero width characters, collapses multiple newlines and spaces to a single Fmt.sp and preserves blank lines and suppresses trailing space and newlines.
val lines : string tlines formats lines by replacing newlines ('\n') in the string with calls to Format.pp_force_newline.
val truncated : max:int -> string ttruncated ~max formats a string using at most max characters. If the string doesn't fit, it is truncated and ended with … (U+2026) which does count towards max.
val ascii_char : char tascii_char prints Char.Ascii.is_print characters and hex escapes for other characters.
val ascii_string : string tascii_string prints Char.Ascii.is_print characters and hex escapes for other characters.
val text_uchar : Stdlib.Uchar.t ttext_uchar formats an UTF-8 encoded Unicode character or its scalar value in U+%04X representation if u is in C0 control characters (U+0000-U+001F), C1 control characters (U+0080-U+009F), line separator (U+2028), paragraph separator (U+2029), left-to-right mark (U+200E) or right-to-left mark (U+200F).
val text_string : string ttext_string formats an UTF-8 encoded string but escapes: C0 control characters (U+0000-U+001F), C1 control characters (U+0080-U+009F), line separator (U+2028) and paragraph separator (U+2029). Decoding errors are replaced by literal Uchar.rep characters.
val styled_text_string : string tstyled_text_string is like text_string but ANSI escape sequences are detected and output as zero width strings.
val text_bytes : bytes ttext_bytes is like text_string but on bytes values.
FIXME. Get rid of these in favour of OCaml
val ascii_string_literal : string tascii_string_literal is like ascii_string but between double quotes '\"'. Double quotes are also escaped and CR an LF are escaped by '\r' and '\n'. The result is a valid, single line, OCaml string.
val text_string_literal : string ttext_string_literal is like text_string but between double quotes '\"'. Double quotes are also escaped an CR and LF are escaped by '\r' and '\n'. The result is a valid, single line, OCaml string.
val styled_text_string_literal : string tstyled_text_string_literal combines text_string_literal and styled_text_string.
val si_size : scale:int -> string -> int tsi_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). The output is UTF-8 encoded, it uses U+03BC for µ (10-6).
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 tbyte_size is si_size ~scale:0 "B".
val uint64_ns_span : int64 tuint64_ns_span formats an unsigned nanosecond time span according to its magnitude using SI prefixes on seconds and non-SI units. Years are counted in Julian years (365.25 SI-accepted days) as defined by the International Astronomical Union.
Rounds towards positive infinity, i.e. over approximates, no duration is formatted shorter than it is.
The output is UTF-8 encoded, it uses U+03BC for µs (10-6s).
and_enum ~empty pp_v ppf l formats l according to its length.
0, formats empty (defaults to nop).1, formats the element with pp_v.2, formats "%a and %a" with the list elementsn, formats "%a, ... and %a" with the list elementsor_enum is like and_enum but uses "or" instead of "and".
unknown ~kind pp_v ~hint (v, hints) formats unknown followed by a space and hint pp_v hints if hints is non-empty.
cardinal ?zero ~one ?other () formats an integer by selecting a formatter according to the cardinal english plural form of its absolute value n:
zero, if n = 0. Defaults to other (as per english rules).one, if n = 1.other, otherwise. Defaults to one followed by a 's' character.val ordinal :
?zero:int t ->
?one:int t ->
?two:int t ->
?three:int t ->
?other:int t ->
unit ->
int tordinal ?zero ?one ?two ?three ?other () formats an integer by selecting a formatter according to the ordinal english plural form of its absolute value n:
zero, if n = 0. Defaults to other (as per english rules).one, if n mod 10 = 1 && n mod 100 <> 11. Defaults to "%dst".two, if n mod 10 = 2 && n mod 100 <> 12. Defaults to "%dnd".three, if n mod 10 = 3 && n mod 100 <> 13. Defaults to "%drd".other otherwise. Defaults to "%dth".Text styling control happens via ANSI escape sequences and is handled globally. See the cookbook for more information.
The kind of styler.
val set_styler : styler -> unitset_styler styler sets the global styler to styler.
Use B0_std_cli.set_no_color to set this from the command line.
val styler : unit -> stylerstyler is the global styler. The initial value is Ansi or Plain if one of the following conditions is satisified:
NO_COLOR environment variable is set and different from the empty string. Yes, even if you have NO_COLOR=0, that's what the dumb https://no-color.org standard says.TERM environment variable is dumb.TERM environment variable is unset and Sys.backend_type is not Other "js_of_ocaml" (browser consoles support ANSI escapes).strip_styles ppf, this overrides the formatter's out_string function to strip out styling information. See also String.strip_ansi_escapes.
Note. This assumes ppf is not used to write data that uses raw U+001B characters.
type color = [ | `Default| `Black| `Black_bright| `Red| `Red_bright| `Green| `Green_bright| `Yellow| `Yellow_bright| `Blue| `Blue_bright| `Magenta| `Magenta_bright| `Cyan| `Cyan_bright| `White| `White_bright ]The type for colors.
Note. The actual color may differ in terminals. See for example the table here and check out the output of the b0-sttyle tool.
type style = [ | `Bold| `Faint| `Italic| `Underline| `Blink of [ `Slow | `Rapid ]| `Reverse| `Fg of color| `Bg of color ]The type for text styles.
st' styles pp_v ppf v formats v with pp_v on ppf styled by styles.
val code : string tcode is st [`Bold].
val hey : string they is st [`Bold; `Fg `Red].
val puterr : unit tputerr formats Error: with [`Fg `Red].
val putwarn : unit tputwarn formats Warning: with [`Fg `Yellow].
val putnote : unit tputnote formats Note: with [`Fg `Blue].