Ever wanted to write awesome command line interface applications in .NET and use colors, lines, tables, progress bar? By seeing all of the great tools / scripts (written in different languages) executing and giving you great visual information, can the same be done in .NET?
Of course we can always try to use Console class in System namespace, but we still have challenge with defining lines, characters, progress bars, colors, etc. That is usually something we need to implement on our own. Do we need to go from scratch?
Spectre.Console to the rescue
Spectre.Console is a .NET 5/.NET Standard 2.0 library that makes it easier to create beautiful, cross platform, console applications, written by Patrik Svensson, creator or Cake Build free and open source cross-platform build automation system with a C# DSL for tasks. In short, it makes your CLI app shine.
This picture says it all:
Installation is very simple, just add nuget package to the project:
dotnet add package Spectre.Console
Give me examples to try it out!
Spectre.Console comes with pre-built examples folder. Everything you need to start building beautiful CLI apps. If you want to play around samples, you can open the solution/folder in your editor of choice, restore and then execute.
If you just want to test them out, without running them in a tool, but seeing results or source code, you can take advantage of another project from the same author, dotnet-example. It works as global tool, you can invoke them anywhere. What you need to do, is to invoke the tool in root directory.
With that you can easily check examples in command line. But what if we want to see the source code? With flag -s you can easily see source code of that specific part:
If you want to use it in your own project, create examples or samples folder inside your code. More here. For more information to be shown in console, you can fill out settings in csproj file like shown here.
What do I get, if I add the library?
Patrik Svensson is actively adding new features, currently supporting:
- simple markup to define rich text with colors, styles and links
- usable prompt with focus on fluent code execution with features like validation and secrets insertion
- widgets like tables, Panels with different border styles, charts, Figlets, Rule, calendar and much more
- Progress to show information about long running tasks in the console
- Status to show information about long running tasks in the console
- Trees showing trees in the console
All this and much more to come in one library.
I really got excited about this library. Spectre.Console is organized, examples folders has a lot of useful and quick samples on how to leverage different functionality. With dotnet example tool this is a seamless experience helping you get from 0 to hero. Code is easy to read and nicely formatted with useful examples.
I do a lot of workshops / demos and setup projects in CLI, especially with Kubernetes, and this library is helping me beautify my samples.
If you are .NET developer and you work with CLI at some point, this library is a good to have in your toolbox.