ConsoleHelper class

Functions for comfortable input and output in console applications. Includes environment checks, a progress bar, cursor movement, and line wrapping output.

Console windows are mostly boring. And yet they can do more than just appending grey text. Starting with colours and cursor movement to certain positions, to an animated progress bar, to well-readable text layout, there are many possibilities. Checks for the current environment allow making good decisions about form and extent of output.

This ConsoleHelper class provides methods that are still missing in the .NET framework’s Console class. The example below describes its possibilities and can be used for your own exploration.

Compatibility: .NET Version 4.0 or newer

Example

The following sample code shows the diverse usages of ConsoleHelper:

public static int Main()
{
    // Reset unsupported character encoding for exotic languages to en-US
    ConsoleHelper.FixEncoding();
   
    // Show the message where the user can see it
    if (ConsoleHelper.IsInteractiveAndVisible)
    {
        // Console is visible, also use colour for this text
        ConsoleHelper.WriteLine("Hello console.", ConsoleColor.Red);
    }
    else
    {
        // Console is not visible, choose another output (non-interactive session,
        // no console window allocated, or output redirected)
        MessageBox.Show("Hello window.");
    }

    // Interaction only if it is possible
    if (!ConsoleHelper.IsInputRedirected)
    {
        Console.Write("Please enter your name: ");
        Console.ReadLine();
    }
   
    // Move cursor and clear line
    Console.Write("Your name is:");
    ConsoleHelper.MoveCursor(-3);
    Console.Write("needs more checking...");
    ConsoleHelper.ClearLine();   // Oh well, doesn’t matter anyway.
   
    // Progress bar only if the output is interactive and can be overwritten. Otherwise,
    // all intermediate frames end up in the file being redirected to, in some form.
    if (!ConsoleHelper.IsOutputRedirected)
    {
        // Activate progress bar and update it regularly
        ConsoleHelper.ProgressTitle = "Downloading";
        ConsoleHelper.ProgressTotal = 10;
        for (int i = 0; i <= 10; i++)
        {
            ConsoleHelper.ProgressValue = i;
            Thread.Sleep(500);
            // Warning and error state is displayed in colour (yellow/red instead of green)
            if (i >= 5)
            {
                ConsoleHelper.ProgressHasWarning = true;
            }
            if (i >= 8)
            {
                ConsoleHelper.ProgressHasError = true;
            }
        }
        // Remove progress bar again
        ConsoleHelper.ProgressTotal = 0;
    }
   
    // Show long text with proper word wrapping
    ConsoleHelper.WriteWrapped("This very long text must be wrapped at the right end of the console window. But that should not tear apart words just where the line is over but move the excess word to the next line entirely. This will regard the actual width of the window.");

    ConsoleHelper.WriteWrapped("This also works for tabular output like a listing of command line parameters:");

    // Line wrapping for tabular output
    ConsoleHelper.WriteWrapped("  /a    Just a short note.", true);
    ConsoleHelper.WriteWrapped("  /b    The text in the following lines is wrapped so that it continues under the last content column (that is this description). That is recognised by the last occurrence of two spaces.", true);
    ConsoleHelper.WriteWrapped("  /cde  Nothing important, really.", true);
   
    // Clear input buffer to not use any premature keystrokes
    ConsoleHelper.ClearKeyBuffer();
    // Confirmation message with timeout (15 seconds) and vanishing dots
    ConsoleHelper.Wait("Seen it all?", 15, true);
   
    // Simple wait for any input key (not Ctrl, Shift, NumLock, Mute, and so on)
    ConsoleHelper.Wait();
   
    // Prevent closing the console window after program end when running with the
    // debugger in Visual Studio. Without the debugger, Visual Studio will wait
    // already. Conforming things here.
    ConsoleHelper.WaitIfDebug();
   
    // Show a red alert message and terminate the process with an exit code (12)
    return ConsoleHelper.ExitError("All is lost!", 12);
}

Download

ConsoleHelper.cs22.9 KiBSource code of the ConsoleHelper class

Changes

2015Feb8
  • ExitError method writes to stderr instead of stdin
  • Added temporary text coloring support with the ConsoleColorScope (IDisposable) class
2015Feb2
  • Fixed line wrapping bug
  • Added custom control character support for partial text coloring

Licence and terms of use

Copying and distribution of this file, with or without modification, are permitted provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty. (GNU All-Permissive licence)

Statistic data

  • Created on 2013-11-08, updated on 2015-02-08.
  • Ca. 500 lines of code, estimated development costs: 500 - 2 000 €