2025-10-11 18:25:59 +08:00

69 lines
2.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* <copyright>
Company="公司"
2020-2025
Author=JCH
Path=XiaoZhiSharp.Utils.FixConsoleWriteLine.cs
* <copyright>
* --------------------------------------------------------------
* 机器名称JCH
* 文件名FixConsoleWriteLine.cs
* 版本号V1.0.0.0
* 创建人JCH
* 创建时间2025/9/24 11:35:26
* 描述:
* --------------------------------------------------------------
* 修改时间2025/9/24 11:35:26
* 修改人JCH
* 版本号V1.0.0.0
* 描述:
* --------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XiaoZhiSharp.Utils
{
public static class FixConsoleWriteLine
{
/// <summary>
/// 上一次打印内容
/// </summary>
private static string? _lastMessage = null;
/// <summary>
/// 固定在新的一行的位置,重复打印新的内容
/// </summary>
/// <param name="message">打印内容</param>
/// <param name="isLast">是否为此位置的最后一次打印</param>
public static void WriteLine(string message, bool isLast = false)
{
if (_lastMessage != null)
{
//如果不是第一次打印,则清除上一次打印的内容,并在原位置打印
Console.SetCursorPosition(0, Console.CursorTop - 1);//将光标移动到上一行行首
Console.Write(new string(' ', _lastMessage.Length));//用空格覆盖原内容
Console.SetCursorPosition(0, Console.CursorTop);//将光标移动到当前行行首
Console.WriteLine(message);//打印新内容
}
else
{
//如果是第一次打印,则在新的一行打印内容
Console.WriteLine(message);
}
if (isLast)
{
_lastMessage = null;
}
else
{
_lastMessage = message;
}
}
}
}