Skip to content

Make Clear-Host clear the scrollback buffer too on all platforms  #8606

Closed

Description

Update:

The decision in #8603 to rely on Console.Clear() amounts to inconsistent behavior across platforms, for no good reason (there's no native behavior that needs to be emulated; native behavior, if desired, is accessible via the external clear utility).

If there's consensus that the desired behavior is to consistently clear the screen and the scrollback buffer on all platforms, we have two options:

The original proposal below was written under the assumption that platform-dependent behavior was intended, and therefore suggested an opt-in mechanism.

Summary of the new feature/enhancement

Implement a switch named -All to direct Clear-Host to clear not only the current screen of terminal (console) hosts but to clear the scrollback buffer (the output history) as well.

Clear-Host now uses [Console]::Clear() (see #8603), which results in platform-specific behavior:

  • Unix-like platforms: clears the current screen (terminal-window content) only.
  • Windows: clears the current screen and the scroll-back buffer.

That is, also clearing the scrollback buffer is invariably implied on Windows, whereas it is currently unavailable on Unix-like platforms.

Implementing -All would allow users to at least opt into a consistent cross-platform experience, e.g. via a user-defined function such as function clsa { Clear-Host -All }.

Note: As before, this means that in the Windows console you do not have the option to selectively clear the current screen only, without erasing the scrollback buffer.

Proposed technical implementation details (optional)

If -All is specified:

  • on Windows: do nothing, because clearing the scrollback buffer is invariably performed anyway.
  • on Unix-like platforms, execute $host.ui.Write("`e[3J") after [Console]::Clear() to also clear the scrollback buffer.

Activity

  1. added
    Issue-Enhancementthe issue is more of a feature request than a bug
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Jan 8, 2019
  2. SteveL-MSFT commented on Jan 8, 2019

    @SteveL-MSFT
    Member

    The work to do this should be pretty minimal and straight forward. Remove Clear-Host as a function and reimplement as a C# cmdlet.

  3. dhulwells commented on Jan 8, 2019

    @dhulwells

    Can I take this one?

  4. SteveL-MSFT commented on Jan 8, 2019

    @SteveL-MSFT
    Member

    Dhul Wells (@dhulwells) consider it yours! If you have something basically working, you can always submit as a Work-In-Progress PR if you need help (just put WIP: as prefix to PR title)

  5. BrucePay commented on Jan 8, 2019

    @BrucePay
    Collaborator

    Steve Lee (@SteveL-MSFT) Michael Klement (@mklement0) Since Clear-Host on Windows already clears the scrollback buffer, this new switch -All would be a useless parameter on Windows resulting in either an error or it being silently ignored if used, neither of which seem undesirable.

    Note: As before, this means that in the Windows console you do not have the option to selectively clear the current screen only, without erasing the scrollback buffer.

    We can certainly implement this on Windows if desired, but I don't know that I've ever wanted it. YMMV.

    James Truher (@JamesWTruher) Joey Aiello (@joeyaiello) IIRC, the thrust of what we discussed in the committee meeting is that PowerShell would call Console.Clear() on all 'console' platforms and the CoreCLR team would implement the appropriate behaviour on each platform. If the CoreCLR isn't doing the right thing on a platform, then a bug should be opened in the CoreCLR repo. BUT(!) we do still need to go through the $Host.UI.RawUI APIs in order to support 'non-console' platforms like remoting. In fact, if the RawUI APIs were fully supported on non-Windows console hosts, this whole discussion might be moot.

  6. SteveL-MSFT commented on Jan 8, 2019

    @SteveL-MSFT
    Member

    Bruce Payette (@BrucePay) it could be argued (and I expect the corefx team to argue) that [Console]::Clear() has the correct behavior from their point of view today which is terminal specific. Personally, I prefer to have the scrollback buffer cleared as my typical use case is that I clear the screen so I can output a bunch of content and expect the buffer to only have the new content whereas retaining the buffer means I have to visually determine where the new content exists when I use my scroll wheel. Having -All being no-op on Windows doesn't seem bad to me.

  7. mklement0 commented on Jan 9, 2019

    @mklement0
    ContributorAuthor

    Personally, I prefer to have the scrollback buffer cleared as my typical use case is that I clear the screen so I can output a bunch of content and expect the buffer to only have the new content whereas retaining the buffer means I have to visually determine where the new content exists when I use my scroll wheel.

    Amen to that.

    So, from that perspective the best user experience would be to consistently clear the scrollback buffer too, on all platforms, as originally suggested in #8554.

    Your decision was to rely on [Console]::Clear(), which, I would argue, shouldn't exhibit platform-specific behavior either (and perhaps should offer a parameter for opting into screen-only clearing).

    So, yes, a CoreFx issue should be opened, but if the consensus is that we want buffer clearing on all platforms, we can certainly make that happen now without having to wait for [Console]::Clear() to be fixed.

  8. mklement0 commented on Jan 9, 2019

    @mklement0
    ContributorAuthor
  9. changed the title [-]Add a switch to Clear-Host to opt into clearing not just the current screen but also the scrollback buffer[/-] [+]Make Clear-Host clear the scrollback buffer too on all platforms [/+] on Jan 9, 2019
  10. SteveL-MSFT commented on Jan 9, 2019

    @SteveL-MSFT
    Member

    Michael Klement (@mklement0) I had to rollback the change from [Console]::Clear() back to $RawUI since [Console]::Clear() doesn't work remotely when there isn't a physical console (see #8609).

  11. iSazonov commented on Jan 10, 2019

    @iSazonov
    Collaborator

    From #8609 (comment)

    there's interest to convert this to a C# cmdlet and extend it, so let's defer any enhancements to that effort
    it's implicitly part of #8606 since the way Clear-Host is currently implemented it's impossible to extend

  12. SeeminglyScience commented on Jan 10, 2019

    @SeeminglyScience
    Contributor

    Is there any reason why these changes wouldn't go into ConsoleHost instead of Clear-Host? The host can decide what to do with the current SetBufferContents even if it doesn't really support the API. For example, PSES will invoke Console.Clear if the args look like it came from Clear-Host but will no-op if it's passed anything else.

    I realize that #8609 already changes back to using $Host for Windows, but is the expectation that custom hosts should replace Clear-Host for non-Windows?

  13. 19 remaining items

  14. removed
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Nov 19, 2019
  15. iSazonov commented on Nov 14, 2023

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) Console.Clear was fixed in .Net 8.0. Can we close the issue?

  16. mklement0 commented on Nov 14, 2023

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov), as of .NET 8.0.0-rc.2.23479.6 (the version underlying PowerShell 7.4.0-rc.1), it is not fixed on macOS.

    I'm unclear on whether the fix hasn't landed yet, or whether it isn't effective on macOS.

  17. iSazonov commented on Nov 14, 2023

    @iSazonov
    Collaborator
  18. mklement0 commented on Nov 14, 2023

    @mklement0
    ContributorAuthor

    Thanks, Ilya (@iSazonov) - indeed, as you've also found, it has not been fixed on macOS.

  19. microsoft-github-policy-service commented on May 13, 2024

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Interactive-Consolethe console experienceWaiting - DotNetCorewaiting on a fix/change in .NET

    Type

    No type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions