Documentation
containers lib
CCFormat
Module
type 'a sequence = ('a -> unit) -> unit
include module type of struct include Stdlib .Format end
val open_box : int -> unit
val close_box : unit -> unit
val open_hbox : unit -> unit
val open_vbox : int -> unit
val open_hvbox : int -> unit
val pp_open_hovbox : formatter -> int -> unit
val open_hovbox : int -> unit
val pp_print_string : formatter -> string -> unit
val print_string : string -> unit
val pp_print_bytes : formatter -> bytes -> unit
val print_bytes : bytes -> unit
val pp_print_as : formatter -> int -> string -> unit
val print_as : int -> string -> unit
val print_int : int -> unit
val pp_print_float : formatter -> float -> unit
val print_float : float -> unit
val pp_print_char : formatter -> char -> unit
val print_char : char -> unit
val pp_print_bool : formatter -> bool -> unit
val print_bool : bool -> unit
val pp_print_space : formatter -> unit -> unit
val print_space : unit -> unit
val print_cut : unit -> unit
val pp_print_break : formatter -> int -> int -> unit
val print_break : int -> int -> unit
val pp_print_custom_break :
formatter ->
fits :(string * int * string) ->
breaks :(string * int * string) ->
unit
val pp_force_newline : formatter -> unit -> unit
val force_newline : unit -> unit
val pp_print_if_newline : formatter -> unit -> unit
val print_if_newline : unit -> unit
val pp_print_flush : formatter -> unit -> unit
val print_flush : unit -> unit
val pp_print_newline : formatter -> unit -> unit
val print_newline : unit -> unit
val set_margin : int -> unit
val get_margin : unit -> int
val pp_set_max_indent : formatter -> int -> unit
val set_max_indent : int -> unit
val pp_get_max_indent : formatter -> unit -> int
val get_max_indent : unit -> int
type geometry = Stdlib__Format .geometry = {
max_indent : int;
margin : int;
}
val pp_set_geometry : formatter -> max_indent :int -> margin :int -> unit
val set_geometry : max_indent :int -> margin :int -> unit
val pp_safe_set_geometry : formatter -> max_indent :int -> margin :int -> unit
val safe_set_geometry : max_indent :int -> margin :int -> unit
val pp_set_max_boxes : formatter -> int -> unit
val set_max_boxes : int -> unit
val pp_get_max_boxes : formatter -> unit -> int
val get_max_boxes : unit -> int
val pp_over_max_boxes : formatter -> unit -> bool
val over_max_boxes : unit -> bool
val open_tbox : unit -> unit
val pp_close_tbox : formatter -> unit -> unit
val close_tbox : unit -> unit
val set_tab : unit -> unit
val print_tab : unit -> unit
val pp_print_tbreak : formatter -> int -> int -> unit
val print_tbreak : int -> int -> unit
val pp_set_ellipsis_text : formatter -> string -> unit
val set_ellipsis_text : string -> unit
val pp_get_ellipsis_text : formatter -> unit -> string
val get_ellipsis_text : unit -> string
type stag = Stdlib__Format .stag = ..
val open_stag : stag -> unit
val pp_close_stag : formatter -> unit -> unit
val close_stag : unit -> unit
val stdbuf : Stdlib .Buffer.t
type symbolic_output_item = Stdlib__Format .symbolic_output_item =
| Output_flush
| Output_newline
| Output_string of string
| Output_spaces of int
| Output_indent of int
type symbolic_output_buffer = Stdlib__Format .symbolic_output_buffer
val pp_print_text : formatter -> string -> unit
val pp_print_either :
left :(formatter -> 'a -> unit) ->
right :(formatter -> 'b -> unit) ->
formatter ->
('a , 'b ) Stdlib .Either.t ->
unit
val printf : ('a , formatter , unit) Stdlib .format -> 'a
val eprintf : ('a , formatter , unit) Stdlib .format -> 'a
val asprintf : ('a , formatter , unit, string) Stdlib .format4 -> 'a
val kdprintf :
((formatter -> unit) -> 'a ) ->
('b , formatter , unit, 'a ) Stdlib .format4 ->
'b
val kasprintf :
(string -> 'a ) ->
('b , formatter , unit, 'a ) Stdlib .format4 ->
'b
val bprintf : Stdlib .Buffer.t -> ('a , formatter , unit) Stdlib .format -> 'a
val kprintf : (string -> 'a ) -> ('b , unit, string, 'a ) Stdlib .format4 -> 'b
val open_tag : tag -> unit
val close_tag : unit -> unit
type t = Stdlib .Format.formatter
type 'a printer = t -> 'a -> unit
CombinatorsForce newline (see Format.pp_force_newline
).
val substring : (string * int * int) printer
Print the substring (s,i,len)
, where i
is the offset in s
and len
the number of bytes in the substring.
val text : string printer
Print string, but replacing spaces with breaks and newlines with newline
. See pp_print_text
on recent versions of OCaml.
Alias to Format.pp_print_flush
.
Similar to CCString.print
.
opt pp
prints options as follows:
Some x
will become "some foo" if pp x ---> "foo"
.None
will become "none".In the tuple printers, the sep
argument is only available.
within a b p
wraps p
inside the strings a
and b
. Convenient, for instances, for brackets, parenthesis, quotes, etc.
Wrap the printer in a vertical box.
Wrap the printer in a horizontal/vertical box.
Wrap the printer in a horizontal or vertical box.
Wrap the printer in an horizontal box.
val return : ('a , _ , _ , 'a ) Stdlib .format4 -> unit printer
return "some_format_string"
takes a argument-less format string and returns a printer actionable by ()
. Examples:
return ",@ "
return "@{<Red>and then@}@,"
return "@[<v>a@ b@]"
val of_to_string : ('a -> string) -> 'a printer
of_to_string f
converts its input to a string using f
, then prints the string.
const pp x
is a unit printer that uses pp
on x
.
some pp
will print options as follows:
Some x
is printed using pp
on x
None
is not printed at alllazy_force pp out x
forces x
and prints the result with pp
.
lazy_or ?default pp out x
prints default
if x
is not evaluated yet, or uses pp
otherwise.
ANSI codesUse ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code to put some colors on the terminal.
This uses tags in format strings to specify the style. Current styles are the following:
"reset" resets style "black" "red" "green" "yellow" "blue" "magenta" "cyan" "white" "bold" bold font "Black" bold black "Red" bold red "Green" bold green "Yellow" bold yellow "Blue" bold blue "Magenta" bold magenta "Cyan" bold cyan "White" bold white Example:
set_color_default true;;
Format.printf
"what is your @{<White>favorite color@}? @{<blue>blue@}! No, @{<red>red@}! Ahhhhhhh@.";;
status: unstable
val set_color_tag_handling : t -> unit
Add functions to support color tags to the given formatter.
val set_color_default : bool -> unit
set_color_default b
enables color handling on the standard formatters (stdout, stderr) if b = true
as well as on sprintf
formatters; it disables the color handling if b = false
.
with_color "Blue" pp
behaves like the printer pp
, but with the given style.
status: unstable
val with_colorf : string -> t -> ('a , t , unit, unit) Stdlib .format4 -> 'a
with_colorf "Blue" out "%s %d" "yolo" 42
will behave like Format.fprintf
, but wrapping the content with the given style.
status: unstable
val with_color_sf : string -> ('a , t , unit, string) Stdlib .format4 -> 'a
with_color_sf "Blue" out "%s %d" "yolo" 42
will behave like sprintf
, but wrapping the content with the given style.
Example:
CCFormat.with_color_sf "red" "%a" CCFormat.Dump.(list int) [1;2;3] |> print_endline;;
status: unstable
val with_color_ksf :
f :(string -> 'b ) ->
string ->
('a , t , unit, 'b ) Stdlib .format4 ->
'a
with_color_ksf "Blue" ~f "%s %d" "yolo" 42
will behave like ksprintf
, but wrapping the content with the given style.
Example: the following with raise Failure
with a colored message
CCFormat.with_color_ksf "red" ~f:failwith "%a" CCFormat.Dump.(list int) [1;2;3];;
IOval output : t -> 'a printer -> 'a -> unit
val to_string : 'a printer -> 'a -> string
val of_chan : Stdlib .out_channel -> t
Alias to Format.formatter_of_out_channel
.
val with_out_chan : Stdlib .out_channel -> (t -> 'a ) -> 'a
with_out_chan oc f
turns oc
into a formatter fmt
, and call f fmt
. Behaves like f fmt
from then on, but whether the call to f
fails or returns, fmt
is flushed before the call terminates.
tee a b
makes a new formatter that writes in both a
and b
.
val sprintf : ('a , t , unit, string) Stdlib .format4 -> 'a
Print into a string any format string that would usually be compatible with fprintf
. Like Format.asprintf
.
val sprintf_no_color : ('a , t , unit, string) Stdlib .format4 -> 'a
Like sprintf
but never prints colors.
val sprintf_dyn_color :
colors :bool ->
('a , t , unit, string) Stdlib .format4 ->
'a
Like sprintf
but enable/disable colors depending on colors
.
Example:
(* with colors *)
CCFormat.sprintf_dyn_color ~colors:true "@{<Red>%a@}"
CCFormat.Dump.(list int) [1;2;3] |> print_endline;;
(* without colors *)
CCFormat.sprintf_dyn_color ~colors:false "@{<Red>%a@}"
CCFormat.Dump.(list int) [1;2;3] |> print_endline;;
val fprintf : t -> ('a , t , unit) Stdlib .format -> 'a
val fprintf_dyn_color : colors :bool -> t -> ('a , t , unit) Stdlib .format -> 'a
Like fprintf
but enable/disable colors depending on colors
.
val ksprintf :
?margin :int ->
f :(string -> 'b ) ->
('a , Stdlib .Format.formatter, unit, 'b ) Stdlib .format4 ->
'a
ksprintf fmt ~f
formats using fmt
, in a way similar to sprintf
, and then calls f
on the resulting string.
val to_file : string -> ('a , t , unit, unit) Stdlib .format4 -> 'a
DumpPrint structures as OCaml values, so that they can be parsed back by OCaml (typically, in the toplevel, for debugging).
Example:
Format.printf "%a@." CCFormat.Dump.(list int) CCList.(1 -- 200);;
Format.printf "%a@." CCFormat.Dump.(array (list (pair int bool)))
[| [1, true; 2, false]; []; [42, false] |];;
module Dump : sig ... end