Page
Library
Module
Module type
Parameter
Class
Class type
Source
AnsiSourceANSI 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.
The type for escape sequences. A function that writes bytes to a writer. Sequences compose by function application.
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.
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.
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_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).
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).
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.
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.
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.
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.
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.
The type for OSC terminators.
osc ~terminator ~payload emits an OSC sequence. terminator defaults to `St.
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.
type mode = | Cursor_visibleDECTCEM 25.
*)| Mouse_trackingDECSET 1000 — basic click.
*)| Mouse_motionDECSET 1003 — all motion.
*)| Mouse_sgrDECSET 1006 — SGR extended coords.
*)| Mouse_sgr_pixelDECSET 1016 — SGR pixel coords.
*)| Mouse_x10DECSET 9 — legacy X10.
*)| Urxvt_mouseDECSET 1015.
*)| Alternate_screenDECSET 1049.
*)| Focus_trackingDECSET 1004.
*)| Bracketed_pasteDECSET 2004.
*)| Sync_outputDECSET 2026.
*)| UnicodeDECSET 2027.
*)| Color_schemeDECSET 2031.
*)The type for DEC private modes.
type query = | Cursor_positionDSR 6.
*)| Pixel_sizeCSI 14 t.
*)| Device_attributesDA1.
*)| Tertiary_attributesDA3.
*)| Terminal_identityXTVERSION.
*)| Device_statusDSR 5.
*)| Csi_u_supportCSI ? u.
*)| Kitty_graphicsKitty graphics probe.
*)| Sixel_geometrySixel geometry limits.
*)| Explicit_width_supportOSC 66 width probe.
*)| Scaled_text_supportOSC 66 scaled text probe.
*)| Color_scheme_queryDSR 996.
*)| Focus_modeDECRQM 1004.
*)| Sgr_pixels_modeDECRQM 1016.
*)| Bracketed_paste_modeDECRQM 2004.
*)| Sync_modeDECRQM 2026.
*)| Unicode_modeDECRQM 2027.
*)| Color_scheme_modeDECRQM 2031.
*)The type for terminal queries. The terminal responds asynchronously; parse responses via Parser.
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.