2.5 KiB

Title: Markup Order: 3 Hidden: False

In Spectre.Console there's a class called Markup that allows you to output rich text to the console.

AnsiConsole.Render(new Markup("[bold yellow]Hello[/] [red]World![/]"));

Which should output something similar to the image below. Note that the actual appearance might vary depending on your terminal.

The Markup class implements IRenderable which means that you can use this in tables, grids, and panels. Most classes that support rendering of IRenderable also have overloads for rendering rich text.

var table = new Table();
table.AddColumn(new TableColumn(new Markup("[yellow]Foo[/]")));
table.AddColumn(new TableColumn("[blue]Bar[/]"));

Convenience methods

There is also convenience methods on AnsiConsole that can be used to write markup text to the console without instantiating a new Markup instance.

AnsiConsole.Markup("[underline green]Hello[/] ");
AnsiConsole.MarkupLine("[bold]World[/]");

Escaping format characters

To output a [ you use [[, and to output a ] you use ]].

AnsiConsole.Markup("[[Hello]] "); // [Hello]
AnsiConsole.Markup("[red][[World]][/]"); // [World]

Setting background color

You can set the background color in markup by prefixing the color with on.

[bold yellow on blue]Hello[/]
[default on blue]World[/]

Colors

For a list of colors, see the Colors section.

Styles

Note that what styles that can be used is defined by the system or your terminal software, and may not appear as they should.

bold Bold text
dim Dim or faint text
italic Italic text
underline Underlined text
invert Swaps the foreground and background colors
conceal Hides the text
slowblink Makes text blink slowly
rapidblink Makes text blink
strikethrough Shows text with a horizontal line through the center