Easier way to handle a Console Theme System I'm just curious is there an easier way to handle console colors rather than just doing something 
like the code below. The problem is the code bloat I'm experiencing when writing a theming system for my console application. It contains almost 200 lines of code, just to handle the chat theme system. Is there a way to minimalize this?

public static enum UserLevel
{
    Moderator, Broadcaster, Admin, Subscriber, Normal, Turbo
}

public static void WriteLine(UserLevel l, string user, string message)
{
    DateTime ts = new DateTime();
    string t = ts.ToLongTimeString();

    Console.BackgroundColor = ConsoleColor.Black;
    Console.Write("[" + t + "] ");
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write("&lt;");

    switch (l)
    {
        case UserLevel.Moderator:
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("[M] ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(user);
            break;
        case UserLevel.Broadcaster:
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("[B] ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(user);
            break;
        case UserLevel.Admin:
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("[A] ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(user);
            break;
        case UserLevel.Subscriber:
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("[S] ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(user);
            break;
        case UserLevel.Normal:
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(user);
            break;
        case UserLevel.Turbo:
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("[T] ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(user);
            break;
        case default:
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(user);
            break;
    }

    Console.ForegroundColor = ConsoleColor.White;
    Console.Write(user);
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write("&gt; ");
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write(message);
}
var keyword:

Use the var keyword wherever you can instead of declaring your variables explicitly. The compiler will determine the type and your code looks cleaner. Example:

var dateTime = new DateTime();
var time = dateTime.ToLongTimeString();


Small changes:


Variable names: l, ts or t are names that don't mean a thing, not for you, not for others. Use meaningful names such as userLevel or time.
DateTime: in the chat you want to show the time. I think you mean to use DateTime.Now instead of new DateTime(). The latter will just return 0:00:00 when you're calling the ToLongTimeString().
String.Format: don't concatenate hardcoded values and variables. Use the String.Format method. Following line:

Console.Write("[" + t + "] ");


will become:

Console.Write("[{0}] ", time);


This is easier to read and maintain.


Shorten your code:

Finally the point where you've been waiting for. Although there is a difference depending on the user-level, it is small. And certainly small enough to create a small method to avoid repetition. First, get the code based on the level and use the result to write to the console. This is done in following method:

private static string GetCodeFromUserLevel(UserLevel level)
{
    switch (level)
    {
        case UserLevel.Admin: return "[A] ";
        case UserLevel.Broadcaster: return "[B] ";
        case UserLevel.Moderator: return "[M] ";
        case UserLevel.Subscriber: return "[S] ";
        case UserLevel.Turbo: return "[T] ";
        default: return "";
    }
}


Now that you have this, you can use the result to write your message. This is done in the already existing message, but it looks cleaner now:

public static void WriteLine(UserLevel level, string userName, string message)
{
    var dateTime = DateTime.Now;
    var time = dateTime.ToLongTimeString();

    Console.BackgroundColor = ConsoleColor.Black;
    Console.Write("[{0}] ", time);
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write("&lt;");

    Console.ForegroundColor = ConsoleColor.Blue;
    Console.Write(GetCodeFromUserLevel(level));
    Console.ForegroundColor = ConsoleColor.White;
    Console.Write(userName);

    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write("&gt; ");
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine(message);
}


This code already looks better but you can take this a step further. You see three similar blocks with following structure:


Set a color
Write a part
Set a color again
Write a part again


This can be extracted in two ways: take step 1 and 2 and place it in a method or take step 1 to 4 and extract it. Here's the method when taking all steps in one method:

private static void WritePart(ConsoleColor startColor, ConsoleColor endColor, string partOne, string partTwo, bool newLine)
{
    Console.ForegroundColor = startColor;
    Console.Write(partOne);
    Console.ForegroundColor = endColor;
    Console.Write("{0}{1}", partTwo, newLine ? Environment.NewLine : "");
}


And here's the usage for the three blocks:

WritePart(ConsoleColor.White, ConsoleColor.Green, String.Format("[{0}] ", time), "&lt;", false);
WritePart(ConsoleColor.Blue, ConsoleColor.White, GetCodeFromUserLevel(level), userName, false);
WritePart(ConsoleColor.Green, ConsoleColor.White, "&gt; ", message, true);


I know that in your code you first set the BackgroundColor, you can add that line separatly before the above three lines.

Perhaps more logic is to only take step 1 and 2 and extract it in a method as it avoids repetition. Here's what it would look like:

private static void WritePart(ConsoleColor color, string value, bool newLine)
{
    Console.ForegroundColor = color;
    Console.Write("{0}{1}", value, newLine ? Environment.NewLine : "");
}


And the usage:

WritePart(ConsoleColor.White, String.Format("[{0}] ", time), false);
WritePart(ConsoleColor.Green, "&lt;", false);

WritePart(ConsoleColor.Blue, GetCodeFromUserLevel(level), false);
WritePart(ConsoleColor.White, userName, false);

WritePart(ConsoleColor.Green, "&gt; ", false);
WritePart(ConsoleColor.White, message, true);


It's what you prefer. Hope this helps!

Update:

Another tip on the method to get the code depending on the level. You have to options here make it shorter/cleaner.

Use following method. This is certainly not the cleanest solution but since the code is the first letter of the enum value you can use this:

private static string GetCodeFromUserLevel(UserLevel level)
{
    return level == UserLevel.Normal ? "" : String.Format("[{0}] ", level.ToString()[0]);
}


The other method is coming from this question on StackOverflow: Enum to String?. Check out the answer and you can apply this too.