[C#]Spectre.Console

2023-02-28 16:24:08 浏览数 (1)

Spectre.Console.NET/C# 平台非常优秀的控制台应用程序 UI 框架库,提供非常多开箱可用且非常好看的 UI 组件。官网地址:https://spectreconsole.net/

使用非常简单,只需要通过 Nuget 安装 Spectre.Console.Cli 拓展包即可。

代码语言:javascript复制
Spectre.Console 和 Spectre.Console.Cli 区别

Spectre.Console 是不包含 args 命令行参数解析的,但作为一个 tools 工具对于解析 args 参数显得非常重要,所以推荐安装 Spectre.Console.Cli

下面是 Spectre.ConsoleUI 组件 预览图:

下面官方实例,是为了自己抄代码:

Borders

代码语言:javascript复制
using Spectre.Console;
using Spectre.Console.Rendering;

namespace Borders;

public static class Program
{
    public static void Main()
    {
        // Render panel borders
        HorizontalRule("PANEL BORDERS");
        PanelBorders();

        // Render table borders
        HorizontalRule("TABLE BORDERS");
        TableBorders();
    }

    private static void PanelBorders()
    {
        static IRenderable CreatePanel(string name, BoxBorder border)
        {
            return new Panel($"This is a panel withnthe [yellow]{name}[/] border.")
                .Header($" [blue]{name}[/] ", Justify.Center)
                .Border(border)
                .BorderStyle(Style.Parse("grey"));
        }

        var items = new[]
        {
                CreatePanel("Ascii", BoxBorder.Ascii),
                CreatePanel("Square", BoxBorder.Square),
                CreatePanel("Rounded", BoxBorder.Rounded),
                CreatePanel("Heavy", BoxBorder.Heavy),
                CreatePanel("Double", BoxBorder.Double),
                CreatePanel("None", BoxBorder.None),
            };

        AnsiConsole.Write(
            new Padder(
                new Columns(items).PadRight(2),
                new Padding(2, 0, 0, 0)));
    }

    private static void TableBorders()
    {
        static IRenderable CreateTable(string name, TableBorder border)
        {
            var table = new Table().Border(border);
            table.AddColumn("[yellow]Header 1[/]", c => c.Footer("[grey]Footer 1[/]"));
            table.AddColumn("[yellow]Header 2[/]", col => col.Footer("[grey]Footer 2[/]").RightAligned());
            table.AddRow("Cell", "Cell");
            table.AddRow("Cell", "Cell");

            return new Panel(table)
                .Header($" [blue]{name}[/] ", Justify.Center)
                .NoBorder();
        }

        var items = new[]
        {
                CreateTable("Ascii", TableBorder.Ascii),
                CreateTable("Ascii2", TableBorder.Ascii2),
                CreateTable("AsciiDoubleHead", TableBorder.AsciiDoubleHead),
                CreateTable("Horizontal", TableBorder.Horizontal),
                CreateTable("Simple", TableBorder.Simple),
                CreateTable("SimpleHeavy", TableBorder.SimpleHeavy),
                CreateTable("Minimal", TableBorder.Minimal),
                CreateTable("MinimalHeavyHead", TableBorder.MinimalHeavyHead),
                CreateTable("MinimalDoubleHead", TableBorder.MinimalDoubleHead),
                CreateTable("Square", TableBorder.Square),
                CreateTable("Rounded", TableBorder.Rounded),
                CreateTable("Heavy", TableBorder.Heavy),
                CreateTable("HeavyEdge", TableBorder.HeavyEdge),
                CreateTable("HeavyHead", TableBorder.HeavyHead),
                CreateTable("Double", TableBorder.Double),
                CreateTable("DoubleEdge", TableBorder.DoubleEdge),
                CreateTable("Markdown", TableBorder.Markdown),
            };

        AnsiConsole.Write(new Columns(items).Collapse());
    }

    private static void HorizontalRule(string title)
    {
        AnsiConsole.WriteLine();
        AnsiConsole.Write(new Rule($"[white bold]{title}[/]").RuleStyle("grey").LeftJustified());
        AnsiConsole.WriteLine();
    }
}

0 人点赞