Skip to content

Integrating with Spectre.Console

Alexey Golub edited this page Apr 24, 2021 · 4 revisions

Spectre.Console is a library that provides rich rendering capabilities for console applications by leveraging ANSI escape sequences supported by modern terminals.

To integrate it with CliFx:

  1. Install Spectre.Console NuGet package

  2. In your command, create an instance of IAnsiConsole bound to the console instance provided by CliFx command:

[Command]
public class TestCommand : ICommand
{
    public ValueTask ExecuteAsync(IConsole console)
    {
        var ansiConsole = AnsiConsole.Create(new AnsiConsoleSettings
        {
            Ansi = AnsiSupport.Detect,
            ColorSystem = ColorSystemSupport.Detect,
            Out = new AnsiConsoleOutput(console.Output)
        });

        // ...
    }
}
  1. Use the API exposed by ansiConsole to render formatted content:
ansiConsole.MarkupLine("[bold yellow on red]Hello[/] [underline]world[/]!");

ansiConsole.Render(new Table()
    .Border(TableBorder.Square)
    .BorderColor(Color.Red)
    .AddColumn(new TableColumn("[u]CDE[/]").Footer("EDC").Centered())
    .AddColumn(new TableColumn("[u]FED[/]").Footer("DEF"))
    .AddColumn(new TableColumn("[u]IHG[/]").Footer("GHI"))
    .AddRow("Hello", "[red]World![/]", "")
    .AddRow("[blue]Bonjour[/]", "[white]le[/]", "[red]monde![/]")
    .AddRow("[blue]Hej[/]", "[yellow]Världen![/]", "")
);

screenshot

Clone this wiki locally