Skip to content

ahmelsayed/Colors.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages