Alex Rivera | Logout

Redirect command prompt output to GUI and KEEP COLOR?

Asked 2012-09-24T22:04:52.117
11

Basically I'm making a command prompt GUI. User sees command prompt output in a rich text box, and inputs commands in a plain textbox underneath. I have succeeded in making this work, EXCEPT that to me it seems impossible to get the color information. For example, if I run a program which outputs red error text, I don't get the color code bytes, they simply aren't in the stream!

Here's what I'm doing now. To start the process:

ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Windows\System32\cmd.exe");
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;

this.promptProcess = Process.Start(startInfo);

Then I create a thread which reads from the output stream and sends that to my text box:

while (true)
{
    while (this.stream.EndOfStream) ;

    //read until there's nothing left in the stream, writing to the (locked) output box                    
    byte [] buffer = new byte[1000];
    int numberRead;
    StringBuilder builder = new StringBuilder();
    do
    {
        numberRead = this.stream.BaseStream.Read(buffer, 0, buffer.Length);
        char[] characters = UTF8Decoder.GetChars(buffer, 0, numberRead);
        builder.Append(characters);
    }
    while (numberRead == buffer.Length);

    this.writeToOutput(builder.ToString());
}

Even if I use my fancy command prompt to start an application which would output colored text, I don't get any additional color information (not even the ANSI color codes mixed in with the text). As you can see above, I'm going to the BaseStream and reading the bytes, then decoding them into UTF8. Unfortunately, it seems that even the raw bytes do not include the original color information.

How can I get the original stream from the applications I run, without any filtering at all? I want the

Edit
Report

1 Answer

11

Redirection of Output stream will never contains "color information". With exception of case, when console program explicitly output text with ANSI escape sequences. But! Windows console does not support ANSI codes, so very small amount of such programs exists.

Colors in WinAPI may be written into the console (not stream) directly only, with family of console functions like WriteConsoleOutput. Naturally, colored output may be readed from console with corresponding functions, like ReadConsoleOutput. Of course, console window must exists and not redirection must be implied.

Same issue with unicode. Stream does not supports UTF-8, unless you ask the program write output in that codepage. But, when you read text from console (not stream) with ReadConsoleOutputW - you'll get unicode "from the box".

PS. My own console emulator ConEmu (read answer on SO) reads console output via ReadConsoleOutputW.

answered 2012-09-24T23:16:58.297

Your Answer