We've all created a console application in the past, but know that generally they are difficult to build if you're trying to accomplish anything interactive and / or usable by non-technical people.
Spectre.Console is a library which tries to change this. It provides support for ANSI Console, which basically means you get access to a much richer looking set of capabilities than you might otherwise expect from a console application, such as text styles, blinking, multiple colours (up to 24 bit!) and importantly, graphics that can be used to create widgets. And Spectre comes with a host of these, including tables, trees, charts and animations.
Spectre.Console in F
As a .NET library, you can use Spectre straight off the bat in an F# console application - simply reference the NuGet package and off you go. As it's primarily designed with C# in mind, you'll notice that the API style doesn't necessary fit to idiomatic F# - you'll need to use ignore
a little more than you'll want to, but generally it works pretty well. And I could imagine someone creating a nice F# wrapper around this without too much difficulty!
A basic F# example
Here's a sample for creating a Table in Spectre.Console in F#:
let table = Table().Centered()
for column in [ "Name"; "Eye Colour"; "Hair Colour"; "Species"; "Homeworld" ] do
table.AddColumn column |> ignore
for person, species, homeworld in people do
table
.AddRow(
person.Name,
person.EyeColor,
person.HairColor,
species,
homeworld
)
|> ignore
Note the use of
ignore
- this is commonly used in F# when working with "Fluent" APIs designed for C#.
Which will create a table looking something like this:
With a little effort, you can embed markdown-style data to provide styling information:
You can find the code to generate the above table here, which uses standard F# data access libraries like F# Data etc.; Spectre Console isn't a walled garden and you should be able to use it with your normal data manipulation libraries etc.
Of course, this is just the beginning. Spectre does a lot of heavy lifting for you for things such as prompts and command line argument parsing, so you can use it for interactive applications.
Summary
Next time you want to create an application but don't feel the "cost" of creating a desktop is warranted and using a web application is not possible (or justified), you might do worse than try out Spectre.Console!
Until next time, have (fun _ -> ())
Isaac