Redirect command line output to GUI and KEEP COLOR?

Basically I create a command line GUI. The user sees the command line output in the extended text field and enters the commands in the form of a simple text field below it. I managed to do this work, EXCEPT that it seems to me impossible to get color information. For example, if I run a program that displays red error text, I do not get the bytes of the color code, they simply are not in the stream!

Here is what I am 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 stream that reads from the output stream and sends it to the text box:

 while (true) { while (this.stream.EndOfStream) ; //read until there 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 prompt to launch an application that displays colored text, I do not receive any additional color information (even ANSI color codes do not mix with text). As you can see above, I go to BaseStream and read the bytes and then decrypt them in UTF8. Unfortunately, it seems that even raw bytes do not include the original color information.

How can I get the source stream from the applications that I run without any filtering? I want raw bytes so that I can do my own color analysis and display console color output.

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

+6
source share
1 answer

Output redirection will never contain "color information". Unless the console program explicitly displays text with ANSI escape sequences. But! The Windows console does not support ANSI codes, so there are very few such programs.

Colors in WinAPI can only be written directly to the console (and not in the stream) using the console family of functions, such as WriteConsoleOutput . Naturally, the color output can be read from the console with the corresponding functions, such as ReadConsoleOutput . Of course, a console window should exist, and no redirection should be implied.

Same problem with unicode. Stream does not support UTF-8 unless you request the output of a program record to this code page. But, when you read text from the console (not a stream) using ReadConsoleOutputW - you will get unicode "from the window".

PS. My ConEmu console emulator ( read the answer to SO ) reads console output through ReadConsoleOutputW .

+9
source

Source: https://habr.com/ru/post/926156/


All Articles