Click here to Skip to main content
Click here to Skip to main content

Tagged as

Console Coloring with ConsolePlus

, 19 Nov 2013 GPL3
Rate this:
Please Sign up or sign in to vote.
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

License

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

Share

About the Author

John_Leitch
Software Developer AutoSec Tools
United States United States
No Biography provided

Comments and Discussions

 
QuestionException thrown ... PinmemberJohannes Distler6-Mar-14 2:36 
Questionmissing a second parameter Pinmemberfredatcodeproject19-Nov-13 2:51 
AnswerRe: missing a second parameter PinprofessionalJohn_Leitch19-Nov-13 8:55 
GeneralRe: missing a second parameter Pinmemberfredatcodeproject20-Nov-13 5:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150128.1 | Last Updated 19 Nov 2013
Article Copyright 2013 by John_Leitch
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid