PowerConsole extension to System.Console

.NET console extended

  • Share

Document Console IconMost of developers, at least all the ones I know mostly use Console to test their code as it is the quickest way to do it. Despite unit tests, console is a fastest way to test your code where you can easily output the results.

However if you have a lot of text in output you can get lost in results. One way to separate results is to apply formatting, but it is a boring thing to do adding dummy code just to format the output.

This is the reason why I created this wrapper class for System.Console. Main idea was to create extension methods but since it is a static class I could only create an new static class that wraps System.Console.

It is not a revolutionary thing but it helps a lot for displaying info, warning and error messages.

The project is hosted at GitHub https://github.com/dejanstojanovic/PowerConsole along with test console application for testing methods

Since I use console mainly for input and output, I mainly extended ReadLine and WriteLine methods of the console. I actually wrapper Console.Writeline and Console.WriteLine. It colorize the output with adding date and time of the message printed out.

Some of the things extending output are:

  • Colorizing the message and setting the default color back after printing out the message
  • Colorizing depending on message status
  • Adds date and time at the beginning of line
  • Sound alert for specific output color or message status
  • Printing out exception message by setting the colorizing and sound alert

Another thing beside writing messages is inserting values. For example when you need to insert something you need to use WriteLine and ReadLine to ask for a value. This is know wrapped in power console.

Some of the things extending input are:

  • Setting different color of the input depending on the passed color value or message status
  • Adds date and time at the beginning of line
  • Validation of input type with possible sound alert
  • Direct cast from input to desired type

Usage of the class is pretty easy. You do not event need to add using of a namespace, just adde refence to console application project. The console wrapper project will compile with System as a root namespace. The only thing you need to do is to add using of ConsoleExtended namespace. The following sample code adds some info message and asks for input of a bool value. It actually does the validation, alarming (sound and visual) and casting to desired value type and all that with just 3 lines of code.

using System;
namespace TestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            PowerConsole.BeepOnMessageStatus = MessageStatus.Error;
            PowerConsole.WriteLine("Hello", MessageStatus.Info);
            PowerConsole.ReadLine<bool>("Enter bool: ",ConsoleColor.Yellow);
        }
    }
}

    

The output is as following

Powerconsole

Since I recently started this mini project on GitHub, there is still lack of documentation and functionality, but check GitHub page occasionally to get updated.

References

  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus