Source file Console.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
external mlfront_cli_init : unit -> int = "mlfront_cli_init"

type terminal = ConsoleVT | ConsoleNoVT | NoTTY
type encoding = UTF_8 | ASCII
type t = { terminal : terminal; encoding : encoding }

let create_no_terminal () = { terminal = NoTTY; encoding = ASCII }

let create_virtual_terminal () =
  match (mlfront_cli_init (), Sys.getenv_opt "TERM") with
  | 0, Some "dumb" -> { terminal = ConsoleNoVT; encoding = ASCII }
  | 11, _ -> { terminal = ConsoleVT; encoding = ASCII }
  | 12, _ -> { terminal = ConsoleVT; encoding = UTF_8 }
  | 1, _ -> { terminal = ConsoleNoVT; encoding = ASCII }
  | 2, _ -> { terminal = ConsoleNoVT; encoding = UTF_8 }
  | -2, _ -> { terminal = NoTTY; encoding = ASCII }
  | _ -> failwith "Console initialization failed"

let supports_virtual_terminal { terminal; encoding = _ } = terminal = ConsoleVT

let supports_ansi_color { terminal; encoding = _ } =
  match (terminal, Sys.getenv_opt "NO_COLOR") with
  | (NoTTY | ConsoleNoVT), _ -> false
  | ConsoleVT, Some nocolor when nocolor <> "" ->
      (* https://no-color.org/ *)
      false
  | ConsoleVT, _ -> true

let supports_utf8 { terminal = _; encoding } = encoding = UTF_8