package dune

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ansi_color.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module Color = struct
  type t =
    | Default
    | Black
    | Red
    | Green
    | Yellow
    | Blue
    | Magenta
    | Cyan
    | White
    | Bright_black
    | Bright_red
    | Bright_green
    | Bright_yellow
    | Bright_blue
    | Bright_magenta
    | Bright_cyan
    | Bright_white

  let fg_code = function
    | Black          -> "30"
    | Red            -> "31"
    | Green          -> "32"
    | Yellow         -> "33"
    | Blue           -> "34"
    | Magenta        -> "35"
    | Cyan           -> "36"
    | White          -> "37"
    | Default        -> "39"
    | Bright_black   -> "90"
    | Bright_red     -> "91"
    | Bright_green   -> "92"
    | Bright_yellow  -> "93"
    | Bright_blue    -> "94"
    | Bright_magenta -> "95"
    | Bright_cyan    -> "96"
    | Bright_white   -> "97"

  let bg_code = function
    | Black          -> "40"
    | Red            -> "41"
    | Green          -> "42"
    | Yellow         -> "43"
    | Blue           -> "44"
    | Magenta        -> "45"
    | Cyan           -> "46"
    | White          -> "47"
    | Default        -> "49"
    | Bright_black   -> "100"
    | Bright_red     -> "101"
    | Bright_green   -> "102"
    | Bright_yellow  -> "103"
    | Bright_blue    -> "104"
    | Bright_magenta -> "105"
    | Bright_cyan    -> "106"
    | Bright_white   -> "107"
end

module Style = struct
  type t =
    | Fg of Color.t
    | Bg of Color.t
    | Bold
    | Dim
    | Underlined

  let code = function
    | Bold       -> "1"
    | Dim        -> "2"
    | Underlined -> "4"
    | Fg c       -> Color.fg_code c
    | Bg c       -> Color.bg_code c

  let escape_sequence l =
    let codes = "0" :: List.map l ~f:code in
    Printf.sprintf "\027[%sm" (String.concat codes ~sep:";")
end

module Styles = struct
  type t =
    { fg         : Color.t
    ; bg         : Color.t
    ; bold       : bool
    ; dim        : bool
    ; underlined : bool
    }

  let default =
    { fg         = Default
    ; bg         = Default
    ; bold       = true
    ; dim        = true
    ; underlined = true
    }

  let apply t (style : Style.t) =
    match style with
    | Fg c       -> { t with fg         = c    }
    | Bg c       -> { t with bg         = c    }
    | Bold       -> { t with bold       = true }
    | Dim        -> { t with dim        = true }
    | Underlined -> { t with underlined = true }

  let escape_sequence t =
    let open Style in
    let l = [] in
    let l =
      match t.fg with
      | Default -> l
      | c       -> Fg c :: l
    in
    let l =
      match t.bg with
      | Default -> l
      | c       -> Bg c :: l
    in
    let l =
      if t.bold then
        Bold :: l
      else
        l
    in
    let l =
      if t.bold then
        Dim :: l
      else
        l
    in
    let l =
      if t.underlined then
        Underlined :: l
      else
        l
    in
    Style.escape_sequence l
end

module Render = Pp.Renderer.Make(struct
    type t = Style.t list

    module Handler = struct
      type t = Styles.t * string

      let init = (Styles.default, "")

      let handle (t, seq) styles =
        let t' = List.fold_left styles ~init:t ~f:Styles.apply in
        if t <> t' then
          let seq' = Styles.escape_sequence t' in
          (seq',
           (t', seq'),
           seq)
        else
          ("", (t, seq), "")
    end
  end)

let strip str =
  let len = String.length str in
  let buf = Buffer.create len in
  let rec loop i =
    if i = len then
      Buffer.contents buf
    else
      match str.[i] with
      | '\027' -> skip (i + 1)
      | c      -> Buffer.add_char buf c; loop (i + 1)
  and skip i =
    if i = len then
      Buffer.contents buf
    else
      match str.[i] with
      | 'm' -> loop (i + 1)
      | _   -> skip (i + 1)
  in
  loop 0
OCaml

Innovation. Community. Security.