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

PowerShell Snippet adds unwanted Backslash Character to Prompt #597

Closed
KenADev opened this issue Oct 6, 2022 · 4 comments · Fixed by #689
Closed

PowerShell Snippet adds unwanted Backslash Character to Prompt #597

KenADev opened this issue Oct 6, 2022 · 4 comments · Fixed by #689
Assignees
Labels
help wanted Extra attention is needed

Comments

@KenADev
Copy link

KenADev commented Oct 6, 2022

In the section PowerShell: powershell.exe or pwsh.exe, this code snippet is shown:

function prompt {
  $loc = $($executionContext.SessionState.Path.CurrentLocation);
  $out = "PS $loc$('>' * ($nestedPromptLevel + 1)) ";
  $out += "$([char]27)]9;9;`"$loc`"$([char]27)\"
  return $out
}

When I put it in my PowerShell Profile and open a new PowerShell window, an unwanted backslash character is added to the command prompt when typing. (In pure PowerShell and also in Windows Terminal)
It is shown, but not interpreted.

Example:
PS C:\Users\xxx\somedir>\$x=

The backslash appears as soon as the equal sign is typed, but also (seemingly) randomly.
I first thought the backslash in this line was at fault, but removing it causes an exception:
$out += "$([char]27)]9;9;`"$loc`"$([char]27)\"

Last 4 keys:
 e Backspace d o

Exception:
System.ArgumentOutOfRangeException: StartIndex must not be lower than zero
Parametername: startIndex
   bei System.String.Substring(Int32 startIndex, Int32 length)
   bei Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor)
   bei Microsoft.PowerShell.PSConsoleReadLine.ForceRender()
   bei Microsoft.PowerShell.PSConsoleReadLine.Insert(Char c)
   bei Microsoft.PowerShell.PSConsoleReadLine.SelfInsert(Nullable`1 key, Object arg)
   bei Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
   bei Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   bei Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)

Windows Terminal Version: 1.15.2712.0
$PSVersionTable:

Name                           Value
----                           -----
PSVersion                      5.1.19041.1682
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1682
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
---

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

@mattwojo mattwojo self-assigned this Nov 3, 2022
@mattwojo
Copy link
Collaborator

mattwojo commented Nov 3, 2022

Hmm... this is a weird issue. @zadjii-msft -- any thoughts?

@mattwojo mattwojo added the help wanted Extra attention is needed label Nov 3, 2022
@zadjii-msft
Copy link
Contributor

Yea I need to update that snippet. Clearly, I don't use pwsh as my daily driver.

I have a much more elaborate pwsh prompt I've been testing lately. I need to take this, and boil it down:

  $loc = $($executionContext.SessionState.Path.CurrentLocation);
  # IMPORTANT: Make sure there's a printable charater _last_ in the prompt.
  # Otherwise, PSReadline is gonna use the terminating `\` here and colorize
  # that if it detects a syntax error
  $out += "`e]133;A$([char]07)";
  $out += "`e]9;9;`"$loc`"$([char]07)";
  $out += "PWSH $loc$('>' * ($nestedPromptLevel + 1)) ";
  $out += "`e]133;B$([char]07)";

  $Global:__LastHistoryId = $LastHistoryEntry.Id

  return $out
  

All the e]133; sequences are prompt markup sequences which aren't released yet, but the 9;9 stuff should be relevant

@mattwojo
Copy link
Collaborator

@zadjii-msft - Just wondering about this snippet issue... should I update the doc with your code above? Or have you had a chance to try boiling it down? Would love to get this resolved when you have a spare moment.

@mattwojo mattwojo assigned zadjii-msft and unassigned mattwojo Mar 11, 2023
@yan12125
Copy link
Contributor

This looks like a PowerShell issue, as I can reproduce it even on Linux (Konsole). See PowerShell/PSReadLine#3719.

nguyen-dows pushed a commit that referenced this issue Aug 31, 2023
PSReadLine cuts a substring since the last non-whitespace character in
the prompt as the error prompt [1], which is displayed when there are
syntax errors. As a result, when the prompt ends in `$([char]27)\`, as
in the previous example, an unexpected backslash may appear.

See: PowerShell/PSReadLine#3719

[1]
https://github.com/PowerShell/PSReadLine/blob/v2.2.6/PSReadLine/ReadLine.cs#L819.

---

Closes #597
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants