Skip to content

Commit

Permalink
feat: 加入一些判断,避免不受支持的环境以及性能优化. (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
joesdu authored Sep 24, 2024
2 parents f4e5b4d + ac49079 commit 3beee26
Showing 1 changed file with 54 additions and 11 deletions.
65 changes: 54 additions & 11 deletions src/EasilyNET.Core/Misc/TextWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

/// <summary>
/// 线程安全的控制台在同一行输出消息
Expand All @@ -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;
}
}
}

Expand All @@ -56,6 +64,7 @@ public static async Task SafeClearCurrentLine(this TextWriter _)
{
using (await _lock.LockAsync())
{
UpdateClearLine();
ClearCurrentLine();
}
}
Expand All @@ -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}");
}
}

/// <summary>
Expand Down

0 comments on commit 3beee26

Please sign in to comment.