Skip to content

Writing Console Apps with DotNet in 2022 - Part 3 - Pastel

Photo by <a href="https://unsplash.com/@pawel_czerwinski?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Pawel Czerwinski</a>
Photo by Pawel Czerwinski

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

Preface

After blogging about Crayon. I noticed a line in the Github Readme referencing Pastel. As Crayon is inspired by Pastel I thought I would take a look at it.

Pastel

Pastel is a .Net Utiliy Package for colorizing console output, Written by Gabriel Bider

Its available on Github - https://github.com/silkfire/Pastel

and on Nuget https://www.nuget.org/packages/Pastel

Getting Started

The main command is the Pastel() method. Its written as an extension method of string.

using System.Drawing;
using Pastel;

var message = "Hello, World!";

Console.WriteLine(message.Pastel(Color.FromArgb(200,0,200)));

You can use the System.Drawing.Color and utilise the its enum values

using System.Drawing;
using Pastel;

var message = "I am Text in Pale Turquoise";

Console.WriteLine(message.Pastel(Color.PaleTurquoise));

Pastel can also colour the background, using the PastelBg() method.

using System.Drawing;
using Pastel;

var message = " Foo ";

for (int i = 0; i < 255; i+=5)
{
    Console.Write(message.Pastel(Color.White).PastelBg(Color.FromArgb(128, i, 128)));
    Console.Write(message.Pastel(Color.White).PastelBg(Color.FromArgb(i, i, 128)));
    Console.Write(message.Pastel(Color.White).PastelBg(Color.FromArgb(128, i, i)));
}

Pastel only alters the output string, there is no need to manipulate or extend the built-in System.Console class.

For those of you who need to adhere to accessibility rules with your console app. Pastel also supports the https://no-color.org initiative. Where the colour output has been requested to turn off, Pastel will honour the request.

You can also stop Pastel by using the ConsoleExtensions Disable() and Enable() methods

using System.Drawing;
using Pastel;


Console.WriteLine("I could be brown,".Pastel(Color.Brown));
Console.WriteLine("I could be blue,".Pastel(Color.Blue));
Console.WriteLine("I could be violet sky,".Pastel(Color.Violet));

ConsoleExtensions.Disable();

Console.WriteLine("I could be brown,".Pastel(Color.Brown));
Console.WriteLine("I could be blue,".Pastel(Color.Blue));
Console.WriteLine("I could be violet sky,".Pastel(Color.Violet));

ConsoleExtensions.Enable();

Console.WriteLine("I could be brown,".Pastel(Color.Brown));
Console.WriteLine("I could be blue,".Pastel(Color.Blue));
Console.WriteLine("I could be violet sky,".Pastel(Color.Violet));

So If you want Colourful Console output with a slightly more simple api, Then check out Pastel and if you like it, Please conside giving the Repo a Github star.