Tomalak
Tomalak

Reputation: 338158

ANSI-Coloring Console Output with .NET

I try to generate colored console output using ANSI escape codes with the following minimal C# program:

using System;

// test.cs
class foo {
    static void Main(string[] args) {
        Console.WriteLine("\x1b[36mTEST\x1b[0m");
    }
}

I am running Ansicon v1.66 on Windows 7 x64 with csc.exe (Microsoft (R) Visual C# Compiler version 4.6.0081.0).

Colored output works fine in this configuration; Ansicon itself is working flawlessly.

To cross-check I use a node.js one-liner that is 100% equivalent to the C# program:

// test.js
console.log("\x1b[36mTEST\x1b[0m");

And, even more basic, a hand-crafted text file:

text file hex editor screenshot

Both of which which correctly do the expected thing: Print a teal-colored string "TEST":

enter image description here

Only the test.exe I built with csc prints something else. Why?

Upvotes: 13

Views: 18314

Answers (4)

Marcel Batista
Marcel Batista

Reputation: 752

Using Ansi sequences worked out of the box for me if I was running my code from Visual Studio (F5/Play). Running from the console though it wouldn't work.

If you are still struggling with this, this is what solved the problem for me:

using System.Runtime.InteropServices;

class Program
{
    // P/Invoke declarations
    private const int STD_OUTPUT_HANDLE = -11;
    private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport("kernel32.dll")]
    private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);

    [DllImport("kernel32.dll")]
    private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);

    static void Main(string[] args)
    {
        // Get the handle to the standard output stream
        var handle = GetStdHandle(STD_OUTPUT_HANDLE);

        // Get the current console mode
        uint mode;
        if (!GetConsoleMode(handle, out mode))
        {
            Console.Error.WriteLine("Failed to get console mode");
            return;
        }

        // Enable the virtual terminal processing mode
        mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
        if (!SetConsoleMode(handle, mode))
        {
            Console.Error.WriteLine("Failed to set console mode");
            return;
        }

        // Now you can use ANSI escape codes to colorize your output
        Console.WriteLine("\x1b[91mThis should be colorful\x1b[0m");
    }
}

Upvotes: 1

silkfire
silkfire

Reputation: 25945

I've created a small plugin (available on NuGet) that allows you to easily wrap your strings in ANSI color codes. Both foreground and background colors are supported.

enter image description here

It works by extending the String object, and the syntax is very simple:

"colorize me".Pastel("#1E90FF");

After which the string is ready to be printed to the console.

Console.WriteLine("colorize me".Pastel("#1E90FF"));

Upvotes: 23

sxleixer
sxleixer

Reputation: 159

I encountered this question today and I could not get the accepted answer to work. After some research on my own I found an answer which will get it to work.

It is a pitty, but we need to go very low level on this and call the Windows API directly. For this purpose I'm using the PInvoke.Kernel32 NuGet for convenience reasons, but if it is too heavy-weight for you, you might create the P\Invoke mapping yourself.

The following method illustrates, how one may activate the ANSI Codes:

bool TryEnableAnsiCodesForHandle(Kernel32.StdHandle stdHandle)
{
    var consoleHandle = Kernel32.GetStdHandle(stdHandle);
    if (Kernel32.GetConsoleMode(consoleHandle, out var consoleBufferModes) &&
        consoleBufferModes.HasFlag(Kernel32.ConsoleBufferModes.ENABLE_VIRTUAL_TERMINAL_PROCESSING))
        return true;
    
    consoleBufferModes |= Kernel32.ConsoleBufferModes.ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    return Kernel32.SetConsoleMode(consoleHandle, consoleBufferModes);
}

To enable it for StdOut you call it like:

TryEnableAnsiCodesForHandle(Kernel32.StdHandle.STD_OUTPUT_HANDLE);

If the method returns true, the ANSI Codes are enabled, else they are not.

The solution uses the very low level Windows API GetConsoleMode and SetConsoleMode to check if the control buffer mode ENABLE_VIRTUAL_TERMINAL_PROCESSING is set, and if it is not set it tries to set the mode.

Upvotes: 0

rene
rene

Reputation: 42434

Your program needs to be compiled for /platform:x64 if you use the AnsiCon x64 environment and with /platform:x86 if you use the AnsiCon x86/32 bits version. The exact reason is a mystery...

Originally I thought you need all this:

You need to grab the StandardOutput and let the Console.WriteLine believe you write to a File instead of to a Console and use an ASCII encoding.

This is how it will work:

 var stdout = Console.OpenStandardOutput();
 var con = new StreamWriter(stdout, Encoding.ASCII);
 con.AutoFlush = true;
 Console.SetOut(con);

 Console.WriteLine("\x1b[36mTEST\x1b[0m");

The .Net Console.WriteLine uses an internal __ConsoleStream that checks if the Console.Out is as file handle or a console handle. By default it uses a console handle and therefor writes to the console by calling WriteConsoleW. In the remarks you find:

Although an application can use WriteConsole in ANSI mode to write ANSI characters, consoles do not support ANSI escape sequences. However, some functions provide equivalent functionality. For more information, see SetCursorPos, SetConsoleTextAttribute, and GetConsoleCursorInfo.

To write the bytes directly to the console without WriteConsoleW interfering a simple filehandle/stream will do which is achieved by calling OpenStandardOutput. By wrapping that stream in a StreamWriter so we can set it again with Console.SetOut we are done. The byte sequences are send to the OutputStream and picked up by AnsiCon.

Do notice that this is only useable with an applicable terminal emulator, like AnsiCon, as shown here:

enter image description here

Upvotes: 10

Related Questions