package matrix
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9
doc/matrix.grid/Grid/index.html
Module GridSource
Mutable grid of terminal cells.
A grid is a two-dimensional framebuffer where each cell stores a character (Glyph.t), foreground and background colors (RGBA), text attributes, and a hyperlink. Backed by bigarrays for cache-friendly access.
Single codepoints are stored directly as packed integers. Multi-codepoint grapheme clusters (ZWJ emoji, combining characters) are interned in a reference-counted Glyph.Pool.t. Wide characters span multiple cells: a start cell followed by continuation markers.
When respect_alpha is enabled, colors with alpha < 1.0 are blended with existing cell colors using a perceptual curve. A stack of scissor clipping regions constrains drawing; cells outside the active clip are silently skipped.
Types
The type for mutable cell grids.
The type for rectangular areas in cell coordinates. Covers cells satisfying x <= col < x + width and y <= row < y + height. Non-positive width or height yields an empty region.
Constructors
val create :
width:int ->
height:int ->
?glyph_pool:Glyph.Pool.t ->
?width_method:Glyph.width_method ->
?respect_alpha:bool ->
unit ->
tcreate ~width ~height () is a grid with all cells set to spaces (white foreground, transparent background) with:
glyph_poolshared grapheme storage. A fresh pool is allocated if omitted.width_methodgrapheme width method. Defaults to`Unicode.respect_alphaalpha blending onset_cell. Defaults tofalse.
Raises Invalid_argument if width <= 0 or height <= 0.
Properties
glyph_pool g is the glyph pool used by g.
width_method g is the current width computation method.
set_width_method g m changes the width method for subsequent draw_text calls. Existing cell widths are not updated.
respect_alpha g is true iff alpha blending is enabled for set_cell and blit_region.
active_height g is the number of rows from the top containing non-blank content (character code other than 0 or space). Returns 0 for an empty or cleared grid.
Cell access
Linear index idx is row-major: idx = y * width + x.
idx g ~x ~y is the flat index for cell (x, y). No bounds checking.
get_text g idx is the grapheme at idx as a string. Returns "" for empty or continuation cells.
get_style g idx reconstructs the style at idx.
get_background g idx is the background color at idx.
Predicates
is_empty g idx is true iff the cell is the null/empty glyph sentinel. Different from a blank space cell (Glyph.space), which is the default content after create and clear.
is_continuation g idx is true iff the cell is the trailing part of a wide character.
is_inline g idx is true iff the cell needs no glyph pool lookup (ASCII or single codepoint).
cell_width g idx is the display width of the cell (0 for empty/continuation, 1–2 for start).
cells_equal g1 idx1 g2 idx2 is true iff both cells have identical content and styling. Uses epsilon comparison for RGBA floats.
hyperlink_url g id resolves a link ID (from get_link) to a URL. Returns None for the no-link sentinel or unknown IDs.
hyperlink_url_direct g id is like hyperlink_url but returns "" instead of None.
Manipulation
resize g ~width ~height resizes the grid, preserving existing contents where possible. Cells outside the new bounds are released. No-op if dimensions are unchanged.
Raises Invalid_argument if width <= 0 or height <= 0.
clear ~color g resets all cells to spaces with white foreground and color background. color defaults to transparent. Releases all glyph pool references. The scissor stack is preserved.
blit ~src ~dst copies all cell data from src to dst, resizing dst to match. When pools are shared, codes are copied verbatim. When pools differ, each distinct grapheme is re-interned once. No alpha blending.
copy g is a deep copy of g sharing the same glyph pool. The scissor stack starts empty on the copy.
val blit_region :
src:t ->
dst:t ->
src_x:int ->
src_y:int ->
width:int ->
height:int ->
dst_x:int ->
dst_y:int ->
unitblit_region ~src ~dst ~src_x ~src_y ~width ~height ~dst_x ~dst_y copies a rectangular region from src to dst.
The region is clamped to valid bounds in both grids. Negative coordinates shift the region inward. Respects the scissor on dst. Alpha blending occurs when dst's respect_alpha is true or source alpha < 1.0. Same-grid overlapping regions are handled correctly. Never resizes dst.
fill_rect g ~x ~y ~width ~height ~color fills a rectangle:
- Transparent (alpha ≈ 0): clears content, preserves existing background.
- Semi-transparent: blends over existing background.
- Opaque: overwrites entirely (space glyph, white foreground,
colorbackground).
Clipped to grid bounds and scissor.
Drawing
val draw_text :
?style:Ansi.Style.t ->
?tab_width:int ->
t ->
x:int ->
y:int ->
text:string ->
unitdraw_text ~style ~tab_width g ~x ~y ~text draws single-line text. Text is segmented into grapheme clusters using the current width_method. Wide characters occupy multiple cells with continuation markers. Newlines are skipped; tabs expand to tab_width spaces (default 2).
When the resolved background has alpha < 1.0, colors are blended with existing cells. A space on a translucent background preserves the existing glyph and tints its colors.
Respects the active scissor.
val draw_box :
t ->
x:int ->
y:int ->
width:int ->
height:int ->
?border:Border.t ->
?sides:Border.side list ->
?style:Ansi.Style.t ->
?fill:Ansi.Color.t ->
?title:string ->
?title_alignment:[ `Left | `Center | `Right ] ->
?title_style:Ansi.Style.t ->
unit ->
unitdraw_box g ~x ~y ~width ~height () draws a box with Unicode borders and:
bordercharacter set. Defaults toBorder.single.sidesto draw. Defaults toBorder.all.stylefor border characters. Defaults toAnsi.Style.default.fillinterior and border cell background color. When absent, no fill is applied.titleon the top border when\`Topis included andwidth >= title_width + 4.title_alignmentdefaults to\`Leftwith 2-cell padding.title_stylefor the title text.
Respects the scissor.
type line_glyphs = {h : string;(*Horizontal segment.
*)v : string;(*Vertical segment.
*)diag_up : string;(*Diagonal up-right.
*)diag_down : string;(*Diagonal down-right.
*)
}The type for line drawing glyph sets.
Unicode box-drawing: "─", "│", "╱", "╲".
ASCII: "-", "|", "/", "\\".
val draw_line :
t ->
x1:int ->
y1:int ->
x2:int ->
y2:int ->
?style:Ansi.Style.t ->
?glyphs:line_glyphs ->
?kind:[ `Line | `Braille ] ->
unit ->
unitdraw_line g ~x1 ~y1 ~x2 ~y2 () draws a line using Bresenham's algorithm with:
\`Line(default) uses box-drawing characters with per-step glyph selection based on direction.\`Brailleuses 2×4 dot patterns that merge with existing braille cells, allowing multiple lines to share cells.
Respects the scissor.
Direct cell write
val set_cell :
t ->
x:int ->
y:int ->
glyph:Glyph.t ->
fg:Ansi.Color.t ->
bg:Ansi.Color.t ->
attrs:Ansi.Attr.t ->
?link:string ->
?blend:bool ->
unit ->
unitset_cell g ~x ~y ~glyph ~fg ~bg ~attrs () writes a single cell. blend defaults to the grid's respect_alpha setting; pass ~blend:true to force blending.
Cells outside the grid or scissor are skipped. Existing wide graphemes spanning this cell are cleaned up. The caller is responsible for writing continuation cells for multi-column graphemes.
Clipping
push_clip g r pushes a clipping region. The effective clip is the intersection of r with the current clip.
clip g r f runs f () with r as the active clip, popping it on return (even on exception).
Opacity stack
Hierarchical opacity for UI trees. Drawing operations multiply color alpha by the product of all stacked opacities. Push/pop pairs must be balanced.
pop_opacity g pops the most recent opacity. No-op if the stack is empty.
current_opacity g is the product of all stacked opacities, or 1.0 if the stack is empty.
Scrolling
scroll g ~top ~bottom n scrolls rows [top..bottom] by n lines. Positive n scrolls content up (new blank lines at bottom). Negative n scrolls down. Zero is a no-op.
Comparison
diff_cells prev curr is the (x, y) coordinates of cells that differ. Iterates over the union of both grids' dimensions. Uses epsilon comparison for RGBA. Sorted by row then column.