Table of Contents

Class ConsoleBuffer

Namespace
SharpConsoleUI.Drivers
Assembly
SharpConsoleUI.dll

Provides double-buffered console rendering with ANSI escape sequence support.

public class ConsoleBuffer
Inheritance
ConsoleBuffer
Inherited Members

Remarks

This class maintains a front buffer (what is currently displayed) and a back buffer (what will be rendered next). Only changed cells are written to the console, optimizing rendering performance by minimizing console output operations.

Constructors

ConsoleBuffer(int, int, ConsoleWindowSystemOptions?, object?)

Initializes a new instance of the ConsoleBuffer class with the specified dimensions.

public ConsoleBuffer(int width, int height, ConsoleWindowSystemOptions? options = null, object? consoleLock = null)

Parameters

width int

The width of the buffer in characters.

height int

The height of the buffer in lines.

options ConsoleWindowSystemOptions

Optional configuration options for buffer behavior.

consoleLock object

Optional shared lock for thread-safe Console I/O operations.

Properties

Diagnostics

Gets or sets the diagnostics system for capturing rendering metrics.

public RenderingDiagnostics? Diagnostics { get; set; }

Property Value

RenderingDiagnostics

Lock

Gets or sets a value indicating whether rendering is locked.

public bool Lock { get; set; }

Property Value

bool

true if rendering should be skipped during Render(); otherwise, false.

Remarks

This property is used to prevent rendering during buffer resizing operations.

Methods

AddContent(int, int, string)

Adds content to the back buffer at the specified position.

public void AddContent(int x, int y, string content)

Parameters

x int

The horizontal position (column) to start writing at.

y int

The vertical position (row) to write to.

content string

The content to write, which may include ANSI escape sequences for formatting.

Remarks

ANSI escape sequences in the content are parsed and associated with the appropriate cells, ensuring proper formatting when rendered. Content that extends beyond the buffer width is truncated.

Clear()

Clears the back buffer by resetting all cells to their default state.

public void Clear()

Remarks

All cells are reset to a space character with no ANSI formatting and marked as dirty.

GetDirtyCharacterCount()

Gets the count of dirty characters in the back buffer.

public int GetDirtyCharacterCount()

Returns

int

The number of characters marked as dirty.

IsCellDirty(int, int)

Checks if a specific cell is dirty (front buffer differs from back buffer). Pure double-buffering: compares buffer content, no state tracking.

public bool IsCellDirty(int x, int y)

Parameters

x int

The x coordinate of the cell

y int

The y coordinate of the cell

Returns

bool

True if the cell content differs between front and back buffers

Render()

Renders the back buffer to the console, updating only the changed portions.

public void Render()

Remarks

This method compares the back buffer with the front buffer and only writes cells that have changed, using cursor positioning to skip unchanged regions. After rendering, the front buffer is synchronized with the back buffer. The cursor is hidden during rendering to prevent flickering.