Terminfo loads entries from the system terminfo database and exposes each capability as a typed token. The cap GADT encodes the return type of every capability so that get is type-safe at compile time.
Terminology.
A capability is a named terminal property: a boolean flag, an integer quantity, a fixed escape sequence, or a parameterized escape sequence.
An entry is the set of capabilities declared for a terminal type (e.g. xterm-256color).
A handle (t) is an immutable, in-memory representation of one entry.
Quick start
match Terminfo.load () with
| Error (`Parse_error msg) ->
prerr_endline ("terminfo parse error: " ^ msg)
| Error `Not_found -> prerr_endline "no terminfo entry"
| Ok ti -> (
Option.iter print_string (Terminfo.get ti Terminfo.Clear_screen);
(match Terminfo.get ti Terminfo.Cursor_position with
| Some goto -> print_string (goto (5, 10))
| None -> ());
match Terminfo.get ti Terminfo.Has_colors with
| Some true -> print_endline "colors"
| _ -> print_endline "monochrome")
Set_foreground: set foreground to color index n (setaf).
*)
The type for capability tokens.
The phantom type parameter encodes the OCaml type returned by get:
bool cap — boolean flags.
int cap — numeric quantities.
string cap — fixed escape sequences.
(… -> string) cap — parameterized escape sequences. The returned function evaluates the terminfo %-expression and allocates a fresh string on each call.
Each constructor maps to a standard terminfo capability whose short name appears in the constructor's documentation.
The type for terminfo entries. A value of this type is an immutable, in-memory handle to a parsed terminfo entry. It can be shared freely across threads.
load ?term () is the terminfo entry for terminal type term.
term defaults to the value of the TERM environment variable.
The search order is /usr/share/terminfo, /lib/terminfo, /etc/terminfo, and $HOME/.terminfo (when present). The entry is parsed eagerly; each call creates a fresh, independent handle.
Errors with `Not_found if no entry is found and `Parse_error msg if the file cannot be decoded.
Raises Sys_error if the entry exists but cannot be read.
get ti cap is the value of cap in ti, if declared.
For parameterized capabilities the returned function evaluates the terminfo %-expression and allocates a fresh string on each call. The function never mutates ti.
Note.Has_colors is synthesized from Max_colors and always yields Some true or Some false.