diff --git a/src/core/CCFormat.ml b/src/core/CCFormat.ml index 97590cf6..da23915e 100644 --- a/src/core/CCFormat.ml +++ b/src/core/CCFormat.ml @@ -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 diff --git a/src/core/CCFormat.mli b/src/core/CCFormat.mli index fdbe2be5..0687d90b 100644 --- a/src/core/CCFormat.mli +++ b/src/core/CCFormat.mli @@ -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