kelleyma49 / PSFzf

A PowerShell wrapper around the fuzzy finder fzf
MIT License
787 stars 34 forks source link

Special characters are replaced with question marks after PSFzf runs (but not after fzf by itself) #71

Open jmoore34 opened 3 years ago

jmoore34 commented 3 years ago

When PSFzf is used with a powerline (e.g. oh-my-posh), the special characters are replaced with question marks after fzf runs.

image

Reproduction steps:

  1. Install oh-my-posh and choose a theme that uses special characters
  2. Invoke PSFzf, and then make a selection or close fzf

Tested on latest version (2.2.6)

Additional note: This issue does not occur when fzf is called manually from the command line

kelleyma49 commented 3 years ago

Unfortunately this is due to a workaround when fzf is invoked from a PSReadline handler. I have to call PSReadline's InvokePrompt to redraw the prompt as PSReadline won't automatically redraw the prompt after a child process modifies the text buffer. I'm not sure how to fix this one, unless this is fixed in PSReadline.

segevfiner commented 2 years ago

Should someone file this bug against PSReadline then?

segevfiner commented 2 years ago

Submitted https://github.com/PowerShell/PSReadLine/issues/2866

segevfiner commented 2 years ago

I worked around this locally by adding:

function script:InvokePrompt()
{
    $previousOutputEncoding = [Console]::OutputEncoding
    [Console]::OutputEncoding = [Text.Encoding]::UTF8

    try {
        [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
    } finally {
        [Console]::OutputEncoding = $previousOutputEncoding
    }
}

And calling that instead of calling [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() directly. Not sure if this is something you will actually want in a pull request..., I said the same about this in https://github.com/PowerShell/PSReadLine/issues/2866#issuecomment-960788773, but who knows if it will ever get any attention... 🤷‍♂️

P.S. Does adding script: do anything? Isn't that already the default scope of functions defined at the script level?

I also set Set-PSReadLineOption -ExtraPromptLineCount 2 since it seems to redraw the prompt better that way. (Using starship prompt)

kelleyma49 commented 2 years ago

Thank for for the investigation! I added your workaround and tested it locally. it looks great!

I created a prerelease version with your change: https://www.powershellgallery.com/packages/PSFzf/2.3.1-alpha