dotnet/runtime

System.Console.Clear() sometimes did not clear the console buffer.

Open

#83.568 aperta il 17 mar 2023

Vedi su GitHub
 (2 commenti) (0 reazioni) (0 assegnatari)C# (5445 fork)batch import
area-System.Consolehelp wanted

Metriche repository

Star
 (17.886 star)
Metriche merge PR
 (Merge medio 12g 11h) (661 PR mergiate in 30 g)

Descrizione

Description

When running a console application that calls Console.Clear() from a command prompt on Windows Terminal, the console buffer may not be completely cleared.

This is different behavior than described in the specification of Console.Clear().

The above specification states that the behavior is equivalent to the command prompt's cls command. And actually the cls command clears all the contents of the console buffer even in the Windows Terminal command prompt.

Reproduction Steps

  1. At the command prompt on Windows Terminal, and
  2. There are more lines of text in the console buffer than there are lines in the console window, and
  3. Run a console application that calls Console.Clear(), and
  4. Try scrolling the console to see previous text.

Expected behavior

All lines in the console buffer have been erased and scrolling is not possible in the first place.

Actual behavior

Scrolling to a previous line retains the contents of the previous line.

Regression?

I don't know from which version the problem existed.

Known Workarounds

I don't know how to work around this issue.

Configuration

  • .NET runtime
    • .NET 7.0.4
    • .NET 6.0.15
  • OS
    • Windows 10
  • Architecture
    • x64
  • Console
    • Command prompt (on Windows terminal)

Other information

Below is a sample source code. This program calls Console.Clear() after displaying 100 lines of text. After running this program, try to view the previous lines by scrolling the console. I was expecting none of the previous lines to remain, but in fact only some of the lines were cleared.

using System;

namespace Experiment.ConsoleClearNotClearingConsoleBuffer.Experiment01
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            for (var count = 0; count < 100; count++)
                Console.WriteLine($"#{count}: This line should be cleared.");
            Console.Clear();
            Console.WriteLine("Check if all lines have been deleted by manipulating the console scroll bar.");
            Console.WriteLine("And compare it with the result of running the cls command.");
        }
    }
}

I have tried the following:

  • When I ran this sample program from the Windows command prompt, it cleared all the text in the console buffer.
  • When I ran this sample program from a command prompt on Windows Terminal, the console buffer was not completely cleared. When I scrolled through the console, I was left with the following text:
...
#71: This line should be cleared.
#72: This line should be cleared.
#73: This line should be cleared.
#74: This line should be cleared.
Check if all lines have been deleted by manipulating the console scroll bar.
And compare it with the result of running the cls command.

C:\>
  • After that, when I ran the cls command in the Windows Terminal Command Prompt, all the text in the console buffer was cleared. Of course this is the expected behavior.
  • When I ran this sample program in ubuntu in windows terminal (with linux on Windows), it did not clear the console buffer at all as shown below. Instead the console was forced to scroll to a new line. As a result, the console screen appeared to be cleared.
...
#95: This line should be cleared.
#96: This line should be cleared.
#97: This line should be cleared.
#98: This line should be cleared.
#99: This line should be cleared.
Check if all lines have been deleted by manipulating the console scroll bar.
And compare it with the result of running the cls command.
rougemeilland@ROUGEMEILLAND:~$

Guida contributor