Hack to force OCaml to use colors when possible

This commit is contained in:
Jeremie Dimino 2017-02-23 12:12:02 +00:00
parent aa5c4078f7
commit d696bd113b
4 changed files with 44 additions and 12 deletions

View File

@ -107,3 +107,27 @@ let strip str =
| _ -> skip (i + 1)
in
loop 0
let stderr_supports_colors = lazy(
not Sys.win32 &&
Unix.(isatty stderr) &&
match Sys.getenv "TERM" with
| exception Not_found -> false
| "dumb" -> false
| _ -> true
)
(* We redirect the output of all commands, so by default the compiler will disable
colors. Since we support colors in the output of commands, we force it via OCAMLPARAM
if stderr supports colors. *)
let setup_env_for_ocaml_colors = lazy(
if Lazy.force stderr_supports_colors then begin
let value =
match Sys.getenv "OCAMLPARAM" with
| exception Not_found -> "color=always,_"
| s -> "color=always," ^ s
in
Unix.putenv "OCAMLPARAM" value
end
)

View File

@ -1,2 +1,4 @@
val colorize : key:string -> string -> string
val strip : string -> string
val stderr_supports_colors : bool Lazy.t
val setup_env_for_ocaml_colors : unit Lazy.t

View File

@ -210,7 +210,9 @@ let create ~(kind : Kind.t) ~path ~env =
all_known := String_map.add !all_known ~key:name ~data:t;
return t
let initial_env = lazy (Unix.environment ())
let initial_env = lazy (
Lazy.force Ansi_color.setup_env_for_ocaml_colors;
Unix.environment ())
let default = lazy (
let env = Lazy.force initial_env in

View File

@ -214,17 +214,20 @@ module Scheduler = struct
| None -> s
| Some dir -> sprintf "(cd %s && %s)" dir s
let strip_colors_for_stderr =
let strip = lazy (
Sys.win32 ||
not (Unix.(isatty stderr)) ||
match Sys.getenv "TERM" with
| exception Not_found -> true
| "dumb" -> true
| _ -> false
) in
fun s ->
if Lazy.force strip then Ansi_color.strip s else s
let stderr_supports_colors = lazy(
not Sys.win32 &&
Unix.(isatty stderr) &&
match Sys.getenv "TERM" with
| exception Not_found -> false
| "dumb" -> false
| _ -> true
)
let strip_colors_for_stderr s =
if Lazy.force Ansi_color.stderr_supports_colors then
s
else
Ansi_color.strip s
type running_job =
{ id : int
@ -375,6 +378,7 @@ module Scheduler = struct
go_rec cwd log t
let go ?log t =
Lazy.force Ansi_color.setup_env_for_ocaml_colors;
let cwd = Sys.getcwd () in
go_rec cwd log t
end