Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Adding try/catch logic to Write-VcsStatus #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 22 additions & 1 deletion HgPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,28 @@ function Write-HgStatus($status = (get-hgStatus $global:PoshHgSettings.GetFileSt
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) {
# Log the errors but dont affect the users prompt, splat error objects into Write-Error
$vcsPromptErrors | % { Write-Error -ErrorAction SilentlyContinue @_ }
$s = $Global:PoshHgSettings
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
$Global:VcsPromptStatuses += {
Expand Down
6 changes: 5 additions & 1 deletion Settings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ $global:PoshHgSettings = New-Object PSObject -Property @{
UnappliedPatchBackgroundColor = $Host.UI.RawUI.BackgroundColor
AppliedPatchForegroundColor = [ConsoleColor]::DarkYellow
AppliedPatchBackgroundColor = $Host.UI.RawUI.BackgroundColor
PatchSeparator = ' '
PatchSeparator = ' '
PatchSeparatorColor = [ConsoleColor]::White

# Error customisation
ErrorForegroundColor = [ConsoleColor]::Red
ErrorBackgroundColor = $Host.UI.RawUI.BackgroundColor

# Current revision
ShowRevision = $true
Expand Down