2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
2017-11-26 06:49:31 -08:00
|
|
|
(* Xavier Leroy, projet Gallium, INRIA Paris *)
|
2016-02-18 07:11:59 -08:00
|
|
|
(* *)
|
2017-11-26 06:49:31 -08:00
|
|
|
(* Copyright 2017 Institut National de Recherche en Informatique et *)
|
2016-02-18 07:11:59 -08:00
|
|
|
(* en Automatique. *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. This file is distributed under the terms of *)
|
|
|
|
(* the GNU Lesser General Public License version 2.1, with the *)
|
|
|
|
(* special exception on linking described in the file LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
1995-08-09 08:06:35 -07:00
|
|
|
|
2017-11-26 06:49:31 -08:00
|
|
|
open Printf
|
|
|
|
|
|
|
|
external isatty : out_channel -> bool = "caml_sys_isatty"
|
|
|
|
external terminfo_rows: out_channel -> int = "caml_terminfo_rows"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-09-02 11:21:48 -07:00
|
|
|
type status =
|
|
|
|
| Uninitialised
|
|
|
|
| Bad_term
|
2017-11-26 06:49:31 -08:00
|
|
|
| Good_term
|
|
|
|
|
2017-08-12 13:24:41 -07:00
|
|
|
let setup oc =
|
2017-11-26 06:49:31 -08:00
|
|
|
let term = try Sys.getenv "TERM" with Not_found -> "" in
|
|
|
|
(* Same heuristics as in Misc.Color.should_enable_color *)
|
|
|
|
if term <> "" && term <> "dumb" && isatty oc
|
|
|
|
then Good_term
|
|
|
|
else Bad_term
|
|
|
|
|
|
|
|
let num_lines oc =
|
|
|
|
let rows = terminfo_rows oc in
|
|
|
|
if rows > 0 then rows else 24
|
|
|
|
(* 24 is a reasonable default for an ANSI-style terminal *)
|
|
|
|
|
|
|
|
let backup oc n =
|
|
|
|
if n >= 1 then fprintf oc "\027[%dA%!" n
|
|
|
|
|
|
|
|
let resume oc n =
|
|
|
|
if n >= 1 then fprintf oc "\027[%dB%!" n
|
|
|
|
|
|
|
|
let standout oc b =
|
|
|
|
output_string oc (if b then "\027[4m" else "\027[0m"); flush oc
|