Skip to content
ConsoleMonster
Class

RichText

ConsoleMonster.Text

Namespace
ConsoleMonster.Text
Assembly
ConsoleMonster
Implements
IWidget
Modifiers
sealed

Multicolour text: a sequence of spans, each literal text (never interpreted as markup) or markup, with a RichStyle. Rich text is accepted wherever text appears (table cells and headers, card and record fields, tree labels, titles, lane names, log lines, chart labels) and is itself a widget, so it can be written or placed in a layout directly. Colours and theme roles are resolved per colour tier when rendered; without colour the text stays readable.

Inheritance

  • object
  • RichText

Remarks

Literal spans are safe for data: brackets and control characters are never interpreted. Build rich text with RichTextBuilder, RichText.FromMarkup, RichText.FromPieces (Write-Color style parallel arrays) or RichText.Gradient. RichText.ToString returns the plain text.

Usage

Explore 88 API references to this type

This type appears in these public API surfaces even when no hand-authored example is attached directly to the page.

Accepted by parameters 60

Constructors

public RichText(IEnumerable<RichSegment> segments) #

Creates rich text from segments.

Parameters

segments System.Collections.Generic.IEnumerable<ConsoleMonster.Text.RichSegment> required
The segments, in order.

Methods

public RichText Append(string? text, RichStyle style = default) #

Returns a copy with literal text appended.

Parameters

text string? required
The text.
style ConsoleMonster.Styling.RichStyle = default optional
Its style.

Returns

RichText The new rich text.
public RichText AppendMarkup(string? markup) #

Returns a copy with markup appended.

Parameters

markup string? required
The markup.

Returns

RichText The new rich text.
public static RichText Concat(params RichText?[] parts) #

Joins rich texts.

Parameters

parts ConsoleMonster.Text.RichText?[] required
The parts.

Returns

RichText The joined text.
public static RichText From(object? value) #

Converts a value: rich text passes through, anything else becomes literal text (its string form), so data is never interpreted as markup.

Parameters

value object? required
The value.

Returns

RichText The rich text.
public static RichText FromMarkup(string? markup, RichStyle style = default) #

Rich text from Spectre-compatible markup ([bold lime]Ready[/] in [muted]3s[/]).

Parameters

markup string? required
The markup.
style ConsoleMonster.Styling.RichStyle = default optional
A style around the whole markup.

Returns

RichText The rich text.

Exceptions

  • ArgumentException – The markup is not valid.
FromPieces 2 overloads
public static RichText FromPieces(IReadOnlyList<string?> texts, IReadOnlyList<RichColor>? colors = null, IReadOnlyList<RichColor>? backgrounds = null, RichStyle style = default) #

Rich text from parallel arrays, as Write-Color takes them: piece i gets colour i and background i; pieces beyond the colours get the first colour (and the first background), as Write-Color does.

Parameters

texts System.Collections.Generic.IReadOnlyList<string?> required
The pieces (literal).
colors System.Collections.Generic.IReadOnlyList<ConsoleMonster.Styling.RichColor>? = null optional
Foreground colours, or null.
backgrounds System.Collections.Generic.IReadOnlyList<ConsoleMonster.Styling.RichColor>? = null optional
Background colours, or null.
style ConsoleMonster.Styling.RichStyle = default optional
Decorations and a link applied to every piece.

Returns

RichText The rich text.
public static RichText FromPieces(IReadOnlyList<object?> pieces, IReadOnlyList<RichColor>? colors, IReadOnlyList<RichColor>? backgrounds, RichStyle style) #

Rich text from pieces as Write-ConsoleText takes them: strings (literal) or rich text (whose own colours win over the piece colours); piece i gets colour i and background i, and pieces beyond the colours get the first ones.

Parameters

pieces System.Collections.Generic.IReadOnlyList<object?> required
The pieces: strings, rich text, or values shown with their invariant text.
colors System.Collections.Generic.IReadOnlyList<ConsoleMonster.Styling.RichColor>? required
Foreground colours, or null.
backgrounds System.Collections.Generic.IReadOnlyList<ConsoleMonster.Styling.RichColor>? required
Background colours, or null.
style ConsoleMonster.Styling.RichStyle required
Decorations, a link and the fallback colour applied to every piece.

Returns

RichText The rich text.
public static RichText Gradient(string? text, RichColor from, RichColor to, RichStyle style = default, ITheme? theme = null) #

Literal text coloured as a gradient from one colour to another, per grapheme. Colours are 24-bit and are down-mapped per colour tier (on 16 colours the gradient becomes steps).

Parameters

text string? required
The text.
from ConsoleMonster.Styling.RichColor required
The first colour.
to ConsoleMonster.Styling.RichColor required
The last colour.
style ConsoleMonster.Styling.RichStyle = default optional
Decorations, background and link for every grapheme.
theme ConsoleMonster.Styling.ITheme? = null optional
The theme that resolves role colours, or null for the dark theme.

Returns

RichText The rich text.
public Size Measure(Constraints constraints, RenderContext context) #

Returns the size the widget wants within the constraints.

Parameters

constraints ConsoleMonster.Layout.Constraints required
The space offered.
context ConsoleMonster.Canvas.RenderContext required
Profile, theme and glyphs.

Returns

Size The desired size, never larger than the constraints.
public static RichText Plain(string? text, RichStyle style = default) #

Literal text with one style.

Parameters

text string? required
The text (shown as written).
style ConsoleMonster.Styling.RichStyle = default optional
The style.

Returns

RichText The rich text.
public void Render(Region region, RenderContext context) #

Renders into a region. The region may be smaller or larger than the measured size.

Parameters

region ConsoleMonster.Canvas.Region required
The target region.
context ConsoleMonster.Canvas.RenderContext required
Profile, theme and glyphs.
public List<RichText> Split(string separator) #

Splits the text at a separator, keeping each piece's styles. Markup segments are split on their text only when they contain no tags.

Parameters

separator string required
The separator.

Returns

List<RichText> The parts (one when the separator does not occur).
public string ToMarkup() #

The text as Spectre-compatible markup: literal spans escaped, styled spans wrapped in tags, tabs expanded to the next multiple of eight cells. Pass it to any API that takes markup.

Returns

string The markup.
public override string ToString() #

The plain text.

Returns

string The text without styling.
public StyledText ToStyledText(RenderContext context) #

The styled lines for a render context (theme and colour tier applied).

Parameters

context ConsoleMonster.Canvas.RenderContext required
The render context.

Returns

StyledText The styled text.
public RichText WithBaseStyle(RichStyle style) #

Returns a copy with a style layered under every span (span colours and decorations win).

Parameters

style ConsoleMonster.Styling.RichStyle required
The style.

Returns

RichText The new rich text.

Properties

public static RichText Empty { get; } #

Empty rich text.

public IReadOnlyList<RichSegment> Segments { get; } #

The segments, in order.

public HorizontalAlignment Alignment { get; set; } #

Alignment when written as a widget (default left).

public TextOverflow Overflow { get; set; } #

How the text fits when written as a widget (default wrap, keeping styles across lines).

public string PlainText { get; } #

The text without styling (markup spans stripped).

public int Width { get; } #

The display width of the widest line, in terminal cells.

Extension Methods

public static RichText Alignment(RichText richText, HorizontalAlignment value) #

Sets RichText.Alignment: Alignment when written as a widget (default left).

Parameters

richText ConsoleMonster.Text.RichText required
The instance.
value ConsoleMonster.Layout.HorizontalAlignment required
The value.

Returns

RichText The same instance.

Examples

// C# initializer or property    // C# fluent
richText.Alignment = value;      richText.Alignment(value);
public static RichText Overflow(RichText richText, TextOverflow value) #

Sets RichText.Overflow: How the text fits when written as a widget (default wrap, keeping styles across lines).

Parameters

richText ConsoleMonster.Text.RichText required
The instance.
value ConsoleMonster.Widgets.TextOverflow required
The value.

Returns

RichText The same instance.

Examples

// C# initializer or property    // C# fluent
richText.Overflow = value;       richText.Overflow(value);