using System.Diagnostics;
namespace StabilityMatrix.Core.Processes;
///
/// Process supporting parsing of ANSI escape sequences
///
public class AnsiProcess : Process
{
public AnsiProcess(ProcessStartInfo startInfo)
{
StartInfo = startInfo;
EnableRaisingEvents = false;
StartInfo.UseShellExecute = false;
StartInfo.CreateNoWindow = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardError = true;
}
///
/// Start asynchronous reading of stdout and stderr
///
/// Called on each new line
public void BeginAnsiRead(Action callback)
{
var stdoutStream = StandardOutput.BaseStream;
var stdoutReader = new AsyncStreamReader(stdoutStream, s =>
{
if (s == null) return;
callback(ProcessOutput.FromStdOutLine(s));
}, StandardOutput.CurrentEncoding);
var stderrStream = StandardError.BaseStream;
var stderrReader = new AsyncStreamReader(stderrStream, s =>
{
if (s == null) return;
callback(ProcessOutput.FromStdErrLine(s));
}, StandardError.CurrentEncoding);
stdoutReader.BeginReadLine();
stderrReader.BeginReadLine();
}
}