Table of Contents

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

width int
height int
defaultBackground Color?

Properties

Bounds

Gets the bounds of this buffer as a rectangle.

public LayoutRect Bounds { get; }

Property Value

LayoutRect

Diagnostics

Gets or sets the diagnostics system for capturing rendering metrics.

public RenderingDiagnostics? Diagnostics { get; set; }

Property Value

RenderingDiagnostics

Height

Gets the height of the buffer.

public int Height { get; }

Property Value

int

Size

Gets the size of this buffer.

public LayoutSize Size { get; }

Property Value

LayoutSize

Width

Gets the width of the buffer.

public int Width { get; }

Property Value

int

Methods

Clear(Color)

Clears the entire buffer with the specified background color.

public void Clear(Color background)

Parameters

background Color

ClearRect(LayoutRect, Color)

Clears a rectangular region with the specified background color.

public void ClearRect(LayoutRect rect, Color background)

Parameters

rect LayoutRect
background Color

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

source CharacterBuffer
sourceRect LayoutRect
destX int
destY int

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

rect LayoutRect
chars BoxChars
foreground Color
background Color

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

x int
y int
length int
character char
foreground Color
background Color

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

x int
y int
length int
character char
foreground Color
background Color

FillRect(LayoutRect, Color)

Fills a rectangle with spaces using the specified background color.

public void FillRect(LayoutRect rect, Color background)

Parameters

rect LayoutRect
background Color

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

rect LayoutRect
character char
foreground Color
background Color

GetCell(int, int)

Gets the cell at the specified position.

public Cell GetCell(int x, int y)

Parameters

x int
y int

Returns

Cell

GetChanges()

Gets all cells that have changed since the last commit.

public IEnumerable<CellChange> GetChanges()

Returns

IEnumerable<CellChange>

GetDirtyCells()

Gets all dirty cells regardless of whether they visually changed.

public IEnumerable<CellChange> GetDirtyCells()

Returns

IEnumerable<CellChange>

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

rect LayoutRect

Resize(int, int)

Resizes the buffer, preserving content where possible.

public void Resize(int newWidth, int newHeight)

Parameters

newWidth int
newHeight int

SetCell(int, int, Cell)

Sets a cell at the specified position.

public void SetCell(int x, int y, Cell cell)

Parameters

x int
y int
cell Cell

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

x int
y int
character char
foreground Color
background Color

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

defaultForeground Color

Default foreground color for optimization.

defaultBackground Color

Default background color for optimization.

Returns

List<string>

List of ANSI-formatted strings, one per row.

WriteCells(int, int, IEnumerable<Cell>)

Writes cells from an enumerable, typically from AnsiParser.

public void WriteCells(int x, int y, IEnumerable<Cell> cells)

Parameters

x int
y int
cells IEnumerable<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

x int
y int
cells IEnumerable<Cell>
clipRect LayoutRect

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

x int
y int
text string
foreground Color
background Color

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

x int
y int
text string
foreground Color
background Color
clipRect LayoutRect