Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding try/catch logic to Write-VcsStatus #170

Merged
merged 1 commit into from
Mar 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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} }