package matrix
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9
doc/matrix.screen/Screen/index.html
Module ScreenSource
Double-buffered terminal screen.
Screen builds terminal frames by mutating a next buffer, diffs it against the current buffer to generate minimal ANSI escape sequences, then swaps the two. Post-processing transforms can run between building and diffing to apply animations or visual filters.
Double buffering
The screen maintains two grid buffers. During build the caller populates the next buffer. During render the next buffer is diffed against current to produce ANSI output, then the buffers swap and the (now-next) buffer is cleared. Content must be redrawn every frame.
Hit testing
A Hit_grid.t is maintained alongside each visual grid, mapping screen coordinates to integer element IDs. Call Hit_grid.add during frame building to register clickable regions. Query coordinates with query_hit after rendering.
Post-processing
Post-processors are persistent functions registered via post_process. They run after building and before diffing, receiving the grid and the delta time since the last frame. Processors execute in insertion order and persist across frames until removed.
Invariants
- The
Grid.tpassed tobuildis the next buffer. Afterrenderthe buffers swap and the now-next buffer is cleared. - Visual and hit buffers swap on
render: regions registered viaHit_grid.addbecome queryable only after the following render. - Post-processors always run, even when no cells changed. Their
~deltaargument is milliseconds since the lastrender(0.for the first frame). set_mouse_enabledandset_cursor_visibleupdate state only; rendering does not emit terminal mode changes.
Types
The type for screens. Manages double-buffered grids, hit grids, post-processing pipeline, statistics, and frame rate control.
type stats = {frame_count : int;total_cells : int;(*Sum of cells diffed across frames.
*)total_bytes : int;(*Sum of ANSI bytes emitted.
*)
}The type for cumulative rendering statistics.
type frame_metrics = {frame_count : int;(*One-based frame index.
*)cells : int;(*Cells diffed.
*)bytes : int;(*ANSI bytes written.
*)frame_time_ms : float;(*Diff/render duration.
*)interval_ms : float;(*Time since previous render.
*)reset_ms : float;(*Buffer swap/reset duration.
*)overall_frame_ms : float;(*Wall-clock duration of the enclosing render call.
*)frame_callback_ms : float;(*Time spent in the frame builder callback.
*)stdout_ms : float;(*Time spent writing to output.
*)mouse_enabled : bool;(*Mouse state at render time.
*)cursor_visible : bool;(*Cursor state at render time.
*)timestamp_s : float;
}The type for per-frame metrics. All durations are in milliseconds unless noted otherwise.
Constructors
val create :
?glyph_pool:Glyph.Pool.t ->
?width_method:Glyph.width_method ->
?respect_alpha:bool ->
?mouse_enabled:bool ->
?cursor_visible:bool ->
?explicit_width:bool ->
unit ->
tcreate ~glyph_pool ~width_method ~respect_alpha ~mouse_enabled ~cursor_visible ~explicit_width () is a screen with:
glyph_poolstores multi-width grapheme clusters. Defaults to a fresh pool. Share a pool across screens to reduce memory for common text.width_methodis the character width computation method. Defaults to`Unicode. SeeGlyph.width_method.respect_alphaenables alpha blending when drawing cells. Defaults tofalse.mouse_enabledis the initial mouse-enabled flag. Defaults totrue.cursor_visibleis the initial cursor visibility. Defaults totrue.explicit_widthenables explicit-width OSC sequences for graphemes. Defaults tofalse.
Grids start at 1x1 and are resized automatically on the first build.
Frame building
build t ~width ~height f builds a frame.
Resizes buffers to width x height when both are positive, clears the hit grid, then calls f with the next grid and hit grid for in-place mutation. Returns t for chaining.
When width <= 0 or height <= 0, f is not called; only the hit grid is cleared.
Warning. The Grid.t and Hit_grid.t passed to f must not be mutated after the next render -- they become the diff baseline.
Shared resources
glyph_pool t is the glyph pool shared by t's grids.
Post-processing
The type for post-processing effect identifiers.
post_process f t registers f as a persistent post-processing transform and is its effect_id.
f is called during each render after frame building but before diffing. It receives the next grid and ~delta (milliseconds since last render, 0. on the first frame). Processors run in insertion order and persist across frames until removed.
See also remove_post_process.
remove_post_process id t unregisters the post-processor identified by id. Returns t for chaining.
clear_post_processes t removes all post-processing functions. Returns t for chaining.
add_hit_region t ~x ~y ~width ~height ~id registers a hit region on the next hit grid. Convenience wrapper around Hit_grid.add for use outside the build callback. Regions outside grid bounds are clipped. Negative dimensions are clamped to zero. Returns t for chaining.
Rendering
render ~full ~height_limit t is the ANSI output for the current frame.
Applies post-processors, diffs next against current (or renders all cells when full is true), then swaps buffers. Hit regions registered during this frame become queryable via query_hit.
fullrenders all cells regardless of changes. Defaults tofalse.height_limitlimits rendering to the firstheight_limitrows.
See also render_to_bytes.
render_to_bytes ~full ~height_limit t buf is like render but writes into buf and is the number of bytes written. buf must be large enough for the output.
Screen state
set_mouse_enabled t b updates the desired mouse-enabled flag. Rendering does not emit mode changes.
set_cursor_visible t b updates the desired cursor visibility. Rendering does not emit mode changes.
set_explicit_width t b enables or disables explicit-width OSC emission for graphemes. When enabled and the terminal supports it, OSC sequences specifying the exact width of multi-width characters are emitted to prevent terminal-side width mismatch.
set_cursor_position t ~row ~col sets the desired cursor coordinates. row and col are one-based terminal coordinates where (1, 1) is the top-left corner.
clear_cursor_position t clears any requested cursor position so the cursor remains wherever the diff body last moved it.
set_cursor_style t ~style ~blinking sets the cursor's visual shape and blinking behaviour.
set_cursor_color t ~r ~g ~b sets the cursor colour via OSC 12. Components outside [0; 255] are clamped.
reset_cursor_color t restores the terminal's default cursor colour.
Cursor info
type cursor_info = {row : int;(*One-based row.
*)col : int;(*One-based column.
*)has_position : bool;(*
*)trueiff a position has been set.style : [ `Block | `Line | `Underline ];(*Cursor shape.
*)blinking : bool;(*
*)trueiff the cursor blinks.color : (int * int * int) option;(*
*)Some (r, g, b)orNonefor the terminal default.visible : bool;(*
*)trueiff the cursor is logically visible.
}The type for cursor state snapshots.
cursor_info t is the current desired cursor state.
Capabilities
val apply_capabilities :
t ->
explicit_width:bool ->
explicit_cursor_positioning:bool ->
hyperlinks:bool ->
unitapply_capabilities t ~explicit_width ~explicit_cursor_positioning ~hyperlinks applies terminal capability flags to t.
explicit_width: whether the terminal supports explicit-width OSC sequences.explicit_cursor_positioning: whether to reposition the cursor after wide graphemes as a fallback whenexplicit_widthisfalse. Prevents column drift in terminals that miscalculate grapheme display widths.hyperlinks: whether the terminal supports OSC 8 hyperlinks.
set_width_method t m sets the grapheme width computation method on both the current and next buffers. Use this after capability changes that affect width calculation to keep buffers consistent across swaps.
Layout
resize t ~width ~height resizes all internal buffers to width x height. Grid contents are preserved where dimensions overlap; hit grids are cleared.
Raises Invalid_argument if width <= 0 or height <= 0.
Note. Normally unnecessary -- build resizes automatically.
reset t clears the next buffer, empties hit grids, zeros statistics, and resets frame timing. The current buffer (diff baseline) is left intact so the next render can efficiently clear previously rendered content. Post-processors and configuration are preserved.
Statistics
stats t is cumulative rendering statistics since creation or the last reset.
last_metrics t is the metrics for the most recent frame.
val record_runtime_metrics :
t ->
frame_callback_ms:float ->
overall_frame_ms:float ->
stdout_ms:float ->
unitrecord_runtime_metrics t ~frame_callback_ms ~overall_frame_ms ~stdout_ms supplements the most recent metrics with runtime measurements. All values are in milliseconds. Intended for higher-level runtimes that measure draw-call duration, total wall-clock time, and output flush time on behalf of the screen.
Direct access
Direct access to internal buffers for advanced use cases. These functions bypass the builder API.
grid t is t's next buffer grid.
Warning. Do not mutate the returned grid after the next render -- it becomes the diff baseline.
query_hit t ~x ~y is the element ID at (x, y) in the current hit grid (i.e. regions from the most recent render). Returns 0 if out of bounds or no region is registered.
set_row_offset t n sets the vertical origin offset applied to all subsequent renders. Negative values are clamped to zero. Useful for inline primary-screen rendering.
invalidate_presented t clears the current buffer so the diff renderer treats all cells as changed on the next render.
Call this after physically erasing the terminal region to keep the renderer's baseline in sync with the actual terminal state.
Note. Does not emit any escape sequences.