Introducing Spectre.Console
Date Published: 19 October 2022
Spectre.Console is a dotnet library for making beautiful, more useful console applications. Here's a quick introduction to getting started with it.
If you'd rather watch a video, I've recorded a quick video for NimblePros' YouTube channel:
YouTube: Introducing Spectre.Console
Getting Started
The quickest way to get started with Spectre.Console is to add it to a new console application. You can follow these steps in a console window right now:
dotnet new console -n SpectreConsole
cd SpectreConsole
dotnet add package Spectre.Console
dotnet watch run
Now open up Program.cs in an editor and replace its contents with this:
using Spectre.Console;
Console.WriteLine("Hello, World!");
AnsiConsole.Markup("[underline red]Hello[/] [bold green]World[/]!");
Save the file and you should see output like this in your terminal window:
Notice that the second "Hello World" is now in Red and Green.
Running the Examples
Another cool tool that Spectre Console uses is a library called dotnet example, which can be used by any project to make it easier to demo. The tool looks in the "examples" or "samples" folder for any csproj or fsproj files and provides an easy way to list and run these from the console.
For Spectre Console, you just need to install the tool and then run it:
dotnet tool install -g dotnet-example
Then pull down the repo for Spectre Console and run the following in that repo's root folder:
dotnet tool restore
dotnet example
This will show you all of the examples, like this:
Here's what Spectre Console's Status update feature looks like:
Another nice feature that virtually every console application could benefit from is better exception styling:
Next steps
This is one of those utilities that everyone should know about and consider using in their console apps (at least the ones that aren't throwaway). If you have a standard template you use for console applications, you might want to add this library to it. If you don't, you might want to consider creating one.
If you have a favorite feature of Spectre.Console that I didn't demo here, please share it in the comments below.
Category - Browse all categories
About Ardalis
Software Architect
Steve is an experienced software architect and trainer, focusing on code quality and Domain-Driven Design with .NET.