Skip to content

[System.Console]::SetWindowPosition() fails to set cursor position #9018

Closed

Description

@nebula-it

Environment

Windows build number: 10.0.19042.0
Windows Terminal version (if applicable): 1.5.10271.0

Any other software? N/A

Steps to reproduce

Run [System.Console]::SetWindowPosition(0,[System.Console]::CursorTop) in terminal. Compare this to its run in regular powershell console.

Expected behavior

Running [System.Console]::SetWindowPosition(0,[System.Console]::CursorTop) should scroll the buffer to top. Detailed info available here as well: https://tommymaynard.com/clear-host-without-clearing-the-host-2014/

Actual behavior

.NET console class method "SetWindowPosition" fails to execute. In regular Powershell console, following command was used to scroll to top of the window (As an alternative to cls/Clear-Host, so buffer was not cleared only scrolled up). Confirmed it is still working there but in Terminal it fails with following error:

MethodInvocationException: Exception calling "SetWindowPosition" with "2" argument(s): "The window position must be set such that the current window size fits within the console's buffer, and the numbers must not be negative. (Parameter 'top')
Actual value was 2."

Using any other value than (0,0) causes this to fail. [System.Console]::SetWindowPosition(0,0) does succeed but does not scroll to top of the window as it does in regular powershell console.

P.S: This might not be a Terminal issue but would love to get it resolved.
and Thank you team for all the love you pour into Terminal, its easily my most used and most loved software on Windows along with PowerShell itself. Cheers

Activity

  1. ghost added
    Needs-Triage
    on Feb 3, 2021
  2. ghost added
    Needs-Tag-Fix
    on Feb 3, 2021
  3. 237dmitry commented on Feb 8, 2021

    @237dmitry

    "Ctrl+L"

  4. 237dmitry commented on Feb 8, 2021

    @237dmitry

    You can use esc-sequences as well.

    "`e[2J`e[H"
    
  5. nebula-it commented on Feb 9, 2021

    @nebula-it
    Author

    This is perfect, thanks 237dmitry. Just out of curiosity, is this documented somewhere?

  6. 237dmitry commented on Feb 9, 2021

    @237dmitry

    is this documented somewhere?

    You can see here. Not of all working in Windows Terminal but this is problem of all terminals

  7. zadjii-msft commented on Feb 23, 2021

    @zadjii-msft
    Member

    So this one is weird. You can't set the Terminal's buffer viewport with SetWindowPosition, because technically there's another buffer that's sitting between powershell and the Terminal. We call this buffer "conpty". The conpty buffer is exactly the size of the viewport, which is more like how other terminal applications (on other platforms behave). So when you try to set the viewport's top to 2, conpty rejects that API call, because the viewport can only even be at the top of the buffer. It's always at top=0.

    Like 237dmitry mentioned, you can use \x1b[2J to clear the "scrollback", the text above the viewport, and \x1b[H to move the cursor to 0,0. These are escape sequences, and we've got a big ol' list of the ones we support here. There's also a lot more details on using escape sequences instead of API calls in this doc.

  8. added
    Issue-Question
    Product-Terminal
    Resolution-Answered
    on Feb 23, 2021
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

    Issue-QuestionNeeds-Tag-FixNeeds-TriageProduct-TerminalResolution-Answered

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions