Its not a good idea to use Console colors too much


With Whidbey (VS2005) managed Console based applications have got a boost with new additions to the System.Console namespace. You can now change the color, size, buffer-size, cursor-size, cursor-position, window-title directly from a managed application without PInvoke.


However, just because it can be done easily, doesn't mean that it should be used. If you are developing a console based game or something similar like a chat client you have the liberty to do this. Otherwise think twice (or thrice) before you play around with any of these settings. The reason is simple most people (like me) will get super annoyed if for some reason the console window suddenly jumps and resizes when I run a program.


If you think that doing some thing simpler like changing the text color (using Console.ForegroundColor) to draw attention is Ok, consider it carefully. It might just not work, could look horrible on some specific console setting or might convey the wrong meaning. I have listed below some common usage of changing these setting programmatically and why they might not work. Most programmers agree and understand that playing with console window size or buffer-size is not a good idea and do not use it so I have omitted them from here.


Changing text color using Console.ForegroundColor


Warning and Error Messages: Using different colors for output text, like yellow for warnings and red for error might be fine. You just need to ensure that you use the right set of colors as their meaning are deeply bound in the mind of the users. The following table typically works



  • Yellow: warning

  • Red: Error

  • Green: Pass, success messages

  • Cyan: Information

Do NOT use colors when its not absolutely required: Its best to avoid using colors when not absolutely required. In most cases it'll lead to trouble and rarely be of any benefit. See the following example where the prompt comes in yellow. Since this is a console based application it would have blocked until I entered the required inputs so drawing attention with yellow does not add any value. Since yellow is associated with warning, a first-time user might thing that something is wrong and he's required to rectify this by entering some server name.

Welcome to my tool
Server : MyServer
Username: Abhinaba

Do NOT use a dark color for text: People do change the background color of the console. I use Teal and DarkBlue often, so ensure that you do not choose one of the darker colors for the text as it might coincide with the background color of the console window and your text will be invisible. I once came accross a tool which used blue for text entered by the user. I had launched that application on a blue-background console. Since I was using the application for the first time, I had a hard time figuring out what was going on, as I couldn't see what I was typing.


The safe colors are Gray, Green, Cyan, Red, Magenta, Yellow and White. However sometimes even these colors in combination with background colors cause eye-sore as in

Some error message!!

Do restore the original color: Even if you use any of the above colors remember to switch the color back to the original color. Use some thing like

public void ShowError(string message)
{
ConsoleColor orgCol = Console.ForegroundColor;
Console.ForegroundColor= ConsoleColor.Red;
Console.WriteLine(message);
Console.ForegroundColor = orgCol; // restore color
}

This might look trivial, but can be a big source of annoyance. Like your application gets an exception, the exception-handler sets color to red, shows the error message and the application exits. Now whatever I type in that console window is in Red!!!


Using Console.BackgroundColor


Do NOT change the background color: Console.BackgroundColor = ... is bad.  The reason being that the color is changed only for subsequent text and it looks horrible as below

Text before color change
Text after I changed the background col to cyan

Even though very few people intentionally do this, it does creep into application. Sometime back in some error message code I saw this.

Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error!!!");

This went undetected in test, until someone opened it in a Teal window and this is what came up

Some error message!!

Playing with cursor


CursorVisible: You might want to hide the cursor when showing some output using Console.CursorVisible = false. Just remember to revert it back to the same visibility state as it originally was.


CursorPosition: Jumping the cursor around in the screen is not a main-stream scenario and most console application don't use it. However, I have seen some tools like build-systems use this to show subsquent outputs in the same line. Consider the following code

int currPos = Console.CursorTop; // get the current pos 
for (int i = 0; i < 30; i++)
{
Console.WriteLine("Counting {0}", i);
Console.CursorTop = currPos; // Go back to the same line
System.Threading.Thread.Sleep(200);
}

This shows the counter in the same line. This might be required in your application. However, in some cases its over-done and you can simply live without it...


Most programmers are used to UI guidelines. Unfortunately most of these guidelines ingnore command line interface and are silent on them...

Comments (9)
  1. zzz says:

    How do you make the cmd prompt background have alpha blend/glassy effect in Vista?

  2. Andrew says:

    Ah yes, just like msbuild. Very annoying as I have white background with black text.

  3. taras says:

    hello, my friend,

    could you pls. suggest..

    Given that the evil of colouring the console is unavoidable, is there any official way to set colours by using just information passed to Console.WriteXXX()? Some sort of old good escape sequences?

    Thank you

  4. taras, AFAIK there is no way to do this. You need to explicitely change colors…

  5. esteewhy says:

    .. A-ha, thank you. Then, i suppose, it makes sense to impl. a custom wrapper for .BackgroundColor, .ForegroundColor and .WriteLine(), to display at once a single stream of characters, with color info embedded ..

    By the way, i’ve noticed that you mostly against using colours in console applications because of the usability matters. Or, non-technical matters, in the first way, right? So, given that we have a clear strategy on using colours for console (despite, as you mentioned, most UI guidlines keep silent on this subj.), than it should not present any technical/compatibility/interoperability issues, if using System.Console properly?

    By the way, i’ve got somewhat related question: what are the differences among Console.WriteLine(), just writing "n" or Environment.NewLine value ? Or, in general, what control characters are "ok" to use with the console? Of course, i’m primarily concerned with the context of .Net framework. Or is it transparent to such issues?

  6. I’d suggest write your own wrapper in a static class with methods like Utility.WriteError, Utility.WriteWarning. In the wrapper methods get the current color, change the color, write text and switch back to the original color.

    However to support usability scenario you should read in the color from the config file and support a command line switch /nocolor in which you disable using any color. This should keep everyone happy.

    Using Console.WriteLine will be the best option….

  7. &lt;to get all the values as an array see here &gt; Sometimes you need to do something in code and you

  8. jbfraser says:

    If you use colors, you should store the original settings and make sure to implement a cancel key handler where you set things back. Otherwise ctrl-c will leave things ugly:

    http://www.codeneverwritten.com/2006/10/ctrl-c-and-net-console-application.html

  9. Zaneo says:

    In .Net 4.5 at least, you can use Console.ResetColor() to revert back to the default scheme. Not sure how far this extends back in versions.

Comments are closed.

Skip to main content