65.9K
CodeProject is changing. Read more.
Home

Console Coloring with ConsolePlus

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.70/5 (5 votes)

Nov 18, 2013

GPL3

1 min read

viewsIcon

19643

downloadIcon

219

ConsolePlus aims to build upon the existing System.Console class by offering a styling language for coloring console output. This enables developers to create applications with rich console output, while minimizing boilerplate code.

Introduction

ConsolePlus aims to build upon the existing System.Console class by offering a styling language for coloring console output. This enables developers to create applications with rich console output, while minimizing boilerplate code.

Coloring console output using the System.Console class is often quite cumbersome, requiring multiple statements just to output a single line. Consider the example shown in listing 1.

Listing 1. An example of console coloring using System.Console
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("Hello, world");
Console.ResetColor();

If you've worked with console applications, you've probably encountered code like this. The ConsolePlus equivalent is shown in listing 2.

Listing 2. An example of console coloring using ConsolePlus
Cli.WriteLine("~White~~|Blue~Hello, world~R~");

Both examples perform the same console operations, yielding the same output, but the second does so using a single statement, rather than four. How does this work? Read on.

Console Styling Language

ConsolePlus utilizes a small, domain-specific language named console styling language (CSL), which is embedded within strings. CSL only has three types of statements: set foreground color, set background color, and reset color. Statements are delimited by the tilde character (~), which can be escaped by doubling it up (~~).

Set Foreground Color Statement

Syntax

~foreground-color~

Example

Cli.WriteLine("~Blue~Foo");

Set Background Color Statement

Syntax

~|background-color~

Example

Cli.WriteLine("~|Yellow~Foo");

Reset Color Statement

Syntax

~R~

Example

Cli.WriteLine("~|Yellow~Foo~R~bar");

CSL and Format Strings

CSL is intended to play nicely with format strings. All ConsolePlus methods that perform string formatting do so before interpreting CSL, allowing for parameterization of colors.

Listing 3 shows an example of parameterized colors. If files.Length is greater than 99, the number will be printed in red, rather than white.

Listing 3. An example application that prints file count
static void WriteFileCount(FileInfo[] files)
{
    var c = files.Length < 100 ? ConsoleColor.White : ConsoleColor.Red;
    Cli.WriteLine("Files: ~{0}~{1}~R~", c, files.Length);
}

Complete Example

using Components.ConsolePlus;
using System;

namespace ConsolePlus
{
    class Program
    {
        // The old way
        static void DisplayMessage(ConsoleColor symbolColor, char symbol, 
            string message, params string[] arg)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write('[');
            Console.ForegroundColor = symbolColor;
            Console.Write(symbol);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("] ");
            Console.ResetColor();
            Console.WriteLine(message, arg);
        }

        // The new way
        static void DisplayMessage2(ConsoleColor symbolColor, char symbol, 
            string message, params string[] arg)
        {
            Cli.WriteLine("~White~[~{0}~{1}~White~]~R~ {2}", symbolColor, symbol,
                string.Format(message, arg));
        }

        static void Main(string[] args)
        {
            DisplayMessage(ConsoleColor.Green, '+', "Action succeeded!");
            DisplayMessage2(ConsoleColor.Green, '+', "Action succeeded!");   
        }
    }
}

History

  • 11/19/2013 - Fixed compatibility issues with .NET 3.5 and 4.0
  • 11/18/2013 - First version of this tip
Console Coloring with ConsolePlus - CodeProject