1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Gallium, INRIA Paris *)
6 (* *)
7 (* Copyright 2017 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 open Printf
17
18 external isatty : out_channel -> bool = "caml_sys_isatty"
19 external terminfo_rows: out_channel -> int = "caml_terminfo_rows"
20
21 type status =
22 | Uninitialised
23 | Bad_term
24 | Good_term
25
26 let setup oc =
27 let term = try Sys.getenv "TERM" with Not_found -> "" in
28 (* Same heuristics as in Misc.Color.should_enable_color *)
29 if term <> "" && term <> "dumb" && isatty oc
30 then Good_term
31 else Bad_term
32
33 let num_lines oc =
34 let rows = terminfo_rows oc in
35 if rows > 0 then rows else 24
36 (* 24 is a reasonable default for an ANSI-style terminal *)
37
38 let backup oc n =
39 if n >= 1 then fprintf oc "\027[%dA%!" n
40
41 let resume oc n =
42 if n >= 1 then fprintf oc "\027[%dB%!" n
43
44 let standout oc b =
45 output_string oc (if b then "\027[4m" else "\027[0m"); flush oc
46