Initial snarf.
[shack.git] / libmojave / util / lm_terminfo.ml
blob68825ce338f604d28d931e367cc2eba52f58dbda
1 (*
2 * Simple terminfo interface.
3 * Copyright (C) 2002 Justin David Smith, Caltech
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation,
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Additional permission is given to link this library with the
20 * OpenSSL project's "OpenSSL" library, and with the OCaml runtime,
21 * and you may distribute the linked executables. See the file
22 * LICENSE.libmojave for more details.
26 (* The C function takes a string ID, and returns the escape sequence
27 (or an empty string if the ID is not defined for this terminal). *)
28 external caml_tgetstr_enabled : unit -> bool = "caml_tgetstr_enabled"
29 external caml_tgetstr : string -> string = "caml_tgetstr"
32 (* Tgetstr is enabled only if the terminal is defined *)
33 let tgetstr_enabled = caml_tgetstr_enabled ()
35 (* tgetstr id
36 Lookup the terminal capability with indicated id. This assumes the
37 terminfo to lookup is given in the TERM environment variable. This
38 function returns None if the terminal capability is not defined. *)
39 let tgetstr id =
40 if tgetstr_enabled then
41 let result = caml_tgetstr id in
42 if result = "" then
43 None
44 else
45 Some result
46 else
47 None
50 (* Various terminfo identifier names for use with tgetstr *)
51 let enter_bold_mode = "bold"
52 let exit_attribute_mode = "sgr0"
55 (* xterm_ok ()
56 Check for an XTerm-compatible terminal, for the XTerm escapes. *)
58 (* XXX: strictly speaking, we should be using the "tsl"/"fsl" capabilities here, but those are often missing *)
59 let xterm_ok () =
60 try
61 match Sys.getenv "TERM" with
62 "xterm" | "color_xterm" | "xterm-color" | "konsole" | "rxvt" ->
63 true
64 | _ ->
65 false
66 with
67 Not_found ->
68 false
71 (* xterm_escape_begin ()
72 Display XTerm title begin escape, if available. *)
73 let xterm_escape_begin () =
74 if xterm_ok () then
75 Some "\027]0;"
76 else
77 None
80 (* xterm_escape_begin ()
81 Display XTerm title end escape, if available. *)
82 let xterm_escape_end () =
83 if xterm_ok () then
84 Some "\007"
85 else
86 None