Skip to content

There's no reliable way to read and suppress terminal output #10327

@Jaykul

Description

@Jaykul

When dealing with VT terminals I need to be able to Read-Host from the terminal input, without waiting for {Enter}, and on Windows, we use $Host.UI.RawUI.ReadKey("NoEcho") for that, but it's failing elsewhere.

Steps to reproduce

On Windows, the following allows me to read responses from the terminal (without having them show up):

function Read-Terminal {
[CmdletBinding()]
param([string]$EscSequence)
  $Host.UI.Write($EscSequence)
  -join $(while ($Host.UI.RawUI.KeyAvailable) { 
    $Host.UI.RawUi.ReadKey("NoEcho,IncludeKeyDown").Character
  })
} 

So for instance, the CSI code 6n causes the terminal to report the cursor location, and we can read our X, Y from the terminal like this:

$answer = Read-Terminal "`e[6n"
"Row {0}, Column {1}" -f @($answer -split ';' -replace '\D')

Will get something like:

Row 29, Column 1"

Actual behavior

But when I try to run the same code on linux (even in the WSL terminal) the $Host.UI.RawUI seems to echo no matter what I do, so the terminal's response is completely unsuppressable.

PS /root> cls; $answer = Read-Terminal "`e[6n"
^[[1;1R

I would be remiss not to point out that in addition to this being unreadable, the escape sequence is being shown as ^[ instead of the expected `e

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.2
PSEdition                      Core
GitCommitId                    6.2.2
OS                             Linux 4.4.0-18362-Microsoft #1-Microsoft Mon Mar 18 12:02:00 PST 2019
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aOS-LinuxResolution-No ActivityIssue has had no activity for 6 months or moreWG-Interactive-Consolethe console experience

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions