Table of Contents

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

width int

The number of space characters to create.

backgroundColor Color

The 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

markup string

The Spectre.Console markup text to convert.

width int?

The optional maximum width for the output.

height int?

The optional maximum height for the output.

overflow bool

If true, escapes markup and allows text to wrap; if false, truncates to fit.

backgroundColor Color?

The optional background color to apply.

foregroundColor Color?

The optional foreground color to apply.

Returns

List<string>

A list of ANSI-formatted strings, one per line.

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

renderable IRenderable

The Spectre.Console renderable object to convert.

width int?

The optional maximum width for the output.

height int?

The optional maximum height for the output.

backgroundColor Color

The background color to use for padding.

Returns

List<string>

A list of ANSI-formatted strings, one per line, padded to the specified width.

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

writer TextWriter

The text writer to capture console output to.

width int?

The optional width of the console.

height int?

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

input string

The 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

input string

The 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

ansiString string

String 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

input string

The input string containing custom tags (e.g., [bold], [fg red]).

width int?

The optional maximum width for line wrapping.

height int?

The optional maximum number of lines to return.

wrap bool

If true, wraps text at the specified width.

backgroundColor string

The optional background color name (e.g., "red", "blue").

foregroundColor string

The optional foreground color name (e.g., "white", "yellow").

Returns

List<string>

A list of ANSI-formatted strings, one per line.

SetAnsiCursorPosition(int, int)

Generates an ANSI escape sequence to set the cursor position.

public static string SetAnsiCursorPosition(int left, int top)

Parameters

left int

The column position (0-based).

top int

The row position (0-based).

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

input string

The 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

text string

Text 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

input string

The ANSI-encoded string

startIndex int

The position to start extraction (refers to visible characters, not including escape sequences)

length int

The 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

input string

The ANSI-encoded string

startIndex int

The position to start extraction (refers to visible characters)

length int

The desired number of visible characters

backgroundColor Color

Background 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

input string

The ANSI-formatted string to truncate.

maxVisibleLength int

The 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

inputStr string

The Spectre.Console markup string to truncate.

maxLength int

The maximum number of visible characters to keep.

Returns

string

A truncated string with markup tags preserved and properly closed.