PowerShell / PSReadLine

A bash inspired readline implementation for PowerShell
BSD 2-Clause "Simplified" License
3.75k stars 296 forks source link

Newline Handling of [Microsoft.PowerShell.PSConsoleReadLine]::Insert broken in Windows #3939

Open Callidus2000 opened 9 months ago

Callidus2000 commented 9 months ago

Prerequisites

Exception report

N/A

Screenshot

image

image

Environment data

PS Version: 7.3.11 and 7.4
PS HostName: ConsoleHost
PSReadLine Version: 2.3.4
PSReadLine EditMode: Windows
OS: 10.0.19041.1682 (WinBuild.160101.0800)
BufferWidth: 120
BufferHeight: 9001

Steps to reproduce

I've developed a module (CmdFav) which inserts commands into the NEXT prompt. If the command is a multiline command the command string is internally build with StringBuilder.AppendLine(). StringBuilder is using Windows' carriage return + new line (`r`n) instead of just new line (`n). This causes PSConsoleReadLine::Insert to insert visible ^M characters (orange arrow in screenshot) and moves the cursor additional lines to low (red marker in screenshot).

Code to reproduce:

$sb=[System.Text.StringBuilder]::new()
[void]$sb.AppendLine('write-host "Hello"').AppendLine('write-host "World"')
$global:Suggestion = $sb.ToString()
$global:InjectCommand = Register-EngineEvent -SourceIdentifier PowerShell.OnIdle {
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert($global:Suggestion)
    Stop-Job $global:InjectCommand
}

The 'too low cursor' prevents the user to modify the code which is inserted into the prompt in the blank space. If I move the cursor into the blank space and (e.g.) press + the real code is shown, interrupted by the + char.

Expected behavior

Actual behavior

Jaykul commented 9 months ago

I do think PSReadLIne should probably convert `r`n to `n when it's passed to Insert

The offset problem only manifests when it's being called like this in the OnIdle.

Compare these:

$global:HistoricalLines = (Get-History -Count 3).CommandLine -join "`n"

function Add-History {
  $global:InjectCommand = Register-EngineEvent -SourceIdentifier PowerShell.OnIdle {
      [Microsoft.PowerShell.PSConsoleReadLine]::Insert($HistoricalLines )
      Stop-Job $global:InjectCommand
  }
}

Set-PSReadLineKeyHandler -Key Alt+h -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::Insert($HistoricalLines)
}

The key handler works exactly how I expected it to, but the OnIdle handler ends up offset by ... however lines there were in the combined history.