Class CharacterBuffer
- Namespace
- SharpConsoleUI.Layout
- Assembly
- SharpConsoleUI.dll
A 2D buffer of character cells for rendering. Supports double-buffering for efficient diff-based output.
public class CharacterBuffer
- Inheritance
-
CharacterBuffer
- Inherited Members
Constructors
CharacterBuffer(int, int, Color?)
Creates a new character buffer with the specified dimensions.
public CharacterBuffer(int width, int height, Color? defaultBackground = null)
Parameters
Properties
Bounds
Gets the bounds of this buffer as a rectangle.
public LayoutRect Bounds { get; }
Property Value
Diagnostics
Gets or sets the diagnostics system for capturing rendering metrics.
public RenderingDiagnostics? Diagnostics { get; set; }
Property Value
Height
Gets the height of the buffer.
public int Height { get; }
Property Value
Size
Gets the size of this buffer.
public LayoutSize Size { get; }
Property Value
Width
Gets the width of the buffer.
public int Width { get; }
Property Value
Methods
Clear(Color)
Clears the entire buffer with the specified background color.
public void Clear(Color background)
Parameters
backgroundColor
ClearRect(LayoutRect, Color)
Clears a rectangular region with the specified background color.
public void ClearRect(LayoutRect rect, Color background)
Parameters
rectLayoutRectbackgroundColor
Commit()
Commits the current buffer state and clears dirty flags. Call this after rendering to prepare for the next frame.
public void Commit()
CopyFrom(CharacterBuffer, LayoutRect, int, int)
Copies a region from another buffer.
public void CopyFrom(CharacterBuffer source, LayoutRect sourceRect, int destX, int destY)
Parameters
sourceCharacterBuffersourceRectLayoutRectdestXintdestYint
CreateSnapshot()
Creates an immutable snapshot of the current buffer state.
public BufferSnapshot CreateSnapshot()
Returns
- BufferSnapshot
A BufferSnapshot containing a deep copy of all cells.
Remarks
This method performs a deep copy of all cells, making the snapshot independent of the original buffer. Useful for screenshots, recording, compositing, or capturing state for diagnostics.
DrawBox(LayoutRect, BoxChars, Color, Color)
Draws a box border.
public void DrawBox(LayoutRect rect, BoxChars chars, Color foreground, Color background)
Parameters
rectLayoutRectcharsBoxCharsforegroundColorbackgroundColor
DrawHorizontalLine(int, int, int, char, Color, Color)
Draws a horizontal line.
public void DrawHorizontalLine(int x, int y, int length, char character, Color foreground, Color background)
Parameters
DrawVerticalLine(int, int, int, char, Color, Color)
Draws a vertical line.
public void DrawVerticalLine(int x, int y, int length, char character, Color foreground, Color background)
Parameters
FillRect(LayoutRect, Color)
Fills a rectangle with spaces using the specified background color.
public void FillRect(LayoutRect rect, Color background)
Parameters
rectLayoutRectbackgroundColor
FillRect(LayoutRect, char, Color, Color)
Fills a rectangle with the specified character and colors.
public void FillRect(LayoutRect rect, char character, Color foreground, Color background)
Parameters
rectLayoutRectcharactercharforegroundColorbackgroundColor
GetCell(int, int)
Gets the cell at the specified position.
public Cell GetCell(int x, int y)
Parameters
Returns
GetChanges()
Gets all cells that have changed since the last commit.
public IEnumerable<CellChange> GetChanges()
Returns
GetDirtyCells()
Gets all dirty cells regardless of whether they visually changed.
public IEnumerable<CellChange> GetDirtyCells()
Returns
InvalidateAll()
Marks all cells as dirty, forcing a full redraw on next render.
public void InvalidateAll()
InvalidateRect(LayoutRect)
Marks a rectangular region as dirty.
public void InvalidateRect(LayoutRect rect)
Parameters
rectLayoutRect
Resize(int, int)
Resizes the buffer, preserving content where possible.
public void Resize(int newWidth, int newHeight)
Parameters
SetCell(int, int, Cell)
Sets a cell at the specified position.
public void SetCell(int x, int y, Cell cell)
Parameters
SetCell(int, int, char, Color, Color)
Sets a cell at the specified position.
public void SetCell(int x, int y, char character, Color foreground, Color background)
Parameters
ToLines(Color, Color)
Converts the buffer to a list of ANSI-formatted strings. Each line is a row of the buffer with ANSI escape codes for colors.
public List<string> ToLines(Color defaultForeground, Color defaultBackground)
Parameters
defaultForegroundColorDefault foreground color for optimization.
defaultBackgroundColorDefault background color for optimization.
Returns
WriteCells(int, int, IEnumerable<Cell>)
Writes cells from an enumerable, typically from AnsiParser.
public void WriteCells(int x, int y, IEnumerable<Cell> cells)
Parameters
xintyintcellsIEnumerable<Cell>
WriteCellsClipped(int, int, IEnumerable<Cell>, LayoutRect)
Writes cells clipped to a rectangle.
public void WriteCellsClipped(int x, int y, IEnumerable<Cell> cells, LayoutRect clipRect)
Parameters
xintyintcellsIEnumerable<Cell>clipRectLayoutRect
WriteString(int, int, string, Color, Color)
Writes a string at the specified position with the given colors.
public void WriteString(int x, int y, string text, Color foreground, Color background)
Parameters
WriteStringClipped(int, int, string, Color, Color, LayoutRect)
Writes a string at the specified position, clipped to the specified rectangle.
public void WriteStringClipped(int x, int y, string text, Color foreground, Color background, LayoutRect clipRect)
Parameters
xintyinttextstringforegroundColorbackgroundColorclipRectLayoutRect