Table of Contents

Class FormattedText

Namespace
InteractiveReadLine.Formatting
Assembly
InteractiveReadLine.dll

Represents a formatted string, where each character has a potentially unique foreground and background color assigned to it.

public class FormattedText : IEquatable<FormattedText>
Inheritance
FormattedText
Implements
Inherited Members

Constructors

FormattedText(FormattedChar)

Creates formatted text containing one character and its colors.

public FormattedText(FormattedChar c)

Parameters

c FormattedChar

The character used to create the text.

FormattedText(string, ConsoleColor?, ConsoleColor?)

Creates formatted text whose characters all have the specified foreground and background colors. Either color can be null to use the display default.

public FormattedText(string text, ConsoleColor? foreground = null, ConsoleColor? background = null)

Parameters

text string

The text.

foreground ConsoleColor?

The foreground color, or null to use the display default.

background ConsoleColor?

The background color, or null to use the display default.

Exceptions

ArgumentNullException

The text is null.

Properties

Background

Gets the background color array of the text

public ConsoleColor?[] Background { get; }

Property Value

ConsoleColor?[]

Foreground

Gets the foreground color array of the text

public ConsoleColor?[] Foreground { get; }

Property Value

ConsoleColor?[]

this[int]

Gets the character and its formatting at position i in the text

public FormattedChar this[int index] { get; }

Parameters

index int

Property Value

FormattedChar

Length

Gets the length of the text

public int Length { get; }

Property Value

int

Text

Gets the characters in the text as a string without any formatting

public string Text { get; }

Property Value

string

Methods

Equals(FormattedText?)

Determines whether this instance and another FormattedText instance contain identical characters and identical per-character foreground and background colors.

public bool Equals(FormattedText? other)

Parameters

other FormattedText

Returns

bool

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

GetHashCode()

Gets a hash code derived from the characters of the text, ignoring the colors.

public override int GetHashCode()

Returns

int

Remarks

The color arrays are mutable and publicly accessible, so including them would let an instance's hash code change while the instance is in a hash-based collection. Hashing only the text keeps the hash code stable for the lifetime of the instance and satisfies the contract because equal instances contain the same text. Instances that differ only by color can have the same hash code.

SetBackground(ConsoleColor?)

Sets the background color for every character in the text

public void SetBackground(ConsoleColor? color)

Parameters

color ConsoleColor?

SetForeground(ConsoleColor?)

Sets the foreground color for every character in the text

public void SetForeground(ConsoleColor? color)

Parameters

color ConsoleColor?

SplitByFormatting()

Splits the formatted text object into an array of FormattedText objects, in which each element of the array consists of text where every character has the same foreground and background colors.

public FormattedText[] SplitByFormatting()

Returns

FormattedText[]

Operators

operator +(FormattedText, FormattedText)

Joins two pieces of formatted text, preserving the colors each character already carried.

public static FormattedText operator +(FormattedText lhs, FormattedText rhs)

Parameters

lhs FormattedText
rhs FormattedText

Returns

FormattedText

implicit operator FormattedText(FormattedChar)

Converts a single formatted character to formatted text of length one.

public static implicit operator FormattedText(FormattedChar c)

Parameters

c FormattedChar

Returns

FormattedText

implicit operator FormattedText(string)

Converts a plain string to formatted text which uses the display's default colors.

public static implicit operator FormattedText(string s)

Parameters

s string

Returns

FormattedText