Skip to content

Commit

Permalink
fix: 修复SafeClearPreviousLine光标位置错误. (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
joesdu authored Sep 24, 2024
2 parents 3beee26 + c6ed526 commit e3b11a5
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/EasilyNET.Core/Misc/TextWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,30 @@ private static void UpdateClearLine()
_clearLine = new(' ', _lastWindowWidth);
}

private static bool IsConsoleCursorPositionSupported()
{
try
{
// 尝试设置光标位置
var current = Console.GetCursorPosition();
Console.SetCursorPosition(current.Left, current.Top);
return true;
}
catch (IOException)
{
// 捕获到 IOException,说明不支持
return false;
}
catch (PlatformNotSupportedException)
{
// 捕获到 PlatformNotSupportedException,说明不支持
return false;
}
}

private static void ClearPreviousLine()
{
if (Console.IsOutputRedirected)
if (!IsConsoleCursorPositionSupported())
{
Console.WriteLine();
return;
Expand All @@ -122,7 +143,7 @@ private static void ClearPreviousLine()

private static void ClearCurrentLine()
{
if (Console.IsOutputRedirected)
if (!IsConsoleCursorPositionSupported())
{
Console.WriteLine();
return;
Expand Down

0 comments on commit e3b11a5

Please sign in to comment.