Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect command prompt output to GUI and KEEP COLOR?

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 raw bytes so that I can do my own color parsing and present correctly-colored console output.

To clarify, I am not asking how to interpret the color codes. I just want to make them available in the stream.

like image 542
BigScary Avatar asked Sep 24 '12 22:09

BigScary


People also ask

How do I redirect the output of a command in command prompt?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

Which Windows command line is used to redirect output?

Redirecting Output The > symbol is used to redirect output by taking the output from the command on the left and passing as input to the file on the right.


1 Answers

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.

like image 106
Maximus Avatar answered Oct 22 '22 23:10

Maximus