Skip to content

Powershell Core OSC9;9 string leaves residue in prompt after using grep from git-for-windows with pipe #425

Closed

Description

@3N4N

Output of the bug

PS C:\Users\ACER> dir | grep 'pattern'           # no match
PS C:\Users\ACER> ←]9;9;"C:\Users\ACER"
PS C:\Users\ACER>
PS C:\Users\ACER> dir | grep 'Documents'     # matches
d-r--           10/9/2021 11:04 PM                Documents
PS C:\Users\ACER> ←]9;9;"C:\Users\ACER"
PS C:\Users\ACER>
PS C:\Users\ACER> grep -HnrIi 'damn' .\Documents\PowerShell\        # to show that without piping the bug doesn't appear
.\Documents\PowerShell\/Scripts/scratch.ps1:1:function Damn([string]$sym)
PS C:\Users\ACER>
PS C:\Users\ACER> dir | win32yank.exe -i  # to show that the problem is not with piping
PS C:\Users\ACER>

Reproduction steps

  1. Put the following in the $PROFILE.
    function prompt
    {
    $loc = Get-Location
    
    # Emulate standard PS prompt with location followed by ">"
    $out = "PS $loc> "
    
    # Add current working directory (FileSystem)
    if ($loc.Provider.Name -eq "FileSystem") {
        $out += "$([char]27)]9;9;`"$($loc.Path)`"$([char]7)"
    }
    
    return $out
    }
  2. Open a new PWSH tab in windows terminal.
  3. Make sure git is in the $env:path and invoke:
    dir | grep -i "pattern"

Versions of tools used

  • Windows Terminal: 1.10.2714.0 (why does wt -v not print in the console? I cannot even copy from the popup window.)
  • PowerShell Core: PowerShell 7.1.4
  • GIt for Windows: git version 2.33.0.windows.2

Discussions elsewhere

I mentioned in another issue -- may or may not be related -- that Windows Terminal crashes randomly if I use OSC9;9 string in the prompt. But I've been using the preview build for a week and it hasn't crashed yet.

Then tbbie pointed me to some other OSC9;9 string setups and when that didn't work he asked me to get the reproducible steps and create an issue. So here it is.

Activity

  1. changed the title [-]Powershell Core OSC9;9 string leaves residue in prompt after using grep with pipe from git-for-windows[/-] [+]Powershell Core OSC9;9 string leaves residue in prompt after using grep from git-for-windows with pipe[/+] on Oct 10, 2021
  2. 3N4N commented on Oct 10, 2021

    @3N4N
    Author

    I may have found where the problem was. Get-Location actually returns an object. So, $loc wasn't really a string variable.

    I have now changed it to (Get-Location).Path and the bug seems to be fixed.

    - $loc = Get-Location
    + $loc = (Get-Location).Path

    Leaving the issue open for confirmation that the resolution makes sense.

    EDIT: Never mind. Without $loc as Path object, the reason for using OSC9;9 -- new splits and tabs in current directory -- doesn't work. So, it's not a solution.

  3. added a commit that references this issue on Oct 10, 2021
  4. TBBle commented on Oct 18, 2021

    @TBBle
    Contributor

    In the posted script, $loc shouldn't be a string, because when it's used in the OCS 9;9 string, the Path is extracted from it.

    $out += "$([char]27)]9;9;`"$($loc.Path)`"$([char]7)"
    

    Anyway, I can't trivially replicate this locally, using that prompt script (or my own prompt via PowerLine).

    image

    I also confirmed that OSC9;9 was working, as my duplicate-tab command following cd C:\Projects started correctly in that directory.

    That's Windows Terminal 1.10.2714.0, PowerShell 7.1.5, and Git for Windows 2.30.2.windows.1. So not precisely the same versions, but close enough that it's not an inherent WT failure.

    One thing to try, see if this makes a difference:

    function prompt
    {
    $out = ""
    
    $loc = Get-Location
    
    # Add current working directory (FileSystem)
    if ($loc.Provider.Name -eq "FileSystem") {
        $out += "$([char]27)]9;9;`"$($loc.Path)`"$([char]7)"
    }
    
    # Emulate standard PS prompt with location followed by ">"
    $out += "PS $loc> "
    
    return $out
    }
    

    i.e. make sure that the BEL character being used to terminate the OSC command isn't the last character in the prompt.

    You could also try

    function prompt
    {
    $loc = Get-Location
    
    # Emulate standard PS prompt with location followed by ">"
    $out = "PS $loc> "
    
    # Add current working directory (FileSystem)
    if ($loc.Provider.Name -eq "FileSystem") {
        $out += "$([char]27)]9;9;`"$($loc.Path)`"$([char]27)\"
    }
    
    return $out
    }
    

    i.e. terminate the OSC command with the ST sequence (Esc+\) rather than BEL, in case something is incorrectly stripping the BEL there.

  5. mattwojo commented on Dec 9, 2021

    @mattwojo
    Contributor

    Closing -- this issue isn't related to a docs update, but should be reopened in the Terminal product repo: https://github.com/Microsoft/terminal
    Thank you!

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions