package ansifmt

  1. Overview
  2. Docs

Source file Fmt.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
include Rich_string.Make (Ansi)

let stylize = enrich

let rec prune_styled rs =
  match rs with
  | Empty | String _ -> rs
  | Enriched (_, rs) -> prune_styled rs
  | Join (sep, rss) ->
    Join (prune_styled sep, List.map prune_styled rss)
;;

let render ~with_styles rs =
  let rs' = if with_styles then rs else prune_styled rs in
  render rs'
;;

let print
      ?(out = stdout)
      ?(ending = Some (String "\n"))
      ?with_styles:(color_strategy = `Auto)
      rs
  =
  let with_styles =
    match color_strategy with
    | `Always -> true
    | `Never -> false
    | `Auto -> Out_channel.isatty out
  in
  let rs' = if with_styles then rs else prune_styled rs in
  print ~out ~ending rs'
;;