From d7ad3b43d0d9f78907c5c9be91639e86fc44d10f Mon Sep 17 00:00:00 2001 From: Ionite Date: Sun, 5 Nov 2023 23:12:15 -0500 Subject: [PATCH] Fix AnsiProcess encoding to not include BOM mark --- StabilityMatrix.Core/Processes/AnsiProcess.cs | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/StabilityMatrix.Core/Processes/AnsiProcess.cs b/StabilityMatrix.Core/Processes/AnsiProcess.cs index 7aff68ef..25f9fe0d 100644 --- a/StabilityMatrix.Core/Processes/AnsiProcess.cs +++ b/StabilityMatrix.Core/Processes/AnsiProcess.cs @@ -10,22 +10,22 @@ public class AnsiProcess : Process { private AsyncStreamReader? stdoutReader; private AsyncStreamReader? stderrReader; - + public AnsiProcess(ProcessStartInfo startInfo) { StartInfo = startInfo; EnableRaisingEvents = false; - + StartInfo.UseShellExecute = false; StartInfo.CreateNoWindow = true; StartInfo.RedirectStandardOutput = true; StartInfo.RedirectStandardInput = true; StartInfo.RedirectStandardError = true; - + // Need this to parse ANSI escape sequences correctly - StartInfo.StandardOutputEncoding = Encoding.UTF8; - StartInfo.StandardErrorEncoding = Encoding.UTF8; - StartInfo.StandardInputEncoding = Encoding.UTF8; + StartInfo.StandardOutputEncoding = new UTF8Encoding(false); + StartInfo.StandardErrorEncoding = new UTF8Encoding(false); + StartInfo.StandardInputEncoding = new UTF8Encoding(false); } /// @@ -35,23 +35,33 @@ public class AnsiProcess : Process public void BeginAnsiRead(Action callback) { var stdoutStream = StandardOutput.BaseStream; - stdoutReader = new AsyncStreamReader(stdoutStream, s => - { - if (s == null) return; - callback(ProcessOutput.FromStdOutLine(s)); - }, StandardOutput.CurrentEncoding); - + stdoutReader = new AsyncStreamReader( + stdoutStream, + s => + { + if (s == null) + return; + callback(ProcessOutput.FromStdOutLine(s)); + }, + StandardOutput.CurrentEncoding + ); + var stderrStream = StandardError.BaseStream; - stderrReader = new AsyncStreamReader(stderrStream, s => - { - if (s == null) return; - callback(ProcessOutput.FromStdErrLine(s)); - }, StandardError.CurrentEncoding); + stderrReader = new AsyncStreamReader( + stderrStream, + s => + { + if (s == null) + return; + callback(ProcessOutput.FromStdErrLine(s)); + }, + StandardError.CurrentEncoding + ); stdoutReader.BeginReadLine(); stderrReader.BeginReadLine(); } - + /// /// Waits for output readers to finish ///