Debian Codegraph @

clib/terminal.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 type color = [
12 `DEFAULT
13 | `BLACK
14 | `RED
15 | `GREEN
16 | `YELLOW
17 | `BLUE
18 | `MAGENTA
19 | `CYAN
20 | `WHITE
21 | `LIGHT_BLACK
22 | `LIGHT_RED
23 | `LIGHT_GREEN
24 | `LIGHT_YELLOW
25 | `LIGHT_BLUE
26 | `LIGHT_MAGENTA
27 | `LIGHT_CYAN
28 | `LIGHT_WHITE
29 | `INDEX of int
30 | `RGB of (int * int * int)
31 ]
32
33 type style = {
34 fg_color : color option;
35 bg_color : color option;
36 bold : bool option;
37 italic : bool option;
38 underline : bool option;
39 negative : bool option;
40 prefix : string option;
41 suffix : string option;
42 }
43
44 let set o1 o2 = match o1 with
45 | None -> o2
46 | Some _ ->
47 match o2 with
48 | None -> o1
49 | Some _ -> o2
50
51 let default = {
52 fg_color = None;
53 bg_color = None;
54 bold = None;
55 italic = None;
56 underline = None;
57 negative = None;
58 prefix = None;
59 suffix = None;
60 }
61
62 let reset = "\027[0m"
63
64 let reset_style = {
65 fg_color = Some `DEFAULT;
66 bg_color = Some `DEFAULT;
67 bold = Some false;
68 italic = Some false;
69 underline = Some false;
70 negative = Some false;
71 prefix = None;
72 suffix = None;
73 }
74
75 let make ?fg_color ?bg_color ?bold ?italic ?underline ?negative ?style ?prefix ?suffix () =
76 let st = match style with
77 | None -> default
78 | Some st -> st
79 in
80 {
81 fg_color = set st.fg_color fg_color;
82 bg_color = set st.bg_color bg_color;
83 bold = set st.bold bold;
84 italic = set st.italic italic;
85 underline = set st.underline underline;
86 negative = set st.negative negative;
87 prefix = set st.prefix prefix;
88 suffix = set st.suffix suffix;
89 }
90
91 let merge s1 s2 =
92 {
93 fg_color = set s1.fg_color s2.fg_color;
94 bg_color = set s1.bg_color s2.bg_color;
95 bold = set s1.bold s2.bold;
96 italic = set s1.italic s2.italic;
97 underline = set s1.underline s2.underline;
98 negative = set s1.negative s2.negative;
99 prefix = set s1.prefix s2.prefix;
100 suffix = set s1.suffix s2.suffix;
101 }
102
103 let diff s1 s2 =
104 let diff_op o1 o2 reset_val = match o1 with
105 | None -> o2
106 | Some _ ->
107 match o2 with
108 | None -> reset_val
109 | Some _ -> if o1 = o2 then None else o2 in
110
111 {
112 fg_color = diff_op s1.fg_color s2.fg_color reset_style.fg_color;
113 bg_color = diff_op s1.bg_color s2.bg_color reset_style.bg_color;
114 bold = diff_op s1.bold s2.bold reset_style.bold;
115 italic = diff_op s1.italic s2.italic reset_style.italic;
116 underline = diff_op s1.underline s2.underline reset_style.underline;
117 negative = diff_op s1.negative s2.negative reset_style.negative;
118 prefix = diff_op s1.prefix s2.prefix reset_style.prefix;
119 suffix = diff_op s1.suffix s2.suffix reset_style.suffix;
120 }
121
122 let base_color = function
123 | `DEFAULT -> 9
124 | `BLACK -> 0
125 | `RED -> 1
126 | `GREEN -> 2
127 | `YELLOW -> 3
128 | `BLUE -> 4
129 | `MAGENTA -> 5
130 | `CYAN -> 6
131 | `WHITE -> 7
132 | `LIGHT_BLACK -> 0
133 | `LIGHT_RED -> 1
134 | `LIGHT_GREEN -> 2
135 | `LIGHT_YELLOW -> 3
136 | `LIGHT_BLUE -> 4
137 | `LIGHT_MAGENTA -> 5
138 | `LIGHT_CYAN -> 6
139 | `LIGHT_WHITE -> 7
140 | _ -> invalid_arg "base_color"
141
142 let extended_color off = function
143 | `INDEX i -> [off + 8; 5; i]
144 | `RGB (r, g, b) -> [off + 8; 2; r; g; b]
145 | _ -> invalid_arg "extended_color"
146
147 let is_light = function
148 | `LIGHT_BLACK
149 | `LIGHT_RED
150 | `LIGHT_GREEN
151 | `LIGHT_YELLOW
152 | `LIGHT_BLUE
153 | `LIGHT_MAGENTA
154 | `LIGHT_CYAN
155 | `LIGHT_WHITE -> true
156 | _ -> false
157
158 let is_extended = function
159 | `INDEX _ | `RGB _ -> true
160 | _ -> false
161
162 let repr st =
163 let fg = match st.fg_color with
164 | None -> []
165 | Some c ->
166 if is_light c then [90 + base_color c]
167 else if is_extended c then extended_color 30 c
168 else [30 + base_color c]
169 in
170 let bg = match st.bg_color with
171 | None -> []
172 | Some c ->
173 if is_light c then [100 + base_color c]
174 else if is_extended c then extended_color 40 c
175 else [40 + base_color c]
176 in
177 let bold = match st.bold with
178 | None -> []
179 | Some true -> [1]
180 | Some false -> [22]
181 in
182 let italic = match st.italic with
183 | None -> []
184 | Some true -> [3]
185 | Some false -> [23]
186 in
187 let underline = match st.underline with
188 | None -> []
189 | Some true -> [4]
190 | Some false -> [24]
191 in
192 let negative = match st.negative with
193 | None -> []
194 | Some true -> [7]
195 | Some false -> [27]
196 in
197 fg @ bg @ bold @ italic @ underline @ negative
198
199 let eval st =
200 let tags = repr st in
201 let tags = List.map string_of_int tags in
202 if List.length tags = 0 then "" else
203 Printf.sprintf "\027[%sm" (String.concat ";" tags)
204
205 let has_style t =
206 Unix.isatty t && Sys.os_type = "Unix"
207
208 let split c s =
209 let len = String.length s in
210 let rec split n =
211 try
212 let pos = String.index_from s n c in
213 let dir = String.sub s n (pos-n) in
214 dir :: split (succ pos)
215 with
216 | Not_found -> [String.sub s n (len-n)]
217 in
218 if len = 0 then [] else split 0
219
220 let check_char i = if i < 0 || i > 255 then invalid_arg "check_char"
221
222 let parse_color off rem = match off with
223 | 0 -> (`BLACK, rem)
224 | 1 -> (`RED, rem)
225 | 2 -> (`GREEN, rem)
226 | 3 -> (`YELLOW, rem)
227 | 4 -> (`BLUE, rem)
228 | 5 -> (`MAGENTA, rem)
229 | 6 -> (`CYAN, rem)
230 | 7 -> (`WHITE, rem)
231 | 9 -> (`DEFAULT, rem)
232 | 8 ->
233 begin match rem with
234 | 5 :: i :: rem ->
235 check_char i;
236 (`INDEX i, rem)
237 | 2 :: r :: g :: b :: rem ->
238 check_char r;
239 check_char g;
240 check_char b;
241 (`RGB (r, g, b), rem)
242 | _ -> invalid_arg "parse_color"
243 end
244 | _ -> invalid_arg "parse_color"
245
246 let set_light = function
247 | `BLACK -> `LIGHT_BLACK
248 | `RED -> `LIGHT_RED
249 | `GREEN -> `LIGHT_GREEN
250 | `YELLOW -> `LIGHT_YELLOW
251 | `BLUE -> `LIGHT_BLUE
252 | `MAGENTA -> `LIGHT_MAGENTA
253 | `CYAN -> `LIGHT_CYAN
254 | `WHITE -> `LIGHT_WHITE
255 | _ -> invalid_arg "parse_color"
256
257 let rec parse_style style = function
258 | [] -> style
259 | 0 :: rem ->
260 let style = merge style reset_style in
261 parse_style style rem
262 | 1 :: rem ->
263 let style = make ~style ~bold:true () in
264 parse_style style rem
265 | 3 :: rem ->
266 let style = make ~style ~italic:true () in
267 parse_style style rem
268 | 4 :: rem ->
269 let style = make ~style ~underline:true () in
270 parse_style style rem
271 | 7 :: rem ->
272 let style = make ~style ~negative:true () in
273 parse_style style rem
274 | 22 :: rem ->
275 let style = make ~style ~bold:false () in
276 parse_style style rem
277 | 23 :: rem ->
278 let style = make ~style ~italic:false () in
279 parse_style style rem
280 | 24 :: rem ->
281 let style = make ~style ~underline:false () in
282 parse_style style rem
283 | 27 :: rem ->
284 let style = make ~style ~negative:false () in
285 parse_style style rem
286 | code :: rem when (30 <= code && code < 40) ->
287 let color, rem = parse_color (code mod 10) rem in
288 let style = make ~style ~fg_color:color () in
289 parse_style style rem
290 | code :: rem when (40 <= code && code < 50) ->
291 let color, rem = parse_color (code mod 10) rem in
292 let style = make ~style ~bg_color:color () in
293 parse_style style rem
294 | code :: rem when (90 <= code && code < 100) ->
295 let color, rem = parse_color (code mod 10) rem in
296 let style = make ~style ~fg_color:(set_light color) () in
297 parse_style style rem
298 | code :: rem when (100 <= code && code < 110) ->
299 let color, rem = parse_color (code mod 10) rem in
300 let style = make ~style ~bg_color:(set_light color) () in
301 parse_style style rem
302 | _ :: rem -> parse_style style rem
303
304 (** Parse LS_COLORS-like strings *)
305 let parse s =
306 let defs = split ':' s in
307 let fold accu s = match split '=' s with
308 | [name; attrs] ->
309 let attrs = split ';' attrs in
310 let accu =
311 try
312 let attrs = List.map int_of_string attrs in
313 let attrs = parse_style (make ()) attrs in
314 (name, attrs) :: accu
315 with Failure _ | Invalid_argument _ -> accu
316 in
317 accu
318 | _ -> accu
319 in
320 List.fold_left fold [] defs