Class AnsiConsoleHelper
- Namespace
- SharpConsoleUI.Helpers
- Assembly
- SharpConsoleUI.dll
Provides helper methods for working with ANSI escape sequences and Spectre.Console markup in console applications.
public static class AnsiConsoleHelper
- Inheritance
-
AnsiConsoleHelper
- Inherited Members
Remarks
This class offers utilities for converting between Spectre.Console markup and raw ANSI sequences, measuring visible string lengths, extracting substrings from ANSI-formatted text, and creating capture consoles for rendering operations.
Methods
AnsiEmptySpace(int, Color)
Creates a string of empty spaces with the specified background color as ANSI-formatted output.
public static string AnsiEmptySpace(int width, Color backgroundColor)
Parameters
widthintThe number of space characters to create.
backgroundColorColorThe background color to apply to the spaces.
Returns
- string
An ANSI-formatted string containing the specified number of spaces with the background color applied.
ConvertSpectreMarkupToAnsi(string, int?, int?, bool, Color?, Color?)
Converts Spectre.Console markup text to ANSI escape sequence formatted strings.
public static List<string> ConvertSpectreMarkupToAnsi(string markup, int? width, int? height, bool overflow, Color? backgroundColor, Color? foregroundColor)
Parameters
markupstringThe Spectre.Console markup text to convert.
widthint?The optional maximum width for the output.
heightint?The optional maximum height for the output.
overflowboolIf
true, escapes markup and allows text to wrap; iffalse, truncates to fit.backgroundColorColor?The optional background color to apply.
foregroundColorColor?The optional foreground color to apply.
Returns
ConvertSpectreRenderableToAnsi(IRenderable, int?, int?, Color)
Converts a Spectre.Console Spectre.Console.Rendering.IRenderable object to ANSI escape sequence formatted strings.
public static List<string> ConvertSpectreRenderableToAnsi(IRenderable renderable, int? width, int? height, Color backgroundColor)
Parameters
renderableIRenderableThe Spectre.Console renderable object to convert.
widthint?The optional maximum width for the output.
heightint?The optional maximum height for the output.
backgroundColorColorThe background color to use for padding.
Returns
CreateCaptureConsole(TextWriter, int?, int?)
Creates an Spectre.Console.IAnsiConsole instance that captures output to the specified TextWriter.
public static IAnsiConsole CreateCaptureConsole(TextWriter writer, int? width, int? height)
Parameters
writerTextWriterThe text writer to capture console output to.
widthint?The optional width of the console.
heightint?The optional height of the console.
Returns
- IAnsiConsole
An Spectre.Console.IAnsiConsole configured for capturing output.
EscapeInvalidMarkupTags(string)
Escapes invalid Spectre.Console markup tags in the input string by doubling the brackets.
public static string EscapeInvalidMarkupTags(string input)
Parameters
inputstringThe input string that may contain invalid markup tags.
Returns
- string
A string with invalid markup tags escaped.
Remarks
Valid tags are preserved as-is, while standalone brackets or invalid tag content are escaped by doubling them (e.g., "[" becomes "[[").
EscapeSpectreMarkup(string)
Extension method that escapes invalid Spectre.Console markup in a string.
public static string EscapeSpectreMarkup(this string input)
Parameters
inputstringThe input string to escape.
Returns
- string
A string with invalid markup tags escaped.
FindFirstContentPosition(string)
Finds the position of the first visible content character in an ANSI-formatted string, skipping over ANSI escape sequences
public static int FindFirstContentPosition(string ansiString)
Parameters
ansiStringstringString with ANSI escape sequences
Returns
- int
Index of first visible content character, or 0 if none found
ParseAnsiTags(string, int?, int?, bool, string?, string?)
Parses custom ANSI-style tags in the input string and converts them to actual ANSI escape sequences.
public static List<string> ParseAnsiTags(string input, int? width, int? height, bool wrap, string? backgroundColor = null, string? foregroundColor = null)
Parameters
inputstringThe input string containing custom tags (e.g., [bold], [fg red]).
widthint?The optional maximum width for line wrapping.
heightint?The optional maximum number of lines to return.
wrapboolIf
true, wraps text at the specified width.backgroundColorstringThe optional background color name (e.g., "red", "blue").
foregroundColorstringThe optional foreground color name (e.g., "white", "yellow").
Returns
SetAnsiCursorPosition(int, int)
Generates an ANSI escape sequence to set the cursor position.
public static string SetAnsiCursorPosition(int left, int top)
Parameters
Returns
- string
An ANSI escape sequence string that moves the cursor to the specified position.
StripAnsiStringLength(string)
Calculates the visible length of a string by removing ANSI escape sequences.
public static int StripAnsiStringLength(string input)
Parameters
inputstringThe string potentially containing ANSI escape sequences.
Returns
- int
The length of the visible (non-escape-sequence) characters.
StripSpectreLength(string)
Calculates the visible length of text by stripping Spectre.Console markup tags
public static int StripSpectreLength(string text)
Parameters
textstringText that may contain markup tags
Returns
- int
Length of visible text
SubstringAnsi(string, int, int)
Extracts a substring from an ANSI-encoded string, preserving ANSI escape sequences
public static string SubstringAnsi(string input, int startIndex, int length)
Parameters
inputstringThe ANSI-encoded string
startIndexintThe position to start extraction (refers to visible characters, not including escape sequences)
lengthintThe number of visible characters to extract
Returns
- string
The extracted substring with all relevant ANSI escape sequences preserved
SubstringAnsiWithPadding(string, int, int, Color)
Extracts a substring from an ANSI-encoded string and pads to the requested length if shorter.
public static string SubstringAnsiWithPadding(string input, int startIndex, int length, Color backgroundColor)
Parameters
inputstringThe ANSI-encoded string
startIndexintThe position to start extraction (refers to visible characters)
lengthintThe desired number of visible characters
backgroundColorColorBackground color for padding spaces
Returns
- string
The extracted substring padded to exactly the specified length
TruncateAnsiString(string, int)
Truncates an ANSI-formatted string to a maximum visible length while preserving escape sequences.
public static string TruncateAnsiString(string input, int maxVisibleLength)
Parameters
inputstringThe ANSI-formatted string to truncate.
maxVisibleLengthintThe maximum number of visible characters to keep.
Returns
- string
A truncated string with ANSI escape sequences preserved and properly terminated.
TruncateSpectre(string, int)
Truncates a Spectre.Console markup string to a maximum visible length while preserving valid markup tags.
public static string TruncateSpectre(string inputStr, int maxLength)
Parameters
inputStrstringThe Spectre.Console markup string to truncate.
maxLengthintThe maximum number of visible characters to keep.
Returns
- string
A truncated string with markup tags preserved and properly closed.