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
- Extension Methods
-
BufferGradientExtensions.StippleFill(CharacterBuffer, LayoutRect, double, Color, Color, LayoutRect?)BufferGraphicsExtensions.DrawCircle(CharacterBuffer, int, int, int, char, Color, Color, LayoutRect?)
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
FillGradient(LayoutRect, ColorGradient, GradientDirection)
Fills a rectangle with a gradient applied to cell background colors. Existing characters and foreground colors are preserved.
public void FillGradient(LayoutRect rect, ColorGradient gradient, GradientDirection direction)
Parameters
rectLayoutRectThe rectangle to fill.
gradientColorGradientThe color gradient to apply.
directionGradientDirectionThe direction of the gradient.
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
FillRect(LayoutRect, Rune, Color, Color)
Fills a rectangle with the specified Rune character and colors.
public void FillRect(LayoutRect rect, Rune character, Color foreground, Color background)
Parameters
rectLayoutRectcharacterRuneforegroundColorbackgroundColor
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, preserving all attributes including decorations. Both Foreground and Background participate in alpha blending.
public void SetCell(int x, int y, Cell cell)
Parameters
SetNarrowCell(int, int, char, Color, Color)
Sets a narrow (width-1) cell at the specified position. Clears IsWideContinuation, Combiners, and Decorations. Do NOT use for cells from MarkupParser.Parse — use SetCell(Cell) instead to preserve flags.
public void SetNarrowCell(int x, int y, char character, Color foreground, Color background)
Parameters
SetNarrowCell(int, int, Rune, Color, Color)
Sets a narrow (width-1) cell at the specified position with a Rune character.
Clears IsWideContinuation, Combiners, and Decorations.
Do NOT use for cells from MarkupParser.Parse — use SetCell(Cell) instead to preserve flags.
Both foreground and background participate in alpha blending.
public void SetNarrowCell(int x, int y, Rune 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. Continuation cells (from MarkupParser) are written as-is without additional wide-char processing.
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. Wide characters that straddle clip boundaries are replaced with a space.
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. Wide characters occupy 2 columns with a continuation cell for the right half.
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. Wide characters that straddle clip boundaries are replaced with a space.
public void WriteStringClipped(int x, int y, string text, Color foreground, Color background, LayoutRect clipRect)