Documentation
containers lib
CCFormat
Module
type 'a sequence = ('a -> unit) -> unit
type 'a printer = t -> 'a -> unit
Combinatorsval list :
?start :string ->
?stop :string ->
?sep :string ->
'a printer ->
'a list printer
val array :
?start :string ->
?stop :string ->
?sep :string ->
'a printer ->
'a array printer
val arrayi :
?start :string ->
?stop :string ->
?sep :string ->
(int * 'a ) printer ->
'a array printer
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
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: experimental
val set_color_tag_handling : t -> unit
adds 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: experimental
val with_colorf : string -> t -> ('a , t , unit, unit) format4 -> 'a
with_colorf "Blue" out "%s %d" "yolo" 42
will behave like Format.fprintf
, but wrapping the content with the given style status: experimental
val with_color_sf : string -> ('a , t , unit, string) 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: experimental
IOval output : t -> 'a printer -> 'a -> unit
val to_string : 'a printer -> 'a -> string
val sprintf : ('a , t , unit, string) format4 -> 'a
Print into a string any format string that would usually be compatible with fprintf
. Similar to Format.asprintf
.
val sprintf_no_color : ('a , t , unit, string) format4 -> 'a
Similar to sprintf
but never prints colors
val sprintf_dyn_color : colors :bool -> ('a , t , unit, string) format4 -> 'a
Similar to 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) format -> 'a
val fprintf_dyn_color : colors :bool -> t -> ('a , t , unit) format -> 'a
Similar to fprintf
but enable/disable colors depending on colors
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) 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