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
widthintThe width of the buffer in characters.
heightintThe height of the buffer in lines.
optionsConsoleWindowSystemOptionsOptional configuration options for buffer behavior.
consoleLockobjectOptional 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
Lock
Gets or sets a value indicating whether rendering is locked.
public bool Lock { get; set; }
Property Value
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
xintThe starting horizontal position (column).
yintThe vertical position (row).
widthintThe number of cells to fill.
charactercharThe character to fill with.
fgColorThe foreground color.
bgColorThe 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
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
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
destXintDestination X position in this buffer.
destYintDestination Y position in this buffer.
sourceCharacterBufferThe source CharacterBuffer to read cells from.
srcXintSource X offset within the CharacterBuffer.
srcYintSource Y (row) within the CharacterBuffer.
widthintNumber of cells to copy.
fallbackBgColorBackground 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
xintThe horizontal position (column).
yintThe vertical position (row).
charactercharThe character to write.
fgColorThe foreground color.
bgColorThe 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)