Integrating with Spectre.Console - Tyrrrz/CliFx GitHub Wiki
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:
-
Install Spectre.Console NuGet package
-
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)
});
// ...
}
}
- 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![/]", "")
);