wip: use Stag in Format

This commit is contained in:
Simon Cruanes 2022-02-21 22:11:51 -05:00
parent 40189757ca
commit e397d90279
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 26 additions and 2 deletions

View file

@ -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_]

View file

@ -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