add CCFormat.with_color_ksf for colored printing

This commit is contained in:
Simon Cruanes 2017-04-03 15:32:26 +02:00
parent e0287b9efe
commit ee69bdcab8
2 changed files with 14 additions and 2 deletions

View file

@ -324,7 +324,7 @@ let sprintf_ c format =
fmt
format
let with_color_sf s fmt =
let with_color_ksf ~f s fmt =
let buf = Buffer.create 64 in
let out = Format.formatter_of_buffer buf in
if !color_enabled then set_color_tag_handling out;
@ -333,9 +333,11 @@ let with_color_sf s fmt =
(fun out ->
Format.pp_close_tag out ();
Format.pp_print_flush out ();
Buffer.contents buf)
f (Buffer.contents buf))
out fmt
let with_color_sf s fmt = with_color_ksf ~f:(fun s->s) s fmt
let sprintf fmt = sprintf_ true fmt
let sprintf_no_color fmt = sprintf_ false fmt
let sprintf_dyn_color ~colors fmt = sprintf_ colors fmt

View file

@ -179,6 +179,16 @@ val with_color_sf : string -> ('a, t, unit, string) format4 -> 'a
{b status: experimental}
@since 0.21 *)
val with_color_ksf : f:(string -> 'b) -> string -> ('a, t, unit, 'b) 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];;
]}
@since NEXT_RELEASE *)
(** {2 IO} *)
val output : t -> 'a printer -> 'a -> unit