Console.Clear();不会清除整个控制台。

huangapple go评论56阅读模式
英文:

Console.Clear(); doesn't clean up the whole console

问题

我在我的应用程序中有一部分生成的输出超出了控制台窗口的容纳范围。我滚动查看生成的行,但清除控制台窗口时只有最后一部分消失。当我向上滚动时,控制台窗口上方的所有内容仍然存在。

现在,是否有一种方法可以清除整个控制台内容,包括滚动缓冲区?或者开发一个分页程序是唯一的解决方案吗?

谢谢您的回复!

编辑:

我编写了以下代码作为POC:

for (int i = 0; i < 100; i++)
{
    Console.WriteLine(i);
}
Console.Clear();
Console.ReadKey();

当我运行该代码时,只有第 71 到 100 行将被删除。

英文:

I have a part in my application that generates more output than in a console window fits. I'm satisfied with scrolling through the generated lines but by clearing the console window only the last part disappears. Everything above the height of the console window is still there when I'm scrolling up.

Now, is there a way to wipe the whole console content even the scrollback buffer? Or is the only solution to develop a pager?

Thanks for your replies!

EDIT:

I wrote the following code as POC:

for (int i = 0; i &lt; 100; i++)
{
    Console.WriteLine(i);
}
Console.Clear();
Console.ReadKey();

When I'm running that code only the lines 71 to 100 will be deleted.

答案1

得分: 2

感谢您所有的回复!

最终,我使用以下转义序列解决了它:

Console.Clear();
Console.WriteLine("\x1b[3J");

这个序列会移除控制台的所有内容。(但只有在首先调用清除命令时才能可靠工作)

在这两行之后,控制台为空,滚动条被移除或禁用。光标位于第二行(最后输入可能还有一些字符)。为了防止这种情况,我再次调用了清除命令。

希望这对有相同问题的人有所帮助。

英文:

Thanks for all your replies!

Finally I solved it with the following escape sequence:

Console.Clear();
Console.WriteLine(&quot;\x1b[3J&quot;);

This sequence removes the whole content of the console. (But it only works reliable if the clear command is called first)

After these two lines the console is empty and the scrollbars are removed or disabled. The cursor is on the second line (and there could be a few chars from the last input). To prevent that I called the clear command again.

I hope this will help someone with the same problem.

huangapple
  • 本文由 发表于 2023年2月16日 19:27:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定