From ac49079a695a026b4d4c68c5ff9aab1155820cd0 Mon Sep 17 00:00:00 2001 From: Joes Date: Tue, 24 Sep 2024 12:44:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8A=A0=E5=85=A5=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=88=A4=E6=96=AD,=E9=81=BF=E5=85=8D=E4=B8=8D=E5=8F=97?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9A=84=E7=8E=AF=E5=A2=83=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Misc/TextWriterExtensions.cs | 65 +++++++++++++++---- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/src/EasilyNET.Core/Misc/TextWriterExtensions.cs b/src/EasilyNET.Core/Misc/TextWriterExtensions.cs index 110b732a..a66d742e 100644 --- a/src/EasilyNET.Core/Misc/TextWriterExtensions.cs +++ b/src/EasilyNET.Core/Misc/TextWriterExtensions.cs @@ -1,5 +1,5 @@ -using EasilyNET.Core.Threading; -using System.Text; +using System.Text; +using EasilyNET.Core.Threading; // ReSharper disable UnusedMember.Global // ReSharper disable MemberCanBePrivate.Global @@ -13,6 +13,9 @@ namespace EasilyNET.Core.Misc; public static class TextWriterExtensions { private static readonly AsyncLock _lock = new(); + private static string _lastOutput = string.Empty; + private static string _clearLine = new(' ', Console.WindowWidth); + private static int _lastWindowWidth = Console.WindowWidth; /// /// 线程安全的控制台在同一行输出消息 @@ -33,8 +36,13 @@ public static async Task SafeWriteOutput(this TextWriter writer, string msg) { using (await _lock.LockAsync()) { - ClearCurrentLine(); - await writer.WriteAsync(msg); + UpdateClearLine(); + if (_lastOutput != msg) + { + ClearCurrentLine(); + await writer.WriteAsync(msg); + _lastOutput = msg; + } } } @@ -56,6 +64,7 @@ public static async Task SafeClearCurrentLine(this TextWriter _) { using (await _lock.LockAsync()) { + UpdateClearLine(); ClearCurrentLine(); } } @@ -78,23 +87,57 @@ public static async Task SafeClearPreviousLine(this TextWriter _) { using (await _lock.LockAsync()) { + UpdateClearLine(); ClearPreviousLine(); } } + private static void UpdateClearLine() + { + if (Console.WindowWidth == _lastWindowWidth) return; + _lastWindowWidth = Console.WindowWidth; + _clearLine = new(' ', _lastWindowWidth); + } + private static void ClearPreviousLine() { - if (Console.CursorTop <= 0) return; - Console.SetCursorPosition(0, Console.CursorTop - 1); - Console.Write(new string(' ', Console.WindowWidth)); - Console.SetCursorPosition(0, Console.CursorTop); + if (Console.IsOutputRedirected) + { + Console.WriteLine(); + return; + } + try + { + if (Console.CursorTop <= 0) return; + Console.SetCursorPosition(0, Console.CursorTop - 1); + Console.Write(_clearLine); + Console.SetCursorPosition(0, Console.CursorTop - 1); + } + catch (IOException ex) + { + // Log the exception or handle it as needed + Console.WriteLine($"IOException: {ex.Message}"); + } } private static void ClearCurrentLine() { - Console.SetCursorPosition(0, Console.CursorTop); - Console.Write(new string(' ', Console.WindowWidth)); - Console.SetCursorPosition(0, Console.CursorTop); + if (Console.IsOutputRedirected) + { + Console.WriteLine(); + return; + } + try + { + Console.SetCursorPosition(0, Console.CursorTop); + Console.Write(_clearLine); + Console.SetCursorPosition(0, Console.CursorTop); + } + catch (IOException ex) + { + // Log the exception or handle it as needed + Console.WriteLine($"IOException: {ex.Message}"); + } } ///