package easy_logging

  1. Overview
  2. Docs

Source file colorize.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
(** Terminal styling function *)


(** Terminal colors *)
type color =
  | Default | Black | Red | Green | Yellow
  | Blue | Magenta | Cyan | Gray | White 
  | LRed | LGreen | LYellow | LBlue
  | LMagenta | LCyan | LGray


(** Terminal styles*)
type format =
  | Bold | Underline | Invert
  | Fg of color | Bg of color
                     

let to_fg_code c =
  match c with 
  | Default -> 39 | Black -> 30  | Red -> 31
  | Green -> 32   | Yellow -> 33 | Blue -> 34
  | Magenta -> 35 | Cyan -> 36   | Gray -> 90
  | White -> 97   | LRed -> 91   | LGreen -> 92
  | LYellow -> 93 | LBlue -> 94  | LMagenta -> 95
  | LCyan -> 96   | LGray -> 37
                                   
let to_bg_code c = 
  match c with 
  | Default -> 49 | Black -> 40  | Red -> 41
  | Green -> 42   | Yellow -> 43 | Blue -> 44
  | Magenta -> 45 | Cyan -> 46   | Gray -> 100
  | White -> 107  | LRed -> 101  | LGreen -> 102
  | LYellow -> 103| LBlue -> 104  | LMagenta -> 105
  | LCyan -> 106   | LGray -> 47
                      
let style_to_codes s =
  match s with
  | Bold -> 1, 21
  | Underline -> 4, 24
  | Invert -> 7, 27
  | Fg c -> to_fg_code c, to_fg_code Default
  | Bg c -> to_bg_code c, to_bg_code Default


(** [format styles str] formats [str] to the given [styles] *)
let rec format styles str =
  match styles with
  | _ as s :: styles' ->
     let set, reset = style_to_codes s in
     Printf.sprintf "\027[%dm%s\027[%dm" set (format styles' str) reset
   | [] -> str
             
OCaml

Innovation. Community. Security.