Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Tip/Trick

Console Coloring with ConsolePlus

Rate me:
Please Sign up or sign in to vote.
4.70/5 (5 votes)
19 Nov 2013GPL31 min read 18.9K   215   11   4
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
C#
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
C#
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

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

Set Background Color Statement

Syntax

~|background-color~

Example

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

Reset Color Statement

Syntax

~R~

Example

C#
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
C#
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

C#
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

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer AutoSec Tools
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionException thrown ... Pin
Johannes Distler6-Mar-14 1:36
Johannes Distler6-Mar-14 1:36 
Questionmissing a second parameter Pin
fredatcodeproject19-Nov-13 1:51
professionalfredatcodeproject19-Nov-13 1:51 
AnswerRe: missing a second parameter Pin
JohnLeitch19-Nov-13 7:55
professionalJohnLeitch19-Nov-13 7:55 
GeneralRe: missing a second parameter Pin
fredatcodeproject20-Nov-13 4:23
professionalfredatcodeproject20-Nov-13 4:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.