Table of Contents

Class StructuredTextWriter

Namespace
Invex.StructuredText
Assembly
Invex.StructuredText.dll

An indentation-aware text writer for producing structured text such as YAML. Indentation is managed through disposable scopes returned by WriteSection(string?), so the nesting of the generating code mirrors the nesting of the generated output.

public sealed class StructuredTextWriter
Inheritance
object
StructuredTextWriter

Examples

var writer = new StructuredTextWriter();

using (writer.WriteSection("jobs:"))
using (writer.WriteSection("build:"))
    writer.WriteLine("runs-on: ubuntu-latest");

var yaml = writer.ToString();

Constructors

StructuredTextWriter(int)

An indentation-aware text writer for producing structured text such as YAML. Indentation is managed through disposable scopes returned by WriteSection(string?), so the nesting of the generating code mirrors the nesting of the generated output.

public StructuredTextWriter(int indentSize = 2)

Parameters

indentSize int

The number of spaces written per indent level. Defaults to 2.

Examples

var writer = new StructuredTextWriter();

using (writer.WriteSection("jobs:"))
using (writer.WriteSection("build:"))
    writer.WriteLine("runs-on: ubuntu-latest");

var yaml = writer.ToString();

Properties

Indent

The current indent level (number of nested sections), not the number of spaces.

public int Indent { get; }

Property Value

int

IndentSize

The number of spaces written per indent level.

public int IndentSize { get; set; }

Property Value

int

StringBuilder

The underlying buffer that accumulates the written text.

public StringBuilder StringBuilder { get; init; }

Property Value

StringBuilder

Version

A counter incremented by every Reset(int). Section scopes created before a reset compare against this value and become no-ops on dispose, so stale scopes cannot corrupt the indentation of content written after the reset.

public int Version { get; }

Property Value

int

Methods

Reset(int)

Clears the buffer, resets the indent level to zero, and invalidates any open section scopes by incrementing Version. Use this to reuse one writer for multiple documents.

public void Reset(int indentSize = 2)

Parameters

indentSize int

The indent size to use from this point on. Defaults to 2.

ToString()

Returns the accumulated text.

public override string ToString()

Returns

string

Write(string?, bool)

Appends text without a trailing newline.

public void Write(string? text = null, bool indent = true)

Parameters

text string

The text to append. When null, nothing is written.

indent bool

Whether to prefix the text with the current indentation. Pass false when continuing a line that has already been started.

WriteLine(string?)

Appends text as a full line at the current indent level, or a blank line when text is null.

public void WriteLine(string? text = null)

Parameters

text string

The text to append, or null to write an empty line.

WriteSection(string?)

Optionally writes a header line, then increases the indent level by one.

public IDisposable WriteSection(string? text = null)

Parameters

text string

An optional header line (e.g. "steps:") written at the current indent level before the indent is increased. When null or empty, only the indent changes.

Returns

IDisposable

A scope that restores the previous indent level when disposed. Dispose it (typically via using) to close the section.

Remarks

If Reset(int) is called while a scope is still open, disposing that scope does nothing — the scope is tied to the Version it was created under.