Class
MTLWriter
Thread-safe actor for accumulating generated text with automatic indentation.
actor MTLWriter
Overview
MTL writers manage the output buffer for text generation, providing thread-safe text accumulation with automatic indentation handling. Writers track whether they are at the start of a line to apply indentation only when needed.
Overview
MTL writers support:
-
Thread-safe accumulation: Actor isolation ensures safe concurrent access
-
Automatic indentation: Indentation is applied at the start of each line
-
Line tracking: Knows when to apply indentation based on line position
-
Content retrieval: Accumulated text can be retrieved and cleared
-
Conditional indentation: Individual writes can opt out of indentation
Indentation Behavior
Indentation is automatically applied:
-
At the start of the first write
-
After each newline when writing new content
-
Unless explicitly disabled with
indent: false
let writer = MTLWriter(indentation: MTLIndentation(level: 1))
await writer.write("Hello") // " Hello" (indented)
await writer.write(" World") // " Hello World" (same line)
await writer.newLine() // " Hello World\n"
await writer.write("Next line") // " Hello World\n Next line"
Example Usage
let writer = MTLWriter()
// Write text with automatic indentation
await writer.writeLine("class Example {")
writer.indentation = writer.indentation.increment()
await writer.writeLine("var x: Int")
writer.indentation = writer.indentation.decrement()
await writer.writeLine("}")
let output = await writer.getContent()
// class Example {
// var x: Int
// }
Topics
Initializers
Creates a new writer with the specified initial indentation.
Instance Properties
The current indentation level.
Instance Methods
Clears the buffer and resets the writer state.
Returns the accumulated buffer contents.
Writes a newline character to the buffer.
Updates the current indentation level.
Writes text to the buffer with optional automatic indentation.
Writes a line of text followed by a newline.