You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Console.Clear() doesn't clear the scrollback buffer on Unix-like platforms #28355
Clears the console buffer and corresponding console window of display information.
Indeed, not just clearing the current terminal window (screen), but also the scrollback buffer is the typical use case, and it is how this method has always worked on Windows.[1]
By contrast, the Unix implementation currently only clears the screen, which makes for an inconsistent cross-platform experience.
While there is no POSIX-compliant way to clear the scrollback buffer (only clearing the screen is mandated by POSIX, via tput clear), xterm-compatible terminal applications do support escape sequence <esc>[3J - see Wikipedia.
In practice, the macOS terminal application and the one on Ubuntu, for instance, do support this escape sequence - generally, terminal emulators based on the X Window System.
I don't know if there are popular terminal emulators out there that do not support it, but even a best-effort implementation would be useful.
Here's a workaround that demonstrates use of the escape sequence:
// Clears the screen and the scrollback buffer in xterm-compatible terminals.Console.Clear();Console.WriteLine("\x1b[3J")
[1] Whether there should be an opt-in for predictably clearing only the screen across platforms, while leaving the scrollback buffer intact, is a separate question.
Reacted by Felix Becker, h110m, Behzad Khosravifar and Unknown6656
@SteveL-MSFT: Given that (a) clearing the buffer is documented and (b) doing so is generally the more sensible behavior, my vote is to simply change the existing behavior to align with the documentation / the behavior on Windows.
Reacted by Daniel Chalmers, Richard Cox and Unknown6656
do you recall whether this was intentional, to not attempt to clear scrollback if supported?
Not clearing it was not a goal. If there's a "safe" way to do it whenever it's possible (by safe I mean not emitting garbage to stdout when the particular escape code isn't supported by the terminal in use), such as relying on some reliable way to query that a compatible terminal is being used, I'm fine with doing so.
Reacted by Michael Klement, Steve Lee and Unknown6656
And presumably if we did that first and then did the current clear (rather than the opposite order), if we did end up for some reason generating garbage, it'd be mitigated by being immediately cleared? :)
@mklement0 sounds good. I sent you a collaborator invite: it's optional, if you accept it I can formally assign you. Note that accepting it switches on notifications for all of the repo, which you'll likely want to switch off again.
Did you try printing a bunch of lines first, instead of immediately clearing @kasperk81 ?
I've not tested it with the fix yet, but plan to tomorrow when 8.0 releases (but I'm not convinced it will work for macOS until I test it myself).
How is it "of course" if it's not in the code you showed.
Edit: I don't know why you're downvoting all my comments for trying to determine if this bug is fixed or not. It wasn't "of course" because your code didn't show it, I never said anything else had an issue, other than my personal doubt that it's fixed (because it's been a bug for years), I simply asked if you checked it since it wasn't in the code (therefore I assumed you hadn't checked it - why do you think I asked). I then pointed out it wasn't "of course", since it wasn't as I just explained.
I just installed the officially released 8.0.100 version and I can confirm @iSazonov's findings: As far as I can tell, it does NOT work on macOS - neither in Terminal.app nor in popular third-party alternative iTerm2.app:
Repro code (reproduces the symptom on macOS 13.5.2):
for(vari=0;i<500;++i){Console.WriteLine(i);};Console.Write("Press a key to clear the terminal");Console.ReadKey(true);Console.Clear();// Scroll back up afterwards - you'll see that the numbers are still there.
By contrast, adding the escape sequence that clears the scrollback buffer shown in the initial post works, in both terminal applications:
Console.Clear();Console.WriteLine("\x1b[3J");
Shell alternatives:
Note: /usr/bin/clear on macOS clears only the current screen, not the scrollback buffer.
Clear+=clearScrollbackBuffer;// the E3 command must come after the Clear command
}
use:
if((RuntimeInformation.IsOSPlatform(OSPlatform.OSX)&&Environment.OSVersion.Version.Major<14?"\x1b[3J":db.GetExtendedString("E3"))isstringclearScrollbackBuffer){Clear+=clearScrollbackBuffer;// the E3 command must come after the Clear command}
Windows is not affected, and I'm not aware of issues on Linux distros, so this should be the only workaround required.
Once .NET no longer supports macOS 13 and below, this workaround can be removed.
I think something like the above is a good solution. (OperatingSystem.IsMacOS() && !OperatingSystem.IsMacOSVersionAtLeast(14) seems better suited though)
Note that with dotnet/roslyn#70497, we should be able to even write \e[3J with C# next :)
If it concerns you that it might print something random out after it @adamsitnik, you could also re-append TermInfo.WellKnownStrings.Clear (or something similar) to it again to ensure that a clear goes through for this workaround - it seems to me like this should work well in most cases.
Ie, you could write:
if(db.GetExtendedString("E3")isstringclearScrollbackBuffer){Clear+=clearScrollbackBuffer;// the E3 command must come after the Clear command}elseif(OperatingSystem.IsMacOS()&&!OperatingSystem.IsMacOSVersionAtLeast(14)){Clear+="\e[3J"+db.GetString(TermInfo.WellKnownStrings.Clear);//use \e[3J to do a full clear on macOS terminal, and print the clear string again in case some terminal emulators don't understand this code to ensure we don't get random characters printed out}
(note: I've not tested the above actually does what it's intended to)
The solution that you suggested assumes that every Terminal that runs on macOS supports "\x1b[3J". I doubt it's always true.
The point of the way I suggested doing it is assuming that they don't all support it. That's why I suggested a normal clear after. Would that not work?
Activity
SteveL-MSFT commentedon Jan 9, 2019
Perhaps adding an overload with a
bool includeScrollBackBufferwould retain current behavior and allow for the desired behaviormklement0 commentedon Jan 9, 2019
@SteveL-MSFT: Given that (a) clearing the buffer is documented and (b) doing so is generally the more sensible behavior, my vote is to simply change the existing behavior to align with the documentation / the behavior on Windows.
danmoseley commentedon Jan 9, 2019
@stephentoub do you recall whether this was intentional, to not attempt to clear scrollback if supported?
stephentoub commentedon Jan 9, 2019
Not clearing it was not a goal. If there's a "safe" way to do it whenever it's possible (by safe I mean not emitting garbage to stdout when the particular escape code isn't supported by the terminal in use), such as relying on some reliable way to query that a compatible terminal is being used, I'm fine with doing so.
mklement0 commentedon Jan 9, 2019
Thanks, @stephentoub.
Based on the answers posted at https://unix.stackexchange.com/q/93376/54804, this should be as simple as:
xterm-compatible terminal emulators set env. var.
TERMeither toxtermorxterm-<colorCountSpec>, e.g.,xterm-256color.stephentoub commentedon Jan 9, 2019
And presumably if we did that first and then did the current clear (rather than the opposite order), if we did end up for some reason generating garbage, it'd be mitigated by being immediately cleared? :)
mklement0 commentedon Jan 10, 2019
Exemplary garbage disposal, @stephentoub :)
danmoseley commentedon Jan 10, 2019
@mklement0 do you wish to offer a PR?
khellang commentedon Jan 10, 2019
You should be able to query the terminfo db for the E3 extension, no?
See user_caps(5) under "Recognized capabilities":
mklement0 commentedon Jan 10, 2019
Thanks, @khellang, but in practice neither
clearnortput clearclear the scrollback buffer on macOS 10.14 and Ubuntu 18.04.Consistent with that, the requisite escape sequence,
\E[3J, is not present in the output frominfocmpwhen invoked from an xterm-compatible terminal.I know little about terminal-info databases, so maybe I'm missing something.
@danmosemsft: I'll give it a shot.
khellang commentedon Jan 10, 2019
Interesting Linux SE answer; https://unix.stackexchange.com/a/375784.
danmoseley commentedon Jan 10, 2019
@mklement0 sounds good. I sent you a collaborator invite: it's optional, if you accept it I can formally assign you. Note that accepting it switches on notifications for all of the repo, which you'll likely want to switch off again.
mklement0 commentedon Jan 11, 2019
Thanks, @danmosemsft; I've accepted the invitation.
danmoseley commentedon Jan 11, 2019
Assigned.
23 remaining items
iSazonov commentedon Nov 14, 2023
@adamsitnik It did not fix MacOS.
kasperk81 commentedon Nov 14, 2023
are you sure? i tried 8.0-rtm with
it clears the scrollback buffer on mac terminal and prints out Hello, World!
7.0 doesn't clear the scrollback buffer
hamarb123 commentedon Nov 14, 2023
Did you try printing a bunch of lines first, instead of immediately clearing @kasperk81 ?
I've not tested it with the fix yet, but plan to tomorrow when 8.0 releases (but I'm not convinced it will work for macOS until I test it myself).
kasperk81 commentedon Nov 14, 2023
of course i tested with overflown buffers and that's the difference between .net 8 vs 7
hamarb123 commentedon Nov 14, 2023
How is it "of course" if it's not in the code you showed.
Edit: I don't know why you're downvoting all my comments for trying to determine if this bug is fixed or not. It wasn't "of course" because your code didn't show it, I never said anything else had an issue, other than my personal doubt that it's fixed (because it's been a bug for years), I simply asked if you checked it since it wasn't in the code (therefore I assumed you hadn't checked it - why do you think I asked). I then pointed out it wasn't "of course", since it wasn't as I just explained.
mklement0 commentedon Nov 14, 2023
I just installed the officially released
8.0.100version and I can confirm @iSazonov's findings: As far as I can tell, it does NOT work on macOS - neither in Terminal.app nor in popular third-party alternative iTerm2.app:Repro code (reproduces the symptom on macOS 13.5.2):
By contrast, adding the escape sequence that clears the scrollback buffer shown in the initial post works, in both terminal applications:
Shell alternatives:
Note:
/usr/bin/clearon macOS clears only the current screen, not the scrollback buffer.kasperk81 commentedon Nov 14, 2023
i'm on 14 (sonoma) and i don't see the numbers afterwards
hamarb123 commentedon Nov 14, 2023
Can confirm the following for Terminal.app:
We should fix this since macOS 10.15+ is supported for .NET 8.
adamsitnik commentedon Nov 15, 2023
The referenced fix (#88487) checks the terminfo for an
E3capability and uses it (not a hardcoded "\x1b[3J") when it's available.I would love to fix it for all terminals, but how can we do that reliably if they don't define this capability? Sharing https://unix.stackexchange.com/questions/375743/why-clear-do-not-clear-whole-screen/375784#375784 for reference.
mklement0 commentedon Nov 15, 2023
@adamsitnik: Given @hamarb123's summary above, there's a pragmatic solution:
Instead of:
runtime/src/libraries/System.Console/src/System/TerminalFormatStrings.cs
Lines 77 to 80 in fd4621c
use:
Windows is not affected, and I'm not aware of issues on Linux distros, so this should be the only workaround required.
Once .NET no longer supports macOS 13 and below, this workaround can be removed.
hamarb123 commentedon Nov 15, 2023
I think something like the above is a good solution. (
OperatingSystem.IsMacOS() && !OperatingSystem.IsMacOSVersionAtLeast(14)seems better suited though)Note that with dotnet/roslyn#70497, we should be able to even write
\e[3Jwith C# next :)If it concerns you that it might print something random out after it @adamsitnik, you could also re-append
TermInfo.WellKnownStrings.Clear(or something similar) to it again to ensure that a clear goes through for this workaround - it seems to me like this should work well in most cases.Ie, you could write:
(note: I've not tested the above actually does what it's intended to)
I can make a PR for it if you'd like @adamsitnik
adamsitnik commentedon Nov 16, 2023
The solution that you suggested assumes that every Terminal that runs on macOS supports
"\x1b[3J". I doubt it's always true.Don't get me wrong, I want to get all the bugs fixed. But in reliable way.
My current best idea is to recognize the Terminals that support it, but don't define that in Terminfo. Similarly to what we do in:
runtime/src/libraries/System.Console/src/System/TerminalFormatStrings.cs
Line 89 in 33d7f98
But I doubt that such a fix would be approved for backporting to .NET 8 (unless we can guarantee that older versions of this Terminals support it).
hamarb123 commentedon Nov 16, 2023
The point of the way I suggested doing it is assuming that they don't all support it. That's why I suggested a normal clear after. Would that not work?