2

GitHub - galassie/fs-spectre: 👻💻 Spectre.Console with F# style.

 1 year ago
source link: https://github.com/galassie/fs-spectre
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

FsSpectre

Spectre.Console with F# style.

FsSpectre is a small library that extends Spectre.Console and allow to write beautiful console applications in a declarative and more F#-friendly way. It leverages Computation Expressions to create the widgets in a declarative style.

Add package

If you want to add this package to your project, execute the following command:

dotnet add package FsSpectre

Build on your machine

If you want to build this library on your machine, execute the following commands:

git clone [email protected]:galassie/fs-spectre.git
cd fs-spectre
dotnet build

Examples

Table

With C# + Spectre.Console:

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.Write(table);

With F# + FsSpectre:

table {
    column_text ""
    column (tableColumn { header "Feature"; centerd })
    row_text [| "Baz"; "[green]Qux[/]" |]
    row [| markup { text "[blue]Corgi[/]" }; panel { content "Waldo" } |]
} |> AnsiConsole.Write

Bar Chart

With C# + Spectre.Console:

AnsiConsole.Write(new BarChart()
    .Width(60)
    .Label("[green bold underline]Number of fruits[/]")
    .CenterLabel()
    .AddItem("Apple", 12, Color.Yellow)
    .AddItem("Orange", 54, Color.Green)
    .AddItem("Banana", 33, Color.Red));

With F# + FsSpectre:

barChart {
    width 60
    label "[green bold underline]Number of fruits[/]"
    center_label
    item "Apple" 12 Color.Yellow
    item "Oranges" 54 Color.Green
    item "Bananas" 33 Color.Red
} |> AnsiConsole.Write

Showcase

To see an example, execute the Showcase.fsx with the following command (you need to build the library first):

dotnet fsi Showcase.fsx

Showcase

Alternatives

If you don't like this style of using Spectre.Console with Computation Expressions, check out these amazing projects:

Contributing

Code contributions are more than welcome! heart_eyes_cat

Please commit any pull requests against the main branch.
If you find any issue, please report it!

License

This project is licensed under The MIT License (MIT).

Author: Enrico Galassi


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK