Module Mosaic_ui.Text Styled text display with wrapping, truncation, and selection.
Rich text rendering with styled fragments.
A text node renders styled content into the grid, handling line breaks and optional word/character wrapping. Content can be plain text (via set_content ) or structured rich text with per-run styles (via set_fragments or set_spans ).
Internally backed by a Text_buffer.t and Text_surface.t .
RepresentationsText supports two content representations:
Content : a plain string set via set_content or the content parameter of create . Styled with Default style .Fragments/Spans : structured styled content set via set_fragments or set_spans . Fragments support nested style hierarchies; spans are flat styled text chunks. Style mergingStyles merge hierarchically: parent styles provide the base and child overrides apply on top, preserving unspecified parent attributes. The text_style parameter provides the base style for all content.
See Text_input for single-line editing and Textarea for multi-line editing.
Typestype fragment = | Text of { text : string; style : Ansi.Style.t option ; } | Span of { style : Ansi.Style.t option ; children : fragment list ; } Structured text fragments for rich text composition.
Text: leaf node with text and optional style override.Span: container applying an optional style to all children.A flat styled text chunk. style overrides the default text style when Some; None inherits the default.
Fragment builderConvenience constructors for fragment values.
Constructionval create :
parent :Renderable.t ->
?index :int ->
?id :string ->
?style :Toffee .Style.t ->
?visible :bool ->
?z_index :int ->
?opacity :float ->
?content :string ->
?text_style :Ansi.Style.t ->
?wrap :Text_surface.wrap ->
?selectable :bool ->
?selection_bg :Ansi.Color.t ->
?selection_fg :Ansi.Color.t ->
?tab_width :int ->
?truncate :bool ->
unit ->
t create ~parent () is a text node attached to parent with:
content: plain text content. Defaults to "".text_style: base visual style for all content. Defaults to Ansi.Style.default .wrap: wrapping mode. Defaults to `None.selectable: whether text can be selected. Defaults to true.selection_bg: background color for selected text. Defaults to None.selection_fg: foreground color for selected text. Defaults to None.tab_width: tab stop width. Defaults to 2.truncate: whether to truncate with ellipsis when wrap = `None. Defaults to false. Accessorsnode t is the underlying renderable.
buffer t is the underlying text buffer.
surface t is the underlying text surface.
Propsmodule Props : sig ... end apply_props t props applies props to t, triggering the minimum necessary layout and render updates.
Contentval set_content : t -> string -> unitset_content t s sets plain text content styled with set_text_style . Invalidates display lines and marks layout dirty.
val set_fragments : t -> fragment list -> unitset_fragments t fragments replaces content with fragments. Normalization merges adjacent text with identical styles, removes empty fragments, and flattens empty spans. No-op if the normalized result equals the current fragments.
fragments t is the current fragment list.
fragments_equal a b is true iff a and b are structurally equal.
val plain_text : t -> stringplain_text t is the concatenation of all fragment texts, without styling.
val spans : t -> span listspans t is the current fragments as a flat span list. Cached; recomputed on fragment changes.
val set_spans : t -> span list -> unitset_spans t spans replaces content with flat spans. Converts to fragments internally.
val append_span : t -> span -> unitappend_span t s appends s to the end of the text.
val clear_spans : t -> unitclear_spans t removes all content.
val set_styled_text : t -> Text_buffer.span list -> unitset_styled_text t spans sets pre-styled buffer content directly. After calling this, apply_props will not overwrite content from Props .
val set_text_style : t -> Ansi.Style.t -> unitset_text_style t s changes the base text style. Invalidates the span cache if the style differs.
Wrappingval set_tab_width : t -> int -> unit Selectionval set_selectable : t -> bool -> unitset_selectable t v enables or disables text selection.
set_selection_bg t color sets the selection background color.
set_selection_fg t color sets the selection foreground color.
val selected_text : t -> stringselected_text t is the currently selected text. Returns "" if no selection is active.
Highlightsadd_highlight t h adds a highlight overlay and requests a render.
val remove_highlights_by_ref : t -> int -> unitval clear_highlights : t -> unitclear_highlights t removes all highlights and requests a render.
Queryval line_count : t -> intline_count t is the number of logical lines.
val display_line_count : t -> intdisplay_line_count t is the number of wrapped display lines.
Formattingpp ppf t formats t on ppf for debugging.