Published release 0.2.1. Unreleased PR #2 reference.
LOOM 0.2.1 / TEXT, MARKDOWN, AND EDITING
TextLayout
Caches sanitized source and display-cell geometry. Width is at least one cell. Newlines and tabs remain in copied source; controls are filtered. Tabs expand by TextWrapOptions.tabWidth cells (default four), not terminal tab stops. Wrapping and paint/hit/copy paths share whole-grapheme geometry. Rebuild after text/width changes.
Standard inherited Swift protocol operations such as synthesized equality and OptionSet set operations are not repeated. The public declarations below are extracted from the exact source revision listed in the coverage manifest.
Example
let layout = TextLayout("Hello 世界", width: 12, wrapping: .word)
let selection = TextSelection(anchor: Point(x: 0, y: 0), head: Point(x: 4, y: 0))
_ = layout.selectedText(selection)
_ = layout.lines[0].glyphsMembers on this page
Feature guides and release status
TextLayout
struct TextLayoutConforms to: Swift.Sendable, Swift.SendableMetatype.
The single source of geometry for painting, hit testing, highlighting, and copying.
Construct once when text or available width changes. Mouse movement never lays out text.
Source · TextLayout.swift:30Glyph
struct GlyphConforms to: Swift.Sendable, Swift.SendableMetatype.
One laid-out extended grapheme, including its cell position, width, style, link, and source String.Index range. Obtain these values from a layout’s lines; there is no public initializer.
Source · TextLayout.swift:31Glyph.text
let text: StringThe stored text content.
Source · TextLayout.swift:32Glyph.column
let column: IntThe glyph’s starting cell column within its row.
Source · TextLayout.swift:33Glyph.width
let width: IntCell width of this laid-out grapheme, normally 1 or 2.
Source · TextLayout.swift:34Glyph.sourceRange
let sourceRange: Range<String.Index>String indices into the parent TextLayout.source.
Source · TextLayout.swift:35Glyph.style
let style: Style?Visual attributes used when painting this value.
Source · TextLayout.swift:36Glyph.patch
let patch: StylePatch?Changes resolved over the explicit glyph style or the inherited paint style.
Source · TextLayout.swift:38Glyph.link
let link: String?Optional hyperlink destination; terminal emission also requires hyperlinks enabled on the renderer.
Source · TextLayout.swift:39Glyph.copyable
let copyable: BoolWhether this content participates in copied selections.
Source · TextLayout.swift:40Line
struct LineConforms to: Swift.Sendable, Swift.SendableMetatype.
One cached display row, including laid-out glyphs and code-block metadata. Obtain rows from TextLayout.lines; construction and source geometry are managed by the layout.
Source · TextLayout.swift:56Line.glyphs
var glyphs: [Glyph]Laid-out graphemes for this display row.
Source · TextLayout.swift:57Line.codeBlock
var codeBlock: TextCodeBlock? { get }Original code and panel styling for this laid-out row, including soft wraps.
Source · TextLayout.swift:59Line.isCodeBlockHeader
var isCodeBlockHeader: Bool { get }Whether this span/row is a code-block header.
Source · TextLayout.swift:60Line.codeBlockStyle
var codeBlockStyle: Style? { get }Optional style for the code panel represented by this row.
Source · TextLayout.swift:61source
let source: StringThe sanitized source string used by this layout’s source indices.
Source · TextLayout.swift:70width
let width: IntWidth in terminal cells.
Source · TextLayout.swift:71lines
let lines: [Line]Cached display rows.
Source · TextLayout.swift:72paddedVertically(_:)
func paddedVertically(_ rows: Int) -> TextLayoutAdds presentation rows without adding characters to the selectable source.
Parameters (1)
rows- Row count or signed row movement, according to the method.
Returns TextLayout.
tailPreview(rows:reservedHeaderRows:omissionStyle:)
func tailPreview(rows: Int, reservedHeaderRows: Int = 0, omissionStyle: Style? = nil) -> TextLayoutKeeps the last wrapped rows while retaining source indices for copying.
Reserved header rows and the omission marker are presentation only.
Parameters (3)
rows- Row count or signed row movement, according to the method.
reservedHeaderRows- Argument type and any default are shown in the declaration; see the behavior notes above.
omissionStyle- Argument type and any default are shown in the declaration; see the behavior notes above.
Returns TextLayout.
headPreview(rows:verticalPadding:)
func headPreview(rows: Int, verticalPadding: Int = 0) -> TextLayoutKeeps the first wrapped content rows and any vertical padding, with a display-only omission mark.
Parameters (2)
rows- Row count or signed row movement, according to the method.
verticalPadding- Argument type and any default are shown in the declaration; see the behavior notes above.
Returns TextLayout.
init(_:width:wrapping:options:measure:)
init(_ text: String, width: Int, wrapping: TextWrapping = .grapheme, options: TextWrapOptions = TextWrapOptions(), measure: (Character) -> Int = terminalCellWidth)Creates a value with the supplied arguments. Default arguments are shown in the declaration.
Parameters (5)
text- Source text.
width- Available columns in terminal cells.
wrapping- Grapheme or word wrapping policy.
options- Terminal policies selected when opening.
measure- Returns terminal-cell width for one Character.
init(_:width:wrapping:continuationPrefix:options:measure:)
init(_ spans: [TextSpan], width: Int, wrapping: TextWrapping = .grapheme, continuationPrefix: TextSpan? = nil, options: TextWrapOptions = TextWrapOptions(), measure: (Character) -> Int = terminalCellWidth)Lays out styled spans as one source string.
A grapheme crossing a span boundary uses the style and patch of its first scalar, keeping selection intact. An explicit continuation prefix overrides repeated display-only leading glyphs on soft wraps, without entering copied text.
Parameters (6)
spans- Styled source spans, in order.
width- Available columns in terminal cells.
wrapping- Grapheme or word wrapping policy.
continuationPrefix- Argument type and any default are shown in the declaration; see the behavior notes above.
options- Terminal policies selected when opening.
measure- Returns terminal-cell width for one Character.
sourceRange(for:)
func sourceRange(for selection: TextSelection) -> Range<String.Index>?Returns the source String.Index range covered by a display selection, or nil when no source grapheme is selected. Use indices only with this layout’s source.
Parameters (1)
for- Argument type and any default are shown in the declaration; see the behavior notes above.
Returns Range<String.Index>?.
selectedText(_:)
func selectedText(_ selection: TextSelection) -> StringCopies sanitized source covered by the inclusive display selection. Excludes non-copyable presentation spans and includes source newlines/tabs rather than soft-wrap separators.
Parameters (1)
selection- Selection using the containing type’s coordinate convention.
Returns String.
wordSelection(at:)
func wordSelection(at point: Point) -> TextSelection?Selects the word or punctuation run under a laid-out cell, across soft wraps.
Parameters (1)
at- Argument type and any default are shown in the declaration; see the behavior notes above.
Returns TextSelection?.
paragraphSelection(at:)
func paragraphSelection(at point: Point) -> TextSelection?Selects the source line under a cell, including any soft-wrapped display rows.
Parameters (1)
at- Argument type and any default are shown in the declaration; see the behavior notes above.
Returns TextSelection?.
horizontalOrigin(forRow:viewportWidth:alignment:)
func horizontalOrigin(forRow row: Int, viewportWidth: Int, alignment: CellAlignment = .left) -> IntThe content-to-viewport shift for a row.
Overwide lines stay start aligned.
Parameters (3)
forRow- Argument type and any default are shown in the declaration; see the behavior notes above.
viewportWidth- Argument type and any default are shown in the declaration; see the behavior notes above.
alignment- Argument type and any default are shown in the declaration; see the behavior notes above.
Returns Int.
contentPoint(at:in:rowOffset:columnOffset:alignment:)
func contentPoint(at point: Point, in area: Rect, rowOffset: Int = 0, columnOffset: Int = 0, alignment: CellAlignment = .left) -> Point?Converts an absolute viewport cell to the logical content coordinates used by selections.
Blank alignment padding and cells without a copyable glyph return nil.
Parameters (5)
at- Argument type and any default are shown in the declaration; see the behavior notes above.
in- Argument type and any default are shown in the declaration; see the behavior notes above.
rowOffset- Number of cached layout rows skipped before painting.
columnOffset- Argument type and any default are shown in the declaration; see the behavior notes above.
alignment- Argument type and any default are shown in the declaration; see the behavior notes above.
Returns Point?.
viewportPoint(for:in:rowOffset:columnOffset:alignment:)
func viewportPoint(for point: Point, in area: Rect, rowOffset: Int = 0, columnOffset: Int = 0, alignment: CellAlignment = .left) -> Point?Converts logical content coordinates to absolute viewport coordinates.
Parameters (5)
for- Argument type and any default are shown in the declaration; see the behavior notes above.
in- Argument type and any default are shown in the declaration; see the behavior notes above.
rowOffset- Number of cached layout rows skipped before painting.
columnOffset- Argument type and any default are shown in the declaration; see the behavior notes above.
alignment- Argument type and any default are shown in the declaration; see the behavior notes above.
Returns Point?.
paint(in:rowOffset:selection:style:columnOffset:alignment:buffer:)
func paint(in area: Rect, rowOffset: Int = 0, selection: TextSelection? = nil, style: Style = Style(), columnOffset: Int = 0, alignment: CellAlignment = .left, buffer: inout Buffer)Paints cached glyphs with shared horizontal alignment, scrolling, clipping, and source selection geometry.
Parameters (7)
in- Argument type and any default are shown in the declaration; see the behavior notes above.
rowOffset- Number of cached layout rows skipped before painting.
selection- Selection using the containing type’s coordinate convention.
style- Text/cell appearance.
columnOffset- Argument type and any default are shown in the declaration; see the behavior notes above.
alignment- Argument type and any default are shown in the declaration; see the behavior notes above.
buffer- Mutable destination cell grid.