Skip to content

Instantly share code, notes, and snippets.

@cspotcode
Last active August 27, 2018 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cspotcode/f45c9d92412becbe9201c8de63e4ff98 to your computer and use it in GitHub Desktop.
Save cspotcode/f45c9d92412becbe9201c8de63e4ff98 to your computer and use it in GitHub Desktop.
#Thread-safe data structure to store prompt strings and communicate about state
$prompt = [Collections.Concurrent.ConcurrentDictionary[String,String]]@{}
$prompt.infix = ''
Set-PSReadlineOption -ExtraPromptLineCount 1 # For 2-line prompt
Register-EngineEvent -SourceIdentifier PowerShell.OnIdle -Action {
if($prompt.state -eq 'rerender') {
$prompt.isIdleRender = 'yes'
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
$prompt.isIdleRender = 'no'
}
} | out-null
# Load multithreading library (https://github.com/cspotcodeMadMultithreading)
import-module 'C:\Users\abradley\Documents\Personal-dev\@TimCurwick\MadMultithreading\Supplemental.psm1'
Function __promptThreadInit {
Import-Module posh-git
$GitPromptSettings.AnsiConsole = $true
}
Function __promptThreadAction($prompt) {
process {
cd $_
$prompt.infix = Write-VCSStatus
$prompt.state = 'rerender'
}
}
# Create persistent background thread to compute status
$promptThreadpool = New-MadThreadPool -Threads 1 -InitializeFunction (Get-Command __promptThreadInit)
Function Prompt {
# If invoked via OnIdle event, refresh existing prompt, don't do a new one
if($prompt.isIdleRender -eq 'yes') {
$prompt.state = 'done'
return $prompt.prefix + $prompt.infix + $prompt.suffix
}
# Compute prompt infix in background thread
$prompt.state = 'waiting'
Invoke-InMadThreadPool -Threadpool $promptThreadpool -function (Get-Command __promptThreadAction) -InputObject $PWD.Path -Parameters @{prompt = $prompt} -NoWaitForResults
# Mostly-normal prompt logic. Infix will carry over from previous prompt and be updated async via onidle event
$prompt.prefix = $PWD.Path
$prompt.suffix = "`n> "
return $prompt.prefix + prompt.infix + $prompt.suffix
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment