Debian Codegraph @

toplevel/colors.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 = [`ON | `AUTO | `EMACS | `OFF]
12
13 let init_color opts =
14 let has_color = match opts with
15 | `OFF -> false
16 | `EMACS -> false
17 | `ON -> true
18 | `AUTO ->
19 Terminal.has_style Unix.stdout &&
20 Terminal.has_style Unix.stderr &&
21 (* emacs compilation buffer does not support colors by default,
22 its TERM variable is set to "dumb". *)
23 try Sys.getenv "TERM" <> "dumb" with Not_found -> false
24 in
25 let term_color =
26 if has_color then begin
27 match Envars.getenv_rocq "_COLORS" with
28 | None -> Topfmt.default_styles (); true (* Default colors *)
29 | Some "" -> false (* No color output *)
30 | Some s -> Topfmt.parse_color_config s; true (* Overwrite all colors *)
31 end
32 else begin
33 Topfmt.default_styles (); false (* textual markers, no color *)
34 end
35 in
36 if opts = `EMACS then
37 Topfmt.set_emacs_print_strings ()
38 else if not term_color then begin
39 Proof_diffs.write_color_enabled term_color;
40 if Proof_diffs.show_diffs () then
41 (prerr_endline "Error: -diffs requires enabling -color"; exit 1)
42 end;
43 Topfmt.init_terminal_output ~color:term_color
44
45 let print_style_tags opts =
46 let () = init_color opts in
47 let tags = Topfmt.dump_tags () in
48 let iter (t, st) =
49 let opt = Terminal.eval st ^ t ^ Terminal.reset ^ "\n" in
50 print_string opt
51 in
52 let make (t, st) =
53 let tags = List.map string_of_int (Terminal.repr st) in
54 (t ^ "=" ^ String.concat ";" tags)
55 in
56 let repr = List.map make tags in
57 let () = Printf.printf "ROCQ_COLORS=\"%s\"\n" (String.concat ":" repr) in
58 let () = List.iter iter tags in
59 flush_all ()
60
61
62 let set_color = function
63 | "yes" | "on" -> `ON
64 | "no" | "off" -> `OFF
65 | "auto" ->`AUTO
66 | _ ->
67 Coqargs.error_wrong_arg ("Error: on/off/auto expected after option color")
68
69
70 let parse_extra_colors ~emacs extras =
71 let rec parse_extra color_mode = function
72 | "-color" :: next :: rest -> parse_extra (set_color next) rest
73 | x :: rest ->
74 let color_mode, rest = parse_extra color_mode rest in color_mode, x :: rest
75 | [] -> color_mode, [] in
76 let c, extras = parse_extra `AUTO extras in
77 (* we parse -color but ignore it when -emacs
78 maybe should be the other way (ignore -emacs for color printing if -color is given)? *)
79 let c = if emacs then `EMACS else c in
80 c, extras