Table of Contents

Class AnsiSink

Namespace
ItTiger.TigerCli.Terminal
Assembly
ItTiger.TigerCli.dll
An ICliRenderSink that writes styled output as ANSI SGR escape sequences to a TextWriter. Unlike ItTiger.TigerCli.Terminal.ConsoleSink — which degrades ANSI 16–255 colours to the nearest ConsoleColorAnsiSink renders the full 0–255 palette faithfully (see ItTiger.TigerCli.Terminal.AnsiSgr for the mapping rules).

Style handling is deterministic and diffed: an escape sequence is emitted only when the foreground, background, or text decorations change; all changed attributes coalesce into a single sequence; and a null colour channel resolves to the ANSI default (39/49) rather than "leave as-is". Decoration flags emit attribute on/off codes (bold 1/22, italic 3/23, underline 4/24) diffed against the previous style. A reset (ItTiger.TigerCli.Terminal.AnsiSgr.Reset) — which also clears decorations — is emitted before each newline and on flush whenever a style is active, so a styled background never bleeds past the line.

This is TigerCli's terminal renderer, so it is terminal-bounded by default: it reports the terminal's width and height to the measure pass exactly as ItTiger.TigerCli.Terminal.ConsoleSink does, and structured output wraps in the layout rather than relying on the terminal's own auto-wrap. Pass Buffer when the writer is a string, file, or capture stream, so the render stays content-driven and identical on every machine.

public sealed class AnsiSink : ICliRenderSink
Inheritance
AnsiSink
Implements
Inherited Members

Constructors

AnsiSink(TextWriter, bool, bool, CliSinkTarget)

Creates an ANSI sink. When emitHyperlinks is true, text runs carrying a HyperlinkTarget are wrapped in OSC 8 hyperlink sequences (the visible text is always written regardless). Defaults to false so existing direct callers (and tests) emit no hyperlink sequences unless they opt in; ItTiger.TigerCli.Terminal.ConsoleSinkFactory sets it from HyperlinkMode.
public AnsiSink(TextWriter writer, bool emitHyperlinks = false, bool emitTerminalControls = true, CliSinkTarget target = CliSinkTarget.Terminal)

Parameters

writer TextWriter
The destination writer.
emitHyperlinks bool
Whether to emit OSC 8 hyperlink sequences.
emitTerminalControls bool
Whether to emit terminal control sequences such as the window title.
target CliSinkTarget
What the sink writes to, which decides the layout bounds it reports. Defaults to Terminal: a plain new AnsiSink(Console.Out) is a terminal renderer and wraps to the terminal's width without further wiring. Use Buffer for a string, file, or capture writer.

Exceptions

ArgumentNullException
writer is null.

Properties

MaxHeight

Hard height constraint reported to grid measurement, or null when unbounded.
public int? MaxHeight { get; }

Property Value

int?

MaxWidth

Hard width constraint reported to grid measurement, or null when unbounded.
public int? MaxWidth { get; }

Property Value

int?

SoftMaxHeight

Soft height constraint reported to grid measurement, or null when unbounded.
public int? SoftMaxHeight { get; }

Property Value

int?

SoftMaxWidth

Soft width constraint reported to grid measurement, or null when unbounded.
public int? SoftMaxWidth { get; }

Property Value

int?

Remarks

Read on each access, like ItTiger.TigerCli.Terminal.ConsoleSink, so a resized terminal is honoured by the next measure pass.

Target

What this sink writes to; see CliSinkTarget.
public CliSinkTarget Target { get; }

Property Value

CliSinkTarget

Methods

Flush()

Resets active ANSI state and flushes the underlying writer.
public void Flush()

NewLine()

Resets active ANSI state and writes a line terminator.
public void NewLine()

Reset()

Closes any active hyperlink and resets active ANSI styling.
public void Reset()

SetWindowTitle(string)

Writes and flushes an ANSI window-title control sequence when terminal controls are enabled.
public void SetWindowTitle(string title)

Parameters

title string
The window title to set.

Write(CliTextSegment)

Writes a styled text segment using ANSI sequences where required.
public void Write(CliTextSegment segment)

Parameters

segment CliTextSegment
The styled text segment to write.