diff --git a/GitPrompt.ps1 b/GitPrompt.ps1 index 574685731..6f725e09e 100644 --- a/GitPrompt.ps1 +++ b/GitPrompt.ps1 @@ -37,6 +37,9 @@ $global:GitPromptSettings = New-Object PSObject -Property @{ UntrackedText = ' !' UntrackedForegroundColor = [ConsoleColor]::DarkRed UntrackedBackgroundColor = $Host.UI.RawUI.BackgroundColor + + ErrorForegroundColor = [ConsoleColor]::Red + ErrorBackgroundColor = $Host.UI.RawUI.BackgroundColor ShowStatusWhenZero = $true @@ -167,12 +170,32 @@ function Write-GitStatus($status) { if(!(Test-Path Variable:Global:VcsPromptStatuses)) { $Global:VcsPromptStatuses = @() } -function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } } + +function Global:Write-VcsStatus { + $Global:VcsPromptStatuses | foreach { + $vcsPromptErrors = $null + try { + Invoke-Command -ScriptBlock $_ -ErrorVariable vcsPromptErrors 2>$null + } + catch { + $vcsPromptErrors = $_ + } + if ($vcsPromptErrors.Length -gt 0) { + $vcsPromptErrors | % { Write-Error -ErrorRecord $_ -ErrorAction SilentlyContinue } + $s = $Global:GitPromptSettings + if ($s) { + Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor + Write-Prompt "Error" -BackgroundColor $s.ErrorBackgroundColor -ForegroundColor $s.ErrorForegroundColor + Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor + } + } + } +} # Add scriptblock that will execute for Write-VcsStatus $PoshGitVcsPrompt = { $Global:GitStatus = Get-GitStatus Write-GitStatus $GitStatus -} +} $Global:VcsPromptStatuses += $PoshGitVcsPrompt $ExecutionContext.SessionState.Module.OnRemove = { $Global:VcsPromptStatuses = $Global:VcsPromptStatuses | ? { $_ -ne $PoshGitVcsPrompt} } \ No newline at end of file