Skip to content
No description, website, or topics provided.
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Colors.Net.Runner
Colors.Net
.gitignore
Colors.Net.nuspec
Colors.Net.sln
LICENSE
README.md

README.md

Colors.Net

Colors.Net

Installation:

PS> 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

Performance cost:

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

Has 0.1 msec overhead when compared to Console.WriteLine(). This is measured by the Runner project doing this 10,000 with an extra runtime of 1000 msec.

Credits:

The interface is inspired by colors.js.

Supported colors:

All colors in System.ConsoleColor enum.

You can’t perform that action at this time.