mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 03:35:30 -05:00
many improvements to CCFormat
- add `some` - add `return` - add `const` - add `of_to_string`
This commit is contained in:
parent
d76d3b95e3
commit
e5adafced6
2 changed files with 50 additions and 0 deletions
|
|
@ -12,6 +12,19 @@ type 'a printer = t -> 'a -> unit
|
||||||
|
|
||||||
let silent _fmt _ = ()
|
let silent _fmt _ = ()
|
||||||
|
|
||||||
|
let return fmt_str out () = Format.fprintf out "%(%)" fmt_str
|
||||||
|
|
||||||
|
(*$inject
|
||||||
|
let to_string_test s = CCFormat.sprintf_no_color "@[<h>%a@]%!" s ()
|
||||||
|
*)
|
||||||
|
|
||||||
|
(*$= & ~printer:(fun s->CCFormat.sprintf "%S" s)
|
||||||
|
"a b" (to_string_test (return "a@ b"))
|
||||||
|
", " (to_string_test (return ",@ "))
|
||||||
|
"and then" (to_string_test (return "@{<Red>and then@}@,"))
|
||||||
|
"a b" (to_string_test (return "@[<h>a@ b@]"))
|
||||||
|
*)
|
||||||
|
|
||||||
let unit fmt () = Format.pp_print_string fmt "()"
|
let unit fmt () = Format.pp_print_string fmt "()"
|
||||||
let int fmt i = Format.pp_print_string fmt (string_of_int i)
|
let int fmt i = Format.pp_print_string fmt (string_of_int i)
|
||||||
let string = Format.pp_print_string
|
let string = Format.pp_print_string
|
||||||
|
|
@ -114,6 +127,14 @@ let hbox pp out x =
|
||||||
pp out x;
|
pp out x;
|
||||||
Format.pp_close_box out ()
|
Format.pp_close_box out ()
|
||||||
|
|
||||||
|
let of_to_string f out x = Format.pp_print_string out (f x)
|
||||||
|
|
||||||
|
let const pp x out () = pp out x
|
||||||
|
|
||||||
|
let some pp out = function
|
||||||
|
| None -> ()
|
||||||
|
| Some x -> pp out x
|
||||||
|
|
||||||
(** {2 IO} *)
|
(** {2 IO} *)
|
||||||
|
|
||||||
let output fmt pp x = pp fmt x
|
let output fmt pp x = pp fmt x
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@ type 'a printer = t -> 'a -> unit
|
||||||
val silent : 'a printer (** Prints nothing *)
|
val silent : 'a printer (** Prints nothing *)
|
||||||
|
|
||||||
val unit : unit printer
|
val unit : unit printer
|
||||||
|
(** Prints "()" *)
|
||||||
|
|
||||||
val int : int printer
|
val int : int printer
|
||||||
val string : string printer
|
val string : string printer
|
||||||
val bool : bool printer
|
val bool : bool printer
|
||||||
|
|
@ -75,6 +77,33 @@ val hbox : 'a printer -> 'a printer
|
||||||
(** Wrap the printer in an horizontal box
|
(** Wrap the printer in an horizontal box
|
||||||
@since 0.16 *)
|
@since 0.16 *)
|
||||||
|
|
||||||
|
val return : ('a, _, _, 'a) 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@]"]
|
||||||
|
|
||||||
|
@since NEXT_RELEASE
|
||||||
|
*)
|
||||||
|
|
||||||
|
val of_to_string : ('a -> string) -> 'a printer
|
||||||
|
(** [of_to_string f] converts its input to a string using [f],
|
||||||
|
then prints the string
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val const : 'a printer -> 'a -> unit printer
|
||||||
|
(** [const pp x] is a unit printer that uses [pp] on [x]
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val some : 'a printer -> 'a option printer
|
||||||
|
(** [some pp] will print options as follows:
|
||||||
|
- [Some x] is printed using [pp] on [x]
|
||||||
|
- [None] is not printed at all
|
||||||
|
@since NEXT_RELEASE
|
||||||
|
*)
|
||||||
|
|
||||||
(** {2 ANSI codes}
|
(** {2 ANSI codes}
|
||||||
|
|
||||||
Use ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code
|
Use ANSI escape codes https://en.wikipedia.org/wiki/ANSI_escape_code
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue