package inquire

  1. Overview
  2. Docs

Module AnsiSource

This module offers basic control of ANSI compliant terminals and the windows shell.

The functions below do not send ANSI codes (i.e., do nothing or only print the output) when then output is not connected to a TTY. Functions providing information (such as pos_cursor) fail when in that situation. TTY detection is configurable by changing the value of isatty.

This library is not thread safe.

  • author Christophe Troestler (Christophe.Troestler@umons.ac.be)
  • author Vincent Hugot (vincent.hugot@gmail.com)

Colors and style

Sourcetype color =
  1. | Black
  2. | Red
  3. | Green
  4. | Yellow
  5. | Blue
  6. | Magenta
  7. | Cyan
  8. | White
  9. | Bright_black
  10. | Bright_red
  11. | Bright_green
  12. | Bright_yellow
  13. | Bright_blue
  14. | Bright_magenta
  15. | Bright_cyan
  16. | Bright_white
  17. | Default
    (*

    Default color of the terminal

    *)

Available colors.

Sourcetype style =
  1. | Reset
  2. | Bold
  3. | Underlined
  4. | Inverse
  5. | Hidden
  6. | Foreground of color
  7. | Background of color

Various styles for the text. Blink and Hidden may not work on every terminal.

Sourceval black : style

Shortcut for Foreground Black

Sourceval red : style

Shortcut for Foreground Red

Sourceval green : style

Shortcut for Foreground Green

Sourceval yellow : style

Shortcut for Foreground Yellow

Sourceval blue : style

Shortcut for Foreground Blue

Sourceval magenta : style

Shortcut for Foreground Magenta

Sourceval cyan : style

Shortcut for Foreground Cyan

Sourceval white : style

Shortcut for Foreground White

Sourceval default : style

Shortcut for Foreground Default

Sourceval bg_black : style

Shortcut for Background Black

Sourceval bg_red : style

Shortcut for Background Red

Sourceval bg_green : style

Shortcut for Background Green

Sourceval bg_yellow : style

Shortcut for Background Yellow

Sourceval bg_blue : style

Shortcut for Background Blue

Sourceval bg_magenta : style

Shortcut for Background Magenta

Sourceval bg_cyan : style

Shortcut for Background Cyan

Sourceval bg_white : style

Shortcut for Background White

Sourceval bg_default : style

Shortcut for Background Default

Sourceval bold : style

Shortcut for Bold

Sourceval underlined : style

Shortcut for Underlined

Shortcut for Blink

Sourceval inverse : style

Shortcut for Inverse

Sourceval hidden : style

Shortcut for Hidden

Sourceval set_autoreset : bool -> unit

Turns the autoreset feature on and off. It defaults to on.

Sourceval print_string : style list -> string -> unit

print_string attr txt prints the string txt with the attibutes attr. After printing, the attributes are automatically reseted to the defaults, unless autoreset is turned off.

Sourceval prerr_string : style list -> string -> unit

Like print_string but prints on the standard error.

Sourceval printf : style list -> ('a, unit, string, unit) format4 -> 'a

printf attr format arg1 ... argN prints the arguments arg1,...,argN according to format with the attibutes attr. After printing, the attributes are automatically reseted to the defaults, unless autoreset is turned off.

Sourceval eprintf : style list -> ('a, unit, string, unit) format4 -> 'a

Same as printf but prints the result on stderr.

Sourceval sprintf : style list -> ('a, unit, string) format -> 'a

Same as printf but returns the result in a string. This only works on ANSI compliant terminals — for which escape sequences are used — and not under Windows — where system calls are required. On Windows, it is identical to the standard sprintf.

Erasing

Sourcetype loc =
  1. | Eol
  2. | Above
  3. | Below
  4. | Screen
Sourceval erase : loc -> unit

erase Eol clear from the cursor position to the end of the line without moving the cursor. erase Above erases everything before the position of the cursor. erase Below erases everything after the position of the cursor. erase Screen erases the whole screen.

This function does not modify the position of the cursor.

Cursor

Sourceval set_cursor : int -> int -> unit

set_cursor x y puts the cursor at position (x,y), x indicating the column (the leftmost one being 1) and y being the line (the topmost one being 1). If x <= 0, the x coordinate is unchanged; if y <= 0, the y coordinate is unchanged.

Sourceval move_cursor : int -> int -> unit

move_cursor x y moves the cursor by x columns (to the right if x > 0, to the left if x < 0) and by y lines (downwards if y > 0 and upwards if y < 0).

Sourceval move_bol : unit -> unit

move_bol () moves the cursor to the beginning of the current line. This is useful for progress bars for example.

Sourceval pos_cursor : unit -> int * int

pos_cursor () returns a couple (x,y) giving the current position of the cursor, x >= 1 being the column and y >= 1 the row.

Sourceval save_cursor : unit -> unit

save_cursor () saves the current position of the cursor.

Sourceval show_cursor : unit -> unit

show_cursor () show the cursor.

Not implemented on Windows.

Sourceval hide_cursor : unit -> unit

show_cursor () hidex the cursor.

Not implemented on Windows.

Sourceval restore_cursor : unit -> unit

restore_cursor () replaces the cursor to the position saved with save_cursor ().

Size

Sourceval resize : int -> int -> unit

resize width height resize the current terminal to the given width and height.

Sourceval size : unit -> int * int

size () returns a pair (width, height) giving the size of the terminal in character cells.

Scrolling

Sourceval scroll : int -> unit

scroll n scrolls the terminal by n lines, up (creating new lines at the bottom) if n > 0 and down if n < 0.

TTY

Sourceval isatty : (Unix.file_descr -> bool) ref

Function used to detect whether the current output is connected to a TTY. Defaults to Unix.isatty.

OCaml

Innovation. Community. Security.