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
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
textstringThe text to truncate (may contain markup)
maxWidthintMaximum visible character width
ellipsisstringEllipsis string to append when truncated (default "...")
cacheTextMeasurementCacheOptional 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
prefixstringFixed prefix that never truncates (e.g., tree indentation)
contentstringThe content to truncate if needed
suffixstringFixed suffix that never truncates (e.g., expand indicator)
totalMaxWidthintMaximum total width for prefix + content + suffix
cacheTextMeasurementCacheCache for measuring text widths
ellipsisstringEllipsis 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
prefixstringcontentstringtotalMaxWidthintcacheTextMeasurementCacheellipsisstring
Returns
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
contentstringsuffixstringtotalMaxWidthintcacheTextMeasurementCacheellipsisstring