Skip to content
ConsoleMonster

API Reference

Class

RichText

Namespace ConsoleMonster.Text
Assembly ConsoleMonster
Implements
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 55 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 27

Constructors

public RichText(IEnumerable<RichSegment> segments) #

Creates rich text from segments.

Parameters

segments System.Collections.Generic.IEnumerable<ConsoleMonster.Text.RichSegment> requiredposition: 0
The segments, in order.

Methods

public RichText Append(String? text, RichStyle style = default) #
Returns: RichText

Returns a copy with literal text appended.

Parameters

text System.String? requiredposition: 0
The text.
style ConsoleMonster.Styling.RichStyle = default optionalposition: 1
Its style.

Returns

The new rich text.

public RichText AppendMarkup(String? markup) #
Returns: RichText

Returns a copy with markup appended.

Parameters

markup System.String? requiredposition: 0
The markup.

Returns

The new rich text.

public static RichText Concat(params RichText?[] parts) #
Returns: RichText

Joins rich texts.

Parameters

parts ConsoleMonster.Text.RichText?[] requiredposition: 0
The parts.

Returns

The joined text.

public static RichText From(Object? value) #
Returns: RichText

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

Parameters

value System.Object? requiredposition: 0
The value.

Returns

The rich text.

public static RichText FromMarkup(String? markup, RichStyle style = default) #
Returns: RichText

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

Parameters

markup System.String? requiredposition: 0
The markup.
style ConsoleMonster.Styling.RichStyle = default optionalposition: 1
A style around the whole markup.

Returns

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) #
Returns: RichText

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<System.String?> requiredposition: 0
The pieces (literal).
colors System.Collections.Generic.IReadOnlyList<ConsoleMonster.Styling.RichColor>? = null optionalposition: 1
Foreground colours, or null.
backgrounds System.Collections.Generic.IReadOnlyList<ConsoleMonster.Styling.RichColor>? = null optionalposition: 2
Background colours, or null.
style ConsoleMonster.Styling.RichStyle = default optionalposition: 3
Decorations and a link applied to every piece.

Returns

The rich text.

public static RichText FromPieces(IReadOnlyList<Object?> pieces, IReadOnlyList<RichColor>? colors, IReadOnlyList<RichColor>? backgrounds, RichStyle style) #
Returns: RichText

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<System.Object?> requiredposition: 0
The pieces: strings, rich text, or values shown with their invariant text.
colors System.Collections.Generic.IReadOnlyList<ConsoleMonster.Styling.RichColor>? requiredposition: 1
Foreground colours, or null.
backgrounds System.Collections.Generic.IReadOnlyList<ConsoleMonster.Styling.RichColor>? requiredposition: 2
Background colours, or null.
style ConsoleMonster.Styling.RichStyle requiredposition: 3
Decorations, a link and the fallback colour applied to every piece.

Returns

The rich text.

public static RichText Gradient(String? text, RichColor from, RichColor to, RichStyle style = default, ITheme? theme = null) #
Returns: RichText

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 System.String? requiredposition: 0
The text.
from ConsoleMonster.Styling.RichColor requiredposition: 1
The first colour.
to ConsoleMonster.Styling.RichColor requiredposition: 2
The last colour.
style ConsoleMonster.Styling.RichStyle = default optionalposition: 3
Decorations, background and link for every grapheme.
theme ConsoleMonster.Styling.ITheme? = null optionalposition: 4
The theme that resolves role colours, or null for the dark theme.

Returns

The rich text.

public Size Measure(Constraints constraints, RenderContext context) #
Returns: Size

Returns the size the widget wants within the constraints.

Parameters

constraints ConsoleMonster.Layout.Constraints requiredposition: 0
The space offered.
context ConsoleMonster.Canvas.RenderContext requiredposition: 1
Profile, theme and glyphs.

Returns

The desired size, never larger than the constraints.

public static RichText Plain(String? text, RichStyle style = default) #
Returns: RichText

Literal text with one style.

Parameters

text System.String? requiredposition: 0
The text (shown as written).
style ConsoleMonster.Styling.RichStyle = default optionalposition: 1
The style.

Returns

The rich text.

public Void Render(Region region, RenderContext context) #
Returns: Void

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

Parameters

region ConsoleMonster.Canvas.Region requiredposition: 0
The target region.
context ConsoleMonster.Canvas.RenderContext requiredposition: 1
Profile, theme and glyphs.
public List<RichText> Split(String separator) #
Returns: List<RichText>

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 System.String requiredposition: 0
The separator.

Returns

The parts (one when the separator does not occur).

public String ToMarkup() #
Returns: String

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

The markup.

public override String ToString() #
Returns: String

The plain text.

Returns

The text without styling.

public StyledText ToStyledText(RenderContext context) #
Returns: StyledText

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

Parameters

context ConsoleMonster.Canvas.RenderContext requiredposition: 0
The render context.

Returns

The styled text.

public RichText WithBaseStyle(RichStyle style) #
Returns: RichText

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

Parameters

style ConsoleMonster.Styling.RichStyle requiredposition: 0
The style.

Returns

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 Int32 Width { get; } #

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

Extension Methods

public static RichText WithAlignment(RichText richText, HorizontalAlignment value) #
Returns: RichText

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

Parameters

richText ConsoleMonster.Text.RichText requiredposition: 0
The instance.
value ConsoleMonster.Layout.HorizontalAlignment requiredposition: 1
The value.

Returns

The same instance.

public static RichText WithOverflow(RichText richText, TextOverflow value) #
Returns: RichText

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

Parameters

richText ConsoleMonster.Text.RichText requiredposition: 0
The instance.
value ConsoleMonster.Widgets.TextOverflow requiredposition: 1
The value.

Returns

The same instance.