Table of Contents

Class TextTruncationHelper

Namespace
SharpConsoleUI.Helpers
Assembly
SharpConsoleUI.dll

Helper class for truncating text with ellipsis, handling Spectre.Console markup correctly. Consolidates 85% similar truncation logic from 4+ locations.

public static class TextTruncationHelper
Inheritance
TextTruncationHelper
Inherited Members

Fields

MinContentCharsBeforeEllipsis

Minimum number of content characters to show before adding ellipsis. If space is too tight, shows content without ellipsis instead of "ab...".

public const int MinContentCharsBeforeEllipsis = 3

Field Value

int

Methods

Truncate(string, int, string, TextMeasurementCache?)

Truncates text to fit within maxWidth, adding ellipsis if truncated. Handles Spectre.Console markup correctly (e.g., "[red]text[/]").

public static string Truncate(string text, int maxWidth, string ellipsis = "...", TextMeasurementCache? cache = null)

Parameters

text string

The text to truncate (may contain markup)

maxWidth int

Maximum visible character width

ellipsis string

Ellipsis string to append when truncated (default "...")

cache TextMeasurementCache

Optional cache for measuring text width

Returns

string

Truncated text with ellipsis if needed

TruncateWithFixedParts(string, string, string, int, TextMeasurementCache, string)

Truncates text with fixed prefix and suffix parts. Only the content portion is truncated; prefix and suffix are preserved. Example: " ├─ " + "[red]VeryLongName[/]" + " [+]" truncated to fit totalMaxWidth.

public static string TruncateWithFixedParts(string prefix, string content, string suffix, int totalMaxWidth, TextMeasurementCache cache, string ellipsis = "...")

Parameters

prefix string

Fixed prefix that never truncates (e.g., tree indentation)

content string

The content to truncate if needed

suffix string

Fixed suffix that never truncates (e.g., expand indicator)

totalMaxWidth int

Maximum total width for prefix + content + suffix

cache TextMeasurementCache

Cache for measuring text widths

ellipsis string

Ellipsis string to append to truncated content (default "...")

Returns

string

Composite string: prefix + truncatedContent + suffix

TruncateWithPrefix(string, string, int, TextMeasurementCache, string)

Truncates text with a fixed prefix only. Convenience overload for TruncateWithFixedParts with empty suffix.

public static string TruncateWithPrefix(string prefix, string content, int totalMaxWidth, TextMeasurementCache cache, string ellipsis = "...")

Parameters

prefix string
content string
totalMaxWidth int
cache TextMeasurementCache
ellipsis string

Returns

string

TruncateWithSuffix(string, string, int, TextMeasurementCache, string)

Truncates text with a fixed suffix only. Convenience overload for TruncateWithFixedParts with empty prefix.

public static string TruncateWithSuffix(string content, string suffix, int totalMaxWidth, TextMeasurementCache cache, string ellipsis = "...")

Parameters

content string
suffix string
totalMaxWidth int
cache TextMeasurementCache
ellipsis string

Returns

string