From e397d902797719a571862adaac7377f4d40edba7 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 21 Feb 2022 22:11:51 -0500 Subject: [PATCH] wip: use Stag in Format --- src/core/CCFormat.ml | 17 +++++++++++++++-- src/core/CCFormat.mli | 11 +++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/core/CCFormat.ml b/src/core/CCFormat.ml index 18783909..a7a6cb5e 100644 --- a/src/core/CCFormat.ml +++ b/src/core/CCFormat.ml @@ -336,15 +336,23 @@ module ANSI_codes = struct | _ -> raise No_such_style end +type stag += + | Style of ANSI_codes.style list + let color_enabled = ref false +let mark_open_style st style = + Stack.push style st; + if !color_enabled then string_of_style_list style else "" + +let mark_close_style st style = + (* either prints the tag of [s] or delegate to [or_else] *) let mark_open_tag st ~or_else s = let open ANSI_codes in try let style = style_of_tag_ s in - Stack.push style st; - if !color_enabled then string_of_style_list style else "" + mark_open_style st style with No_such_style -> or_else s let mark_close_tag st ~or_else s = @@ -378,6 +386,11 @@ let update_tag_funs_ funs f1 f2 = mark_close_tag = f2 ~or_else:funs.mark_close_tag; } +let styling stl pp out x = + pp_open_stag out (Style stl); + try pp out x; pp_close_stag out () + with e -> pp_close_stag out (); raise e + [@@@ocaml.warning "+3"] [@@@else_] diff --git a/src/core/CCFormat.mli b/src/core/CCFormat.mli index dcb74c6a..fa616162 100644 --- a/src/core/CCFormat.mli +++ b/src/core/CCFormat.mli @@ -324,6 +324,17 @@ module ANSI_codes : sig is a very shiny style. *) end +[@@@ifge 4.8] + +val styling : ANSI_codes.style list -> 'a printer -> 'a printer +(** [styling st p] is the same printer as [p], except it locally sets + the style [st]. + + Available only on OCaml >= 4.08. + @since NEXT_RELEASE *) + +[@@@endif] + (** {2 IO} *) val output : t -> 'a printer -> 'a -> unit