package mosaic

  1. Overview
  2. Docs

Module Mosaic_ui.Text_bufferSource

Styled text storage with per-character styles and highlight overlays.

Styled text storage with per-character styles and highlight overlays.

A text buffer stores content as a sequence of styled spans. It tracks logical line boundaries and per-line display widths. Highlights (see Highlight) provide priority-based style overlays for selection and syntax highlighting.

Consumers typically wrap a buffer in a Text_surface.t for rendering.

Sourcetype t

A mutable styled text buffer.

Spans

Sourcetype span = {
  1. text : string;
  2. style : Ansi.Style.t;
}

A contiguous run of text with a single visual style.

Highlights

Sourcemodule Highlight : sig ... end

Character-range style overlays.

Construction

Sourceval create : ?default_style:Ansi.Style.t -> ?width_method:Glyph.width_method -> ?tab_width:int -> unit -> t

create () is an empty buffer with:

  • default_style: style applied to plain text set via set_text and append. Defaults to Ansi.Style.default.
  • width_method: grapheme width computation method. Defaults to `Unicode.
  • tab_width: tab stop width, clamped to >= 1. Defaults to 2.

Content

Sourceval set_text : t -> string -> unit

set_text t s replaces content with plain text s styled with default_style. Invalidates cached metrics.

Sourceval set_styled_text : t -> span list -> unit

set_styled_text t spans replaces content with pre-styled spans. Invalidates cached metrics.

Sourceval append : t -> string -> unit

append t s appends plain text s styled with default_style. Invalidates cached metrics.

Sourceval append_styled : t -> span list -> unit

append_styled t spans appends pre-styled spans to existing content. Invalidates cached metrics.

Sourceval clear : t -> unit

clear t removes all content and highlights.

Sourceval grapheme_count : t -> int

grapheme_count t is the total number of grapheme clusters.

Sourceval plain_text : t -> string

plain_text t is the concatenation of all span texts. Cached; recomputed on content change.

Default style

Sourceval default_style : t -> Ansi.Style.t

default_style t is the style applied to plain text via set_text and append.

Sourceval set_default_style : t -> Ansi.Style.t -> unit

set_default_style t s changes the default style. Does not re-style existing content.

Line information

Sourceval line_count : t -> int

line_count t is the number of logical lines (delimited by line breaks). Cached; recomputed on content change.

Sourceval line_width : t -> int -> int

line_width t n is the display width of logical line n. Returns 0 if n is out of range. Cached; recomputed on content change.

Sourceval max_line_width : t -> int

max_line_width t is the display width of the widest logical line. Cached; recomputed on content change.

Sourceval line_spans : t -> int -> span list

line_spans t n is the styled spans for logical line n. Uses cached line boundaries to avoid rescanning. Returns [] if n is out of range.

Sourceval text_in_range : t -> start:int -> len:int -> string

text_in_range t ~start ~len is the plain text for grapheme offsets from start (inclusive) to start + len (exclusive). If start + len exceeds grapheme_count, the result extends to the end of the buffer. Returns "" if start is out of bounds or len <= 0.

Highlight operations

Sourceval add_highlight : t -> Highlight.t -> unit

add_highlight t h adds a highlight overlay.

Sourceval remove_highlights_by_ref : t -> int -> unit

remove_highlights_by_ref t ref_id removes all highlights whose Highlight.ref_id equals ref_id.

Sourceval clear_highlights : t -> unit

clear_highlights t removes all highlights.

Sourceval highlights_in_range : t -> start:int -> len:int -> Highlight.t list

highlights_in_range t ~start ~len is the highlights intersecting grapheme offsets from start (inclusive) to start + len (exclusive), sorted by ascending Highlight.priority.

Tab width

Sourceval tab_width : t -> int

tab_width t is the current tab stop width. Defaults to 2.

Sourceval set_tab_width : t -> int -> unit

set_tab_width t w changes the tab stop width. w is clamped to >= 1. Invalidates cached line info if the value changes.

Width method

Sourceval width_method : t -> Glyph.width_method

width_method t is the grapheme width computation method.

Sourceval set_width_method : t -> Glyph.width_method -> unit

set_width_method t m changes the width method. Invalidates cached line info if m differs from the current value.

Versioning

Sourceval version : t -> int

version t is a monotonically increasing counter incremented whenever content, tab width, or width method changes. Useful for cache invalidation in consumers (e.g. Text_surface).