13

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Its not really a valid escape code in C#, see: https://msdn.microsoft.com/en-us/library/h21280bw.aspx It looks like the string `\x1b` is turned into a non-printable character before output to the console, the rest of it is output as-is. If you want to try outputing the raw text, prepend the string with the `@` symbol, aka: `Console.WriteLine(@"\x1b[36mTEST\x1b[0m");` – Ron Beyer Dec 03 '15 at 18:30
  • @RonBeyer Of coursed this is a valid escape code. It's the ASCII ESC character. – Tomalak Dec 03 '15 at 18:31
  • @Tomalak How do you expect the ESC character to be printed? – Fᴀʀʜᴀɴ Aɴᴀᴍ Dec 03 '15 at 18:32
  • 1
    By invalid I mean non-printable, any hex number is "valid". – Ron Beyer Dec 03 '15 at 18:32
  • I'm sorry to say it, but I am afraid that you will have to read up on how ANSI color codes work. (I provided the JS counter-example not without reason.) – Tomalak Dec 03 '15 at 18:34
  • I understand the color codes, and I understand what you are trying to do here, but the C# compiler is replacing \x1b with the ESC code during compilation, so its basically turning it into a unicode unprintable character before its ever output to the console. I'm not sure this is making it to the console the same way as the JS version. – Ron Beyer Dec 03 '15 at 18:37
  • That's exactly what node.js does. – Tomalak Dec 03 '15 at 18:41

3 Answers3

21

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"));
Vinod Srivastav
  • 3,644
  • 1
  • 27
  • 40
silkfire
  • 24,585
  • 15
  • 82
  • 105
9

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

Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152
  • ...unfortunately, it does not seem to work. After the change, the output of your program stays the same `[36m...TEST...[0m` for me (plus the visible replacement characters for the `\x1b`). – Tomalak Dec 04 '15 at 09:39
  • @Tomalak hmm, maybe it has something to do with the default encoding. In my experiment last night I first went down the route to get the ASCII encoded byte array of the test string and then do a Console.OpenStandardOutput().WriteByte(array,0, array.Length); when that worked I reworked the stuff to my working example. Maybe the streamwriter needs an encoding? – rene Dec 04 '15 at 10:32
  • I'm at work atm and don't have a dev box ready, I'll revisit this tonight. – rene Dec 04 '15 at 10:32
  • @Tomalak I found the issue. I had to add the Encoding.ASCII to the streamwriter again. I must have missed that I removed the encoding but didn't recompile, sorry for the hassle. – rene Dec 04 '15 at 20:10
  • Nope, still no dice. It looks as if AnsiCon would be entirely circumvented. When I run this on an unarmed cmd.exe the output is exactly the same. Can you post the exact source code that works for you? – Tomalak Dec 04 '15 at 21:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97019/discussion-between-rene-and-tomalak). – rene Dec 04 '15 at 21:37
  • In case you don't want to limit yourself with stand set of colors, look at this https://stackoverflow.com/a/76005078/3057246 – Vinod Srivastav Apr 13 '23 at 11:54
0

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.

sxleixer
  • 159
  • 1
  • 6
  • It sounds like you're not using Ansicon, but native Windows console coloring? – Tomalak Jan 30 '22 at 11:34
  • Actually, I don't know nothing about the internals of the solution. But in the further program I am using `Console.WriteLine` and `Console.Error.WriteLine` and it works. – sxleixer Jan 30 '22 at 11:37
  • I checked PowerShell, CMD and Rider Terminal. They all work with this solution. – sxleixer Jan 30 '22 at 12:10
  • They probably won't in Windows 7 cmd.exe (which this question was about specifically), because native support for color codes was added to Windows later. So this might solve your issue, but it's not really an answer to the question, because your setup is different. – Tomalak Jan 30 '22 at 12:15
  • See this answer, https://stackoverflow.com/a/76005078/3057246 it's a color mixer and not limited to standard set – Vinod Srivastav Apr 13 '23 at 11:53