‹ Home
May 14, 2017 · software

Colors.Net

Colors.Net

Installation:

Install-Package Colors.Net  

Basic usage:

using Colors.Net;

ColoredConsole.WriteLine("Hello, World");  
ColoredConsole.Write("Hello, World");  

So it's pretty much the same surface as System.Console. However, to add color, all you have to do is either

using Colors.Net.StringColorExtensions;

ColoredConsole.WriteLine($"{"Hello".Red()}, world!");  

or

using static Colors.Net.StringStaticMethods;

ColoredConsole.WriteLine($"{Red("Hello")}, World");  

Since this is just manipulating strings, it's very easy to use which ever pattern you like. I personally like:

using Colors.Net.StringColorExtensions;

var @hello = "Hello".Red();

ColoredConsole.WriteLine($"{@hello}, World");  

and the result of course is the same in all 2 cases:

Hello, World!

Nested colors:

You can also use nested colors and they will work as you'd expect string concat to work. For example:

var @yellow = Yellow("I'm Yellow!");  
var @red = Red("I'm Red!");  
var @blue = "I'm Blue".Blue(); // just to show a different way

ColoredConsole  
    .WriteLine($@"{Green($"I'm green yet {@yellow}, {@red}, {@blue} and back to green.")}");

and this prints:

Nested colors

Fluent-like Api:

I like chaining calls that logically make sense together. Anyway, WriteLine() and Write() return an instance of IConsoleWriter so you can chain WriteLine() and Write() calls. It looks something like this:

ColoredConsole  
    .WriteLine($"{DarkCyan("Title:")} Welcome to My Cli")
    .WriteLine()
    .WriteLine($"{DarkCyan("Version:")} {Magenta("1.0.0")}");

and that prints:

Chaining

Credits:

The interface is inspired by colors.js.

Supported colors:

All colors in System.ConsoleColor enum.