package ansifmt

  1. Overview
  2. Docs
A simple, lightweight library for ANSI styling

Install

dune-project
 Dependency

Authors

Maintainers

Sources

ansifmt-2.0.0.tbz
sha256=7bd5aff4eb547ca9bbc50cda6fa71af016673fbed0b0913b6c7bf5fce4688459
sha512=1042f4d0ae6d02ab90c1034d839f7b6d30980983b6192cb6c4c18dce030312693a82edbc37e5fa70acf4d1713f0875c3e7e2b5ff12fcac5c64737465c5e72def

doc/src/ansifmt/Fmt.ml.html

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'
;;