Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to async update the PowerShell prompt?

Tags:

powershell

I like to include the Mercurial/Git repo status in my PowerShell prompt. Sometimes this slows down the prompt() function considerably, causing a long wait until you can start typing in the shell. I'm wondering, is it possible to update the prompt async?

Ideally it would:

  1. Write-Host the current directory (typical prompt)
  2. Start collecting repo status in the background.
  3. When repo status is collected, assuming the prompt hasn't been called again, insert the repo status into the prompt.

Has anyone done this before?

like image 768
joshuapoehls Avatar asked May 31 '12 19:05

joshuapoehls


1 Answers

Not per se.

You can't update the prompt asynchronously because the prompt just returns text, and it's called in a host whenever a top-level command is done.

You could rewrite prompt to do something like check a data source, or a variable which you update asynchronously. So the prompt wouldn't keep changing while the command is running, but it won't hang for a while because it will just be looking up data.

The more tricky thing of this is that it gets into the game of trying to peek at execution results after the fact. PowerShell doesn't keep them around unless they're assigned to something. So to do this right one must override Out-Default (which only works well in the ISE, PowerGUI, etc, not powershell.exe). Overriding this isn't so hard, but it requries knowing a lot of PowerShell voodoo, even by my impressive standards.

On the bright side, I've obviously been thinking this thru a lot, and have a module that does a lot of this, I'm just determining what I'm going to do with it and if it stays in its own module or joins one of the other modules we build.

I'll update this when out, but this roadmap should help considerably.

The short answer is that you cannot update the prompt async, but you can make it darn fast, and the tricks required to do so open up many cooler doors.

Hope this helps

like image 91
Start-Automating Avatar answered Oct 29 '22 16:10

Start-Automating