Spectre.Console

repository·main·Indexed 11 days ago

https://github.com/spectreconsole/spectre.console

A .NET library for building cross-platform console applications with advanced UI elements such as tables, grids, and panels. It features a BBCode-inspired markup language for text styling, support for various color depths, and a static AnsiConsole API. The ecosystem includes extensions like Spectre.Console.ImageSharp for Canvas widgets, Spectre.Console.Json for JSON visualization, and Spectre.Console.Testing for intercepting and verifying console output.

Tokens
3.3K
Snippets
11
Records
19
Agent score
94%

What's inside Spectre.Console

  1. Overview of Spectre.Console features

    main

    Spectre.Console is a .NET library designed to create beautiful, cross-platform console applications. It is inspired by the Python library Rich and provides several high-level UI capabilities:

    • Layout & Structure: Supports tables, grids, and panels.
    • Markup Language: Uses a Rich-inspired markup language for styling.
    • Text Styling: Supports common SGR parameters including bold, dim, italic, underline, strikethrough, and blinking text.
    • Color Support: Supports 3, 4, 8, and 24-bit colors. The library automatically detects terminal capabilities and downgrades colors if the terminal does not support higher bit depths.
  2. Use Markup for rich text styling

    main

    Spectre.Console uses a BBCode-inspired markup language to apply styles like bold, color, and underlines. Styles are defined within square brackets [] and can be closed with [/].

    // Basic markup
    AnsiConsole.Markup("[underline red]Hello[/] World!");
    
    // Using MarkupLine for convenience
    AnsiConsole.MarkupLine("[bold]World[/]");
    
    // Using hex or RGB colors
    AnsiConsole.Markup("[#ff0000]Bar[/] ");
    AnsiConsole.Markup("[rgb(255,0,0)]Baz[/] ");
  3. Run Spectre.Console examples

    main

    You can explore the capabilities of Spectre.Console by installing the dotnet-example global tool and running the provided examples.

    # Restore and list examples
    > dotnet tool restore
    > dotnet example
    
    # Run a specific example (e.g., tables)
    > dotnet example tables
  4. Use Spectre.Console.Testing for unit testing

    main

    The Spectre.Console.Testing library is a .NET utility designed to simplify writing unit tests for applications built with Spectre.Console. It provides tools to intercept and verify console output and user interactions during testing.

    For detailed implementation instructions and advanced usage guides, refer to the official project website at https://spectreconsole.net.

  5. Create and render tables

    main

    Use the Table class to display tabular data in the terminal. Spectre.Console automatically adjusts column widths to fit the content. You can add columns, rows, and even nest other IRenderable objects (like Markup, Panel, or other Table instances) within cells. To display the table, pass the instance to AnsiConsole.Render().

    // テーブルの作成
    var table = new Table();
    
    // 列の追加
    table.AddColumn("Foo");
    table.AddColumn(new TableColumn("Bar").Centered());
    
    // 行の追加
    table.AddRow("Baz", "[green]Qux[/]");
    table.AddRow(new Markup("[blue]Corgi[/]"), new Panel("Waldo"));
    
    // コンソールにテーブルの描画
    AnsiConsole.Render(table);