package matrix
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9
doc/matrix/Matrix/index.html
Module MatrixSource
Immediate-mode terminal application runtime.
Matrix owns the terminal while an application runs: it negotiates input protocols, builds frames against a double-buffered Grid.t, and diffs the grid to emit minimal ANSI output.
The module re-exports the lower-level subsystems as submodules and provides an immediate-mode event loop via create and run.
Sub-libraries
AnsiANSI escape sequence generation.GlyphUnicode glyph measurement.GridCell-based drawing grid.InputTerminal input event parsing.ScreenDouble-buffered frame rendering.TerminalTerminal protocol state machine.ImageDeclarative image composition and rendering.
Sub-libraries
Types
The type for Kitty keyboard protocol configurations.
`Autoenables the protocol when the terminal advertises support; falls back to modify-other-keys for Ctrl+Alt chords on legacy terminals.`Disablednever negotiates the protocol.`Enabled flagsforces activation with the given bitmask. See the Kitty keyboard protocol specification for flag values.
The type for presentation modes.
`Altuses the alternate screen buffer. The application fully owns the screen; content is restored on exit.`Primaryrenders inline on the primary screen below the current cursor row. Static output flows above the UI and enters native scrollback.
The type for debug overlay anchor corners.
Lifecycle
val create :
?mode:mode ->
?raw_mode:bool ->
?target_fps:float option ->
?respect_alpha:bool ->
?mouse_enabled:bool ->
?mouse:Terminal.mouse_mode option ->
?bracketed_paste:bool ->
?focus_reporting:bool ->
?kitty_keyboard:kitty_keyboard ->
?exit_on_ctrl_c:bool ->
?debug_overlay:bool ->
?debug_overlay_corner:debug_overlay_corner ->
?debug_overlay_capacity:int ->
?frame_dump_every:int ->
?frame_dump_dir:string ->
?frame_dump_pattern:string ->
?frame_dump_hits:bool ->
?cursor_visible:bool ->
?explicit_width:bool ->
?input_timeout:float option ->
?resize_debounce:float option ->
?output:[ `Stdout | `Fd of Unix.file_descr ] ->
?signal_handlers:bool ->
?initial_caps:Terminal.capabilities ->
?min_tui_height:int ->
?start_idle:bool ->
unit ->
appcreate () is a live application with Unix I/O wired in.
Sets up the terminal, enables raw mode, installs signal handlers, and returns a ready-to-run handle.
Display:
modepresentation mode. Defaults to`Alt.raw_modewhether to switch the TTY into raw mode. Defaults totrue.
Rendering:
respect_alphawhether to honour alpha blending. Defaults tofalse.cursor_visibleinitial cursor visibility. Defaults totruein`Altmode,falsein`Primary.explicit_widthwhether to use built-in wcwidth instead of querying the terminal. Defaults tofalse.
Frame timing:
target_fpsoptional FPS cap in Hz. Defaults toSome 30..Nonefor uncapped.resize_debouncedebounce window in seconds for resize events. Defaults toSome 0.1.
Primary mode:
min_tui_heightminimum height in rows reserved for the dynamic TUI region. Static content will not grow past this floor. Defaults to1.start_idlewhentrue, the render loop starts in idle state even whentarget_fpsis set. The loop begins running only whenrequest_liveis called. One-shot redraws viarequest_redrawstill work while idle. Defaults tofalse.
Input:
mouse_enabledwhether to enable mouse tracking. Defaults totrue.mouseexplicit mouse tracking mode. Defaults toNone(selects`Sgr_anywhenmouse_enabledistrue).bracketed_pastewhether to enable bracketed paste. Defaults totrue.focus_reportingwhether to enable focus-in/focus-out events. Defaults totrue.kitty_keyboardKitty keyboard protocol configuration. Defaults to`Auto.exit_on_ctrl_cwhether Ctrl+C exits. Defaults totrue.input_timeouttimeout in seconds for input polling when no cadence is active. Defaults toNone(block indefinitely).
Unix:
outputoutput target. Defaults to`Stdout.signal_handlerswhether to install SIGTERM/SIGINT/SIGQUIT handlers. Defaults totrue.initial_capsseed capabilities passed toTerminal.make.
Diagnostics:
debug_overlayshow the debug overlay. Defaults tofalse.debug_overlay_corneranchor corner. Defaults to`Bottom_right.debug_overlay_capacitymaximum metric samples retained.frame_dump_everydump every Nth frame. Defaults to0(disabled).frame_dump_dirdirectory for frame dumps.frame_dump_patternfilename pattern for frame dumps.frame_dump_hitsinclude hit grid in dumps. Defaults tofalse.
val run :
?on_frame:(app -> dt:float -> unit) ->
?on_input:(app -> Input.t -> unit) ->
?on_resize:(app -> cols:int -> rows:int -> unit) ->
?primary_required_rows:(app -> int option) ->
on_render:(app -> unit) ->
app ->
unitrun ~on_render app drives the immediate-mode event loop.
Each iteration:
- Polls for events and invokes
on_input/on_resize. - Calls
on_framewith the elapsed seconds since the last render. - Calls
prepareto clear buffers. - Invokes
on_render(draw togridhere). - Optionally samples
primary_required_rowsfor`Primarymode sizing. - Calls
submitto diff and flush output.
The loop exits when running becomes false. Exceptions close the runtime before propagating.
Frame building
hits app is the hit grid for the current frame.
Control
close app tears down protocols and releases resources. Safe to call multiple times.
stop app marks the runtime as stopped. The run loop exits on the next tick.
start app resumes the render cadence and marks the control state as explicitly started.
pause app stops the render cadence but leaves the terminal configured. start resumes.
suspend app pauses rendering and restores the terminal to cooked mode. resume reapplies configuration.
request_live app signals pending live work. Restarts the render cadence when transitioning from idle.
drop_live app decrements the live counter. When it reaches zero in auto mode the cadence idles.
request_redraw app marks the frame dirty for the next iteration.
Terminal queries
full_size app is the full terminal dimensions (cols, rows), ignoring the primary-mode render offset. In `Alt mode this is identical to size.
effective_size app is the dynamic-region dimensions (cols, rows) that will be in effect once pending static writes are flushed. In `Alt mode or when the static queue is empty this equals size. Use this in on_render to lay out the dynamic UI against the post-commit geometry.
pixel_resolution app is the last known pixel resolution as (width, height), or None if the terminal has not reported one.
terminal app is the underlying terminal handle.
capabilities app is the current terminal capabilities.
Static output
These functions write to the primary screen above the renderer. They are ignored in `Alt mode.
static_write app ~rows s writes s to the static area, using rows as the exact number of terminal rows consumed. The caller is responsible for computing rows accurately (e.g. from Grid.active_height after rendering to a grid).
static_clear app clears static content and resets the primary scroll region.
Cursor control
set_cursor ?visible ?style app updates cursor visibility and/or style.
set_cursor_style app ~style ~blinking sets cursor shape and blink.
set_cursor_position app ~row ~col moves the cursor to 1-based coordinates within the dynamic render region.
set_cursor_color app ~r ~g ~b ~a sets the cursor color. Components are in [0.;1.].
Diagnostics
set_debug_overlay app ~enabled shows or hides the debug overlay. corner defaults to the value given to create.
toggle_debug_overlay app flips overlay visibility.
val configure_frame_dump :
?every:int ->
?dir:string ->
?pattern:string ->
?hits:bool ->
app ->
unitconfigure_frame_dump app updates the periodic frame-dump schedule. See the diagnostics parameters of create.
dump_frame app writes the current frame to disk immediately.
Unix utilities
install_signal_handlers () installs shutdown handlers for SIGTERM, SIGINT, SIGQUIT, and SIGABRT. Idempotent.
Custom backends
attach creates an application wired to caller-provided I/O callbacks instead of Unix. Use this in tests or with alternative runtimes (e.g. Eio).
val attach :
?mode:mode ->
?raw_mode:bool ->
?target_fps:float option ->
?respect_alpha:bool ->
?mouse_enabled:bool ->
?mouse:Terminal.mouse_mode option ->
?bracketed_paste:bool ->
?focus_reporting:bool ->
?kitty_keyboard:kitty_keyboard ->
?exit_on_ctrl_c:bool ->
?debug_overlay:bool ->
?debug_overlay_corner:debug_overlay_corner ->
?debug_overlay_capacity:int ->
?frame_dump_every:int ->
?frame_dump_dir:string ->
?frame_dump_pattern:string ->
?frame_dump_hits:bool ->
?cursor_visible:bool ->
?explicit_width:bool ->
?input_timeout:float option ->
?resize_debounce:float option ->
?min_tui_height:int ->
?start_idle:bool ->
write_output:(bytes -> int -> int -> unit) ->
now:(unit -> float) ->
wake:(unit -> unit) ->
terminal_size:(unit -> int * int) ->
set_raw_mode:(bool -> unit) ->
flush_input:(unit -> unit) ->
read_events:(timeout:float option -> on_event:(Input.t -> unit) -> unit) ->
query_cursor_position:(timeout:float -> (int * int) option) ->
cleanup:(unit -> unit) ->
parser:Input.Parser.t ->
terminal:Terminal.t ->
width:int ->
height:int ->
?render_offset:int ->
?static_needs_newline:bool ->
unit ->
appattach ... () is like create but wired to caller-provided I/O callbacks.
The optional parameters mirror create. The required callbacks are:
write_outputwriteslenbytes frombufatoff.nowreturns the current monotonic time in seconds.wakesignals the event loop to re-check state.terminal_sizereturns(cols, rows).set_raw_modetoggles raw mode.flush_inputdiscards pending input.read_eventsblocks for events up totimeout, invokingon_eventfor each.query_cursor_positionqueries cursor position with the given timeout.cleanupreleases resources onclose.
parser is the input parser, terminal the protocol handle, and width/height the initial dimensions.
render_offset defaults to 0. static_needs_newline defaults to false.