Skip to content

Commit

Permalink
feat: 添加清除控制台上一行内容的方法. (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
joesdu authored Sep 23, 2024
2 parents c9a1124 + 7600cd3 commit b42ecc0
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/EasilyNET.Core/Misc/TextWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ public static async Task SafeClearCurrentLine(this TextWriter _)
}
}

/// <summary>
/// 线程安全的清除上一行
/// <remarks>
/// <para>
/// 使用方式:
/// <code>
/// <![CDATA[
/// Console.Out.SafeClearPreviousLine();
/// ]]>
/// </code>
/// </para>
/// </remarks>
/// </summary>
/// <returns></returns>
public static async Task SafeClearPreviousLine(this TextWriter _)
{
using (await _lock.LockAsync())
{
ClearPreviousLine();
}
}

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 - 1);
}

private static void ClearCurrentLine()
{
Console.SetCursorPosition(0, Console.CursorTop);
Expand Down

0 comments on commit b42ecc0

Please sign in to comment.