Language: EN

csharp-spectre-console

Enhance Your C# Console Applications with Spectre.Console

Spectre.Console is a C# library that allows you to create advanced and customizable user interfaces in the command console.

User interfaces in the console have come a long way since the days of simple text lines. Spectre.Console is an impressive library that allows us to create rich and dynamic user interfaces in the console.

With Spectre.Console, you can add colors, tables, bar charts, trees, text boxes, and much more to your console applications. This enables us to create a better user experience in the command console and … well… they look absolutely delightful ❤️

Features of Spectre.Console,

  • Text colors and styles: Complete customization of text with colors, bold, italic, underline, and more.
  • Tables: Create tables with support for alignment and advanced styles.
  • Bar charts: Visualize data in horizontal and vertical bar charts.
  • Text boxes: Frame text with custom borders.
  • Trees: Hierarchical representation of data.
  • Interactive prompts: Interactive user inputs with validation.
  • Progress and spinners: Progress indicators and animated spinners.

Spectre.Console is open source, and all its code and documentation are available in the project repository on GitHub - Spectre.Console.

Installing Spectre.Console

To get started using Spectre.Console in your .NET project, you can add the corresponding NuGet package:

Install-Package Spectre.Console
Install-Package Spectre.Console.Cli

You can also install it using the NuGet Package Manager in Visual Studio.

How to Use Spectre.Console

Below are several examples illustrating how to use Spectre.Console to create advanced user interfaces in the console.

Styled Text

This example shows how to apply different styles to text in the console.

using Spectre.Console;

class Program
{
    static void Main(string[] args)
    {
        AnsiConsole.Markup("[bold red]Hello[/], [underline blue]world[/]!");
        AnsiConsole.WriteLine();
    }
}

spectre-console-helloworld

Creating Tables

In this example, we demonstrate how to create and display a table in the console.

using Spectre.Console;

class Program
{
    static void Main(string[] args)
    {
        var table = new Table();
        
        table.AddColumn("Id");
        table.AddColumn("Name");
        table.AddColumn("Age");
        
        table.AddRow("1", "John", "25");
        table.AddRow("2", "Maria", "30");
        table.AddRow("3", "Luis", "28");
        
        AnsiConsole.Write(table);
    }
}

spectre-console-tables

Bar Charts

This example shows how to create and display a horizontal bar chart.

using Spectre.Console;

class Program
{
    static void Main(string[] args)
    {
        var chart = new BarChart()
            .Width(60)
            .Label("[green bold underline]Bar Chart[/]")
            .AddItem("C#", 12, Color.Yellow)
            .AddItem("Java", 8, Color.Green)
            .AddItem("Python", 14, Color.Blue);
        
        AnsiConsole.Write(chart);
    }
}

spectre-console-bar-chart

Text Boxes

This example shows how to create and display a text box with a custom border.

using Spectre.Console;

class Program
{
    static void Main(string[] args)
    {
        var panel = new Panel("This is a text box with a custom border.")
        {
            Border = BoxBorder.Rounded,
            Padding = new Padding(1, 1, 1, 1),
            BorderStyle = new Style(Color.Red)
        };
        
        AnsiConsole.Write(panel);
    }
}

spectre-console-text-box

Progress Indicators

This example shows how to create and display a progress indicator in the console.

using Spectre.Console;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        AnsiConsole.Progress()
            .Start(ctx =>
            {
                var task = ctx.AddTask("[green]Progress[/]");
                
                while (!task.IsFinished)
                {
                    task.Increment(1);
                    Thread.Sleep(100);
                }
            });
    }
}

spectre-console-progress-bar