package climate

  1. Overview
  2. Docs

Source file ansi_style.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
open! Import

module Color = struct
  type t =
    [ `Black
    | `Red
    | `Green
    | `Yellow
    | `Blue
    | `Magenta
    | `Cyan
    | `White
    | `Bright_black
    | `Bright_red
    | `Bright_green
    | `Bright_yellow
    | `Bright_blue
    | `Bright_magenta
    | `Bright_cyan
    | `Bright_white
    ]
end

type t =
  { bold : bool
  ; dim : bool
  ; underline : bool
  ; color : Color.t option
  }

(* This is what the style will be after the terminal is reset. *)
let default = { bold = false; dim = false; underline = false; color = None }
let reset = "\x1b[0m"

let is_default { bold; dim; underline; color } =
  (not bold) && (not dim) && (not underline) && Option.is_none color
;;

let escape { bold; dim; underline; color } =
  let effects =
    List.concat
      [ (if bold then [ ";1" ] else [])
      ; (if dim then [ ";2" ] else [])
      ; (if underline then [ ";4" ] else [])
      ]
  in
  let color_code =
    match (color : Color.t option) with
    | None -> 0
    | Some `Black -> 30
    | Some `Red -> 31
    | Some `Green -> 32
    | Some `Yellow -> 33
    | Some `Blue -> 34
    | Some `Magenta -> 35
    | Some `Cyan -> 36
    | Some `White -> 37
    | Some `Bright_black -> 90
    | Some `Bright_red -> 91
    | Some `Bright_green -> 92
    | Some `Bright_yellow -> 93
    | Some `Bright_blue -> 94
    | Some `Bright_magenta -> 95
    | Some `Bright_cyan -> 96
    | Some `Bright_white -> 97
  in
  Printf.sprintf "\x1b[%d%sm" color_code (String.concat ~sep:"" effects)
;;

let pp_with_style t ppf ~f =
  if not (is_default t) then Format.pp_print_string ppf (escape t);
  f ppf;
  if not (is_default t) then Format.pp_print_string ppf reset
;;
OCaml

Innovation. Community. Security.