Skip to content

Writing Console Apps with DotNet in 2022 - Part 1 - Crayon

Photo by <a href="https://unsplash.com/@tejasexplorer?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Veronica Lorine</a>
Photo by Veronica Lorine

This blog is part of a series of blog posts looking at .Net packages to help .Net Developers create console apps.

Preface

A few months ago, I saw Evan You demo Vite. One thing in the demo I noticed were the options displayed in Coloured Text in the Console Window.

Initially dismissing this as a feature unique to Mac Os. I wanted to know if this was possible in a .Net Console App.

Crayon

Crayon is a .NET Package written by Manuel Riezebosch.

Its available on Github - https://github.com/riezebosch/crayon

and on Nuget - https://www.nuget.org/packages/Crayon

Getting Started

With Crayon installed, You have methods to style the Text.

So you can put

using static Crayon.Output;

Console.WriteLine("");
Console.Write(Red("All I see is Red !!!!"));
Console.WriteLine("");

Which displays

Red text example

Now this is possible with Colour Commands already built into the Framework, but Crayon allows you to pass RGB Values, extending the colour palette.

using static Crayon.Output;

Console.WriteLine("");
Console.Write(Bold(Red("Red")));
Console.Write(" and ");
Console.Write(Rgb(255, 255, 100).Text("Yellow"));
Console.Write(Bold(" and "));
Console.Write(Bold(Rgb(255,192,203 ).Text("Pink")));
Console.Write(" and ");
Console.WriteLine(Green().Text("Green"));

Console.Write(Rgb(191, 64, 191).Text("Purple"));
Console.Write(" and ");
Console.Write(Rgb(255, 191, 0).Text("Orange"));
Console.Write(Bold(" and "));
Console.WriteLine(Blue().Text("Blue"));
Console.Write("I can sing a ");
Console.Write(Red("R"));
Console.Write(Rgb(255, 191, 0).Text("a"));
Console.Write(Rgb(255, 255, 100).Text("i"));
Console.Write(Green("n"));
Console.Write(Blue("b"));
Console.Write(Rgb(75, 01, 130).Text("o"));
Console.WriteLine(Rgb(238, 130, 238).Text("w"));

Which displays

Colour text example 1

Comes in Colours Everywhere

There is even a built in Rainbow Class

var rainbow = new Rainbow(0.5);
string testStr = "  Somewhere over the rainbow, way up high\n\r  " +
                 "There's a land that I heard of once in a lullaby\n\r\n\r";
foreach (char c in testStr)
{
    Console.Write(string.IsNullOrWhiteSpace(c.ToString()) 
            ? c.ToString() : rainbow.Next().Bold().Text(c.ToString()));
}

All of this runs perfectly on Windows Terminal

Colour text Window Terminal example

and also via WSL, here is an example of the above code on Ubuntu.

Colour text Ubuntu example

So If you want a more Colourful Console output, Then check out Crayon and if you like it Please consider giving the Repo a Github star.

Posted in Writing Console Apps with DotNet in 2022