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
Extension Methods

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

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.

FillCells(int, int, int, char, Color, Color)

Fills a horizontal run of cells in the back buffer with the specified character and colors.

public void FillCells(int x, int y, int width, char character, Color fg, Color bg)

Parameters

x int

The starting horizontal position (column).

y int

The vertical position (row).

width int

The number of cells to fill.

character char

The character to fill with.

fg Color

The foreground color.

bg Color

The background color.

FillCells(int, int, int, Rune, Color, Color)

Fills a horizontal run of cells with the specified Rune and colors.

public void FillCells(int x, int y, int width, Rune character, Color fg, Color bg)

Parameters

x int
y int
width int
character Rune
fg Color
bg Color

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.

SetCellsFromBuffer(int, int, CharacterBuffer, int, int, int, Color)

Copies a horizontal strip of cells from a CharacterBuffer directly into the back buffer, bypassing ANSI string serialization and parsing entirely.

public void SetCellsFromBuffer(int destX, int destY, CharacterBuffer source, int srcX, int srcY, int width, Color fallbackBg)

Parameters

destX int

Destination X position in this buffer.

destY int

Destination Y position in this buffer.

source CharacterBuffer

The source CharacterBuffer to read cells from.

srcX int

Source X offset within the CharacterBuffer.

srcY int

Source Y (row) within the CharacterBuffer.

width int

Number of cells to copy.

fallbackBg Color

Background color to use for padding when source is out of bounds.

SetNarrowCell(int, int, char, Color, Color)

Sets a single cell in the back buffer with the specified character and colors.

public void SetNarrowCell(int x, int y, char character, Color fg, Color bg)

Parameters

x int

The horizontal position (column).

y int

The vertical position (row).

character char

The character to write.

fg Color

The foreground color.

bg Color

The background color.

SetNarrowCell(int, int, Rune, Color, Color)

Sets a narrow (width-1) cell in the back buffer with the specified Rune and colors. Clears IsWideContinuation and Combiners.

public void SetNarrowCell(int x, int y, Rune character, Color fg, Color bg)

Parameters

x int
y int
character Rune
fg Color
bg Color