Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I recieve color output from console application (like Far)?

I have some code:

cmdProcess = new Process();
var procStartInfo = new ProcessStartInfo( "cmd", "/k "C:\\Program Files (x86)\\Far Manager\\Far.exe"" );

procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UseShellExecute = false;

procStartInfo.CreateNoWindow = true;
cmdProcess.OutputDataReceived += ( s, e ) => {
       callbackFn(e.Data + "\n");
};
cmdProcess.StartInfo = procStartInfo;

cmdProcess.Start();
cmdProcess.BeginOutputReadLine();

But with this code I only can launch the process and get something, but not completely and not color. Also I tried the ReceiveConsoleOutput function and I receive only blank buffer. With WinAPI i can only start console and nothing else - I don't understand it well. But I'm not against WinAPI examples, because i think that my problem may be solved with it. I would be grateful if anyone can help me.

P.S. I'm sorry for bad english.

like image 349
Undermuz Avatar asked Mar 07 '16 14:03

Undermuz


1 Answers

You talk about two different things. ConEmu and the original console has color support, but this is achieved via the console buffer API (here is a complete C# library). The console supports not just coloring but cursors and mouse, too; however, none of them have anything to do with the standard output.

But if you want to receive color information in the standard output, you can use the ANSI escape sequences, which is a standard in terminal communication (and this is used for ANSI graphics art, too), supports coloring and cursor positioning as well and can be encoded as a character stream.

But if the process you call does not dump ANSI sequences, (cmd does not do this) you will not receive any color information.

like image 109
György Kőszeg Avatar answered Sep 28 '22 11:09

György Kőszeg