Table of Contents

Class TextMeasurementCache

Namespace
SharpConsoleUI.Helpers
Assembly
SharpConsoleUI.dll

Caches expensive text measurement operations for improved rendering performance. Eliminates duplicate measurements during rendering cycles (40+ calls per frame in complex controls).

public class TextMeasurementCache
Inheritance
TextMeasurementCache
Inherited Members

Constructors

TextMeasurementCache(Func<string, int>)

Creates a new text measurement cache with the specified measurement function.

public TextMeasurementCache(Func<string, int> measurementFunc)

Parameters

measurementFunc Func<string, int>

Function to measure text width (e.g., AnsiConsoleHelper.StripSpectreLength)

Properties

CachedCount

Gets the number of cached entries.

public int CachedCount { get; }

Property Value

int

Methods

GetCachedLength(string)

Gets the cached length of the text, or measures and caches it if not already cached.

public int GetCachedLength(string text)

Parameters

text string

The text to measure

Returns

int

The visible character length of the text

GetCachedTotalLength(params string[])

Gets the total cached length of multiple text strings. Useful for measuring composite strings like "prefix + content + suffix".

public int GetCachedTotalLength(params string[] texts)

Parameters

texts string[]

Array of text strings to measure

Returns

int

Sum of the visible character lengths

InvalidateCache()

Invalidates the entire cache, forcing all subsequent measurements to be recalculated. Call this when the measurement function behavior changes or when you need to free memory.

public void InvalidateCache()

InvalidateCachedEntry(string)

Invalidates a specific cached entry.

public void InvalidateCachedEntry(string text)

Parameters

text string

The text whose cached measurement should be removed