package matrix
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9
doc/matrix.ansi/Ansi/index.html
Module AnsiSource
ANSI terminal escape sequences.
Escape sequences are represented as functions (t) that write directly to a writer buffer. Combining sequences creates a new function, not a new string.
For quick one-off output, convert to a string with to_string:
Ansi.(to_string (cursor_position ~row:10 ~col:5))For render loops, use emit with a pre-allocated buffer:
let buf = Bytes.create 65536 in
let w = Ansi.Writer.make buf in
Ansi.emit (Ansi.cursor_position ~row:1 ~col:1) w;
Ansi.emit (Ansi.literal "Hello") wFor hot loops where even closure allocation matters, use sgr_direct or Sgr_state which write directly to the buffer.
Types
The type for escape sequences. A function that writes bytes to a writer. Sequences compose by function application.
Sequence combinators
bytes b ~off ~len copies a byte slice to the writer.
Raises Invalid_argument if the slice is out of bounds.
utf8 cp encodes Unicode scalar value cp as UTF-8. Invalid codepoints (negative, > 0x10FFFF, or surrogates 0xD800–0xDFFF) are replaced with U+FFFD.
Output
to_string seq converts seq to a string. Allocates a temporary buffer.
to_buffer seq buf appends seq to buf. Allocates an intermediate string via to_string.
CSI and SGR primitives
sgr codes emits ESC [ codes m. sgr [] emits nothing; use reset or sgr [0] to clear attributes.
sgr_direct f w emits SGR codes generated by callback f. The callback receives a push function; every call to push code appends that integer to the SGR parameter list. Avoids list allocation.
Cursor control
Relative movement
cursor_up ~n moves the cursor up n lines. Negative values are clamped to 0.
cursor_previous_line ~n moves up n lines to column 1 (CPL).
Absolute positioning
cursor_horizontal_absolute col moves to 1-based column col. Values < 1 are clamped to 1.
cursor_vertical_absolute row moves to 1-based row row. Values < 1 are clamped to 1.
cursor_position ~row ~col moves to 1-based (row, col).
State
cursor_save saves cursor position (CSI s). Must be paired with cursor_restore.
cursor_restore restores cursor position (CSI u). Behaviour is undefined without a prior cursor_save.
Appearance
The type for cursor shapes (DECSCUSR).
cursor_style ~shape sets the cursor shape.
cursor_color ~r ~g ~b sets the cursor color (OSC 12). Pair with reset_cursor_color or reset_cursor_color_fallback during teardown.
reset_cursor_color_fallback resets the cursor color using OSC 12 "default". Fallback for terminals without OSC 112.
Screen control
The type for erase display modes.
erase_display ~mode erases parts of the screen (ED).
The type for erase line modes.
erase_line ~mode erases parts of the current line (EL).
set_scrolling_region ~top ~bottom restricts scrolling to rows top through bottom. top must be >= 1 and bottom > top.
Raises Invalid_argument if bounds are invalid.
reset_scrolling_region resets the scrolling region to the full screen.
Colors and attributes
set_foreground ~r ~g ~b sets the foreground to truecolor RGB. Components are clamped to [0, 255].
set_background ~r ~g ~b sets the background to truecolor RGB.
Terminal properties
explicit_width ~width ~text renders text as width cells wide (OSC 66). The text payload is emitted verbatim; ensure text does not contain unescaped ST sequences.
explicit_width_bytes ~width ~bytes ~off ~len is like explicit_width but writes directly from a byte buffer.
Operating System Commands (OSC)
The type for OSC terminators.
osc ~terminator ~payload emits an OSC sequence. terminator defaults to `St.
Hyperlinks (OSC 8)
hyperlink_start ~params ~url opens a hyperlink. params defaults to "" and holds optional key=value pairs. Must be paired with hyperlink_end.
hyperlink ~params ~url ~text emits a complete linked text segment.
Terminal modes
type mode = | Cursor_visible(*DECTCEM 25.
*)| Mouse_tracking(*DECSET 1000 — basic click.
*)| Mouse_motion(*DECSET 1003 — all motion.
*)| Mouse_sgr(*DECSET 1006 — SGR extended coords.
*)| Mouse_sgr_pixel(*DECSET 1016 — SGR pixel coords.
*)| Mouse_x10(*DECSET 9 — legacy X10.
*)| Urxvt_mouse(*DECSET 1015.
*)| Alternate_screen(*DECSET 1049.
*)| Focus_tracking(*DECSET 1004.
*)| Bracketed_paste(*DECSET 2004.
*)| Sync_output(*DECSET 2026.
*)| Unicode(*DECSET 2027.
*)| Color_scheme(*DECSET 2031.
*)
The type for DEC private modes.
Keyboard encoding
Device and capability queries
type query = | Cursor_position(*DSR 6.
*)| Pixel_size(*CSI 14 t.
*)| Device_attributes(*DA1.
*)| Tertiary_attributes(*DA3.
*)| Terminal_identity(*XTVERSION.
*)| Device_status(*DSR 5.
*)| Csi_u_support(*CSI ? u.
*)| Kitty_graphics(*Kitty graphics probe.
*)| Sixel_geometry(*Sixel geometry limits.
*)| Explicit_width_support(*OSC 66 width probe.
*)| Scaled_text_support(*OSC 66 scaled text probe.
*)| Color_scheme_query(*DSR 996.
*)| Focus_mode(*DECRQM 1004.
*)| Sgr_pixels_mode(*DECRQM 1016.
*)| Bracketed_paste_mode(*DECRQM 2004.
*)| Sync_mode(*DECRQM 2026.
*)| Unicode_mode(*DECRQM 2027.
*)| Color_scheme_mode(*DECRQM 2031.
*)
The type for terminal queries. The terminal responds asynchronously; parse responses via Parser.
High-level convenience
render ~hyperlinks_enabled segs renders styled segments to a string. Emits minimal SGR transitions by tracking terminal state. Closes open hyperlinks and appends a final reset.
hyperlinks_enabled defaults to true; when false, suppresses OSC 8 sequences.
parse s tokenizes s into ANSI tokens. Never raises; malformed sequences become Parser.control.Unknown controls.
strip s removes all ANSI escape sequences from s, returning plain text only.