1

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 < 100; i++)
{
    Console.WriteLine(i);
}
Console.Clear();
Console.ReadKey();

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

Alex
  • 70
  • 5
  • 1
    Cannot reproduce. When calling Console.Clear() the buffer is cleared as well. This also matches the documentation: https://learn.microsoft.com/en-us/dotnet/api/system.console.clear?view=net-5.0. – wohlstad Feb 16 '23 at 11:32
  • What OS? What kind of console? – shingo Feb 16 '23 at 11:40
  • Windows 11, IDE: Visual Studio 2022 – Alex Feb 16 '23 at 11:47
  • Using the Clear method is equivalent invoking the MS-DOS cls command in the command prompt window. When the Clear method is called, the cursor automatically scrolls to the top-left corner of the window and the contents of the screen buffer are set to blanks using the current foreground background colors. – CharithJ Feb 16 '23 at 11:49
  • May be you have something else that writes even after the clear? Show your full code. – CharithJ Feb 16 '23 at 11:49
  • @CharithJ Unfortunately my code is too long to post. So I've edited my question and added a short poc – Alex Feb 16 '23 at 12:00
  • Or maybe I messed somethig up with my Visual Studio?? – Alex Feb 16 '23 at 12:01
  • Visual Studio is not involved at all if you run a console process. – Klaus Gütter Feb 16 '23 at 12:06
  • 3
    https://stackoverflow.com/questions/52127653/how-to-clear-the-entire-terminal-powershell – Hans Passant Feb 16 '23 at 12:24
  • The following may be helpful: [Classic Console APIs versus Virtual Terminal Sequences](https://learn.microsoft.com/en-us/windows/console/classic-vs-vt), [Windows Console and Terminal Ecosystem Roadmap](https://learn.microsoft.com/en-us/windows/console/ecosystem-roadmap), [Console Virtual Terminal Sequences](https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences), [Console Functions](https://learn.microsoft.com/en-us/windows/console/console-functions), – Tu deschizi eu inchid Feb 16 '23 at 15:04
  • [VB.NET Console: Use ANSI Sequences (Console Virtual Terminal Sequences](https://stackoverflow.com/a/66432648/10024425) – Tu deschizi eu inchid Feb 16 '23 at 15:05
  • Did you enable the [legacy console mode](https://learn.microsoft.com/en-us/windows/console/legacymode)? Also try to [disable scroll forward](https://devblogs.microsoft.com/commandline/new-experimental-console-features/#disable-scroll-forward) – shingo Feb 17 '23 at 03:47
  • 1
    @Alex, I am now able to reproduce your problem. Seems to be related with the new "Windows Terminal", which comes with Win11. If you still have Win10, you can download and install the new Terminal here: https://learn.microsoft.com/en-us/windows/terminal/install?source=docs. Then, you have to start the program not from Visual Studio, but in the terminal. In the terminal the problem can be reproduced in the Powershell and in the CommandLine mode. – Chrigl Mar 03 '23 at 16:11

1 Answers1

1

Thanks for all your replies!

Finally I solved it with the following escape sequence:

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

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.

Alex
  • 70
  • 5