PSReadLine: Make the output of script block handler formatted and rendered to the console?

When using programs(less, fzf etc.), that emit to alternate buffers, inside ScriptBlock, there is no display of the program buffer. For Eg:

Set-PSReadLineKeyHandler -Chord Ctrl+o -ScriptBlock { less foo.txt }
Set-PSReadLineKeyHandler -Chord Ctrl+o -ScriptBlock { find . | fzf }

Show no output

Couple of questions a) Why is that? b) What should I do if I need to display them? (Start separate process and redirect stdout from that process?)

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (11 by maintainers)

Most upvoted comments

Hmm, PSFzf doesn’t want to play nice for me under linux under PowerShell Core 7 and I think that it would be awesome if interactive scripts could be invoked using hotkeys, so I would appreciate if this issue could be solved in a way that doesn’t require external tools.

Edit: Hmm, it seems like I’ve managed to solve it for now, but this doesn’t seem to be the cleanest workaround:

Set-PSReadLineKeyHandler -Chord Ctrl+f -ScriptBlock {
  # Start-Process -FilePath fzf -Wait

  $history = Get-Content -Raw (Get-PSReadLineOption).HistorySavePath

  $p = [System.Diagnostics.Process]@{StartInfo = @{
    FileName = "fzf";
    Arguments = "--tac --no-sort --bind tab:toggle-sort --height `"25%`"";
    RedirectStandardInput = $true;
    RedirectStandardOutput = $true;
  }}

  $p.Start()
  $p.StandardInput.Write($history)
  $stdout = $p.StandardOutput.ReadToEnd()
  $p.WaitForExit()

  [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
  [Microsoft.PowerShell.PSConsoleReadLine]::Insert($stdout.Trim())
}


Yes, I guess I was hinting that you’d have to return, but a function like AcceptAndGetNext could be used to restore the current input.