Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
Callidus2000 opened this issue Feb 2, 2024 · 1 comment
Open
3 tasks done
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation.

Comments

@Callidus2000
Copy link

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues, especially the pinned issues.

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

  • No ^M visible
  • No blank lines
  • Inserted code should be editable in the right space

Actual behavior

  • Multiline text is postfixed with a ^M
  • After the inserted text additional blank lines are inserted, the cursor is not at the right spot
  • The effective inserted text is hidden in the blank lines and has to be edited there instead in the directly visible part
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Feb 2, 2024
@daxian-dbw daxian-dbw added Issue-Bug It either shouldn't be doing this or needs an investigation. and removed Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. labels Feb 2, 2024
@Jaykul
Copy link

Jaykul commented Feb 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation.
Projects
None yet
Development

No branches or pull requests

3 participants