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
cFormattedCharThe 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
textstringThe text.
foregroundConsoleColor?The foreground color, or null to use the display default.
backgroundConsoleColor?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
indexint
Property Value
Length
Gets the length of the text
public int Length { get; }
Property Value
Text
Gets the characters in the text as a string without any formatting
public string Text { get; }
Property Value
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
otherFormattedText
Returns
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
GetHashCode()
Gets a hash code derived from the characters of the text, ignoring the colors.
public override int GetHashCode()
Returns
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
colorConsoleColor?
SetForeground(ConsoleColor?)
Sets the foreground color for every character in the text
public void SetForeground(ConsoleColor? color)
Parameters
colorConsoleColor?
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
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
lhsFormattedTextrhsFormattedText
Returns
implicit operator FormattedText(FormattedChar)
Converts a single formatted character to formatted text of length one.
public static implicit operator FormattedText(FormattedChar c)
Parameters
Returns
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
sstring