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
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
AddContent(int, int, string)
Adds content to the back buffer at the specified position.
public void AddContent(int x, int y, string content)
Parameters
xintThe horizontal position (column) to start writing at.
yintThe vertical position (row) to write to.
contentstringThe 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
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.