loom & weft
Made for Swift.

Published release 0.2.1. Unreleased PR #2 reference.

LOOM 0.2.1 / RENDERING AND LAYOUT

Buffer

Dimensions are clamped to zero or greater at initialization. Direct subscripting requires valid coordinates. Prefer put for clipped writes: it accepts exactly one grapheme and a width of 1 or 2, rejects terminal controls, and never paints half a wide grapheme. Wide trailing cells have width 0 and an empty symbol.

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

var buffer = Buffer(width: 8, height: 2)
buffer.put("界", width: 2, at: Point(x: 0, y: 0))
buffer.highlight(at: Point(x: 1, y: 0))
_ = buffer.plainText
Members on this page

Feature guides and release status

Buffer

Structure
struct Buffer

Conforms to: Swift.Sendable, Swift.SendableMetatype.

A rectangular grid of terminal cells with whole-grapheme clipping.

Source · Buffer.swift:4

width

Instance Property
let width: Int

Width in terminal cells.

Source · Buffer.swift:5

height

Instance Property
let height: Int

Height in terminal cells.

Source · Buffer.swift:6

area

Instance Property
var area: Rect { get }

The available zero-based rectangle in terminal cells.

Source · Buffer.swift:11

plainText

Instance Property
var plainText: String { get }

The visible symbols, with explicit newlines between buffer rows.

Source · Buffer.swift:16

init(width:height:)

Initializer
init(width: Int, height: Int)

Creates a value with the supplied arguments. Default arguments are shown in the declaration.

Parameters (2)
width
Available columns in terminal cells.
height
Available rows in terminal cells.
Source · Buffer.swift:26

subscript(_:_:)

Instance Subscript
subscript(x: Int, y: Int) -> Cell { get }

Accesses a cell at valid, zero-based buffer coordinates.

Parameters (2)
x
Argument type and any default are shown in the declaration; see the behavior notes above.
y
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns Cell.

Source · Buffer.swift:35

reset()

Instance Method
mutating func reset()

Clears every cell while retaining the allocated buffer.

Source · Buffer.swift:41

withClip(_:_:)

Instance Method
mutating func withClip(_ area: Rect, _ draw: (inout Buffer) -> Void)

Restricts writes to the intersection of this area and any enclosing clip.

Coordinates remain absolute. Whole graphemes crossing an edge are omitted.

Parameters (2)
area
Assigned absolute terminal-cell rectangle.
draw
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · Buffer.swift:50

highlight(at:)

Instance Method
mutating func highlight(at point: Point)

Highlights the entire grapheme occupying the selected cell.

Parameters (1)
at
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · Buffer.swift:105

clear(_:)

Instance Method
mutating func clear(_ region: Rect)

Clears complete glyphs inside the region and current clip.

Parameters (1)
region
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · Buffer.swift:140

apply(_:to:)

Instance Method
mutating func apply(_ patch: StylePatch, to region: Rect)

Applies appearance changes only to complete glyphs inside the region and current clip.

Parameters (2)
patch
Argument type and any default are shown in the declaration; see the behavior notes above.
to
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · Buffer.swift:145

copy(from:region:to:)

Instance Method
mutating func copy(from source: Buffer, region: Rect, to destination: Point)

Copies complete source glyphs to a destination origin, retaining styles and links.

Value semantics make copies from this buffer safe even when source and destination overlap.

Parameters (3)
from
Argument type and any default are shown in the declaration; see the behavior notes above.
region
Argument type and any default are shown in the declaration; see the behavior notes above.
to
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · Buffer.swift:163

compose(_:at:)

Instance Method
mutating func compose(_ source: Buffer, at destination: Point = Point(x: 0, y: 0))

Composes the complete source grid at the supplied destination origin.

Parameters (2)
source
Argument type and any default are shown in the declaration; see the behavior notes above.
at
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · Buffer.swift:187

resized(width:height:preservingContents:)

Instance Method
func resized(width: Int, height: Int, preservingContents: Bool = true) -> Buffer

Returns a resized grid, optionally retaining complete glyphs in the shared area.

Parameters (3)
width
Available columns in terminal cells.
height
Available rows in terminal cells.
preservingContents
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns Buffer.

Source · Buffer.swift:192

resize(width:height:preservingContents:)

Instance Method
mutating func resize(width: Int, height: Int, preservingContents: Bool = true)

Resizes the grid.

Existing glyphs crossing the new edge are omitted.

Parameters (3)
width
Available columns in terminal cells.
height
Available rows in terminal cells.
preservingContents
Argument type and any default are shown in the declaration; see the behavior notes above.
Source · Buffer.swift:201

differences(from:)

Instance Method
func differences(from previous: Buffer) -> [BufferDifference]

Returns cell-level changes, including removed cells and wide-glyph continuation cells.

Parameters (1)
from
Argument type and any default are shown in the declaration; see the behavior notes above.

Returns [BufferDifference].

Source · Buffer.swift:206