diff --git a/HgPrompt.ps1 b/HgPrompt.ps1 index 21ff3a6..48cec67 100644 --- a/HgPrompt.ps1 +++ b/HgPrompt.ps1 @@ -1,5 +1,5 @@ # For backwards compatibility -$global:HgPromptSettings = $global:PoshHgSettings +$global:HgPromptSettings = $global:VcsStatusSettings function Write-Prompt($Object, $ForegroundColor, $BackgroundColor = -1) { if ($BackgroundColor -lt 0) { @@ -9,75 +9,70 @@ function Write-Prompt($Object, $ForegroundColor, $BackgroundColor = -1) { } } -function Write-HgStatus($status = (get-hgStatus $global:PoshHgSettings.GetFileStatus $global:PoshHgSettings.GetBookmarkStatus)) { - if ($status) { - $s = $global:PoshHgSettings +function Write-HgStatus($status = (get-hgStatus $global:VcsStatusSettings.EnableFileStatus $global:VcsStatusSettings.GetBookmarkStatus)) { + if ($status -and $global:VcsStatusSettings.EnablePromptStatus) { + $s = $global:VcsStatusSettings $branchFg = $s.BranchForegroundColor $branchBg = $s.BranchBackgroundColor if($status.Behind) { - $branchFg = $s.Branch2ForegroundColor - $branchBg = $s.Branch2BackgroundColor + $branchFg = $s.BranchBehindForegroundColor + $branchBg = $s.BranchBehindBackgroundColor } if ($status.MultipleHeads) { - $branchFg = $s.Branch3ForegroundColor - $branchBg = $s.Branch3BackgroundColor + $branchFg = $s.BranchBeheadForegroundColor + $branchBg = $s.BranchBeheadBackgroundColor } Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor Write-Prompt $status.Branch -BackgroundColor $branchBg -ForegroundColor $branchFg - if($status.Added) { - Write-Prompt "$($s.AddedStatusPrefix)$($status.Added)" -BackgroundColor $s.AddedBackgroundColor -ForegroundColor $s.AddedForegroundColor + if($s.ShowStatusWhenZero -or $status.Added) { + Write-Prompt "$($s.AddedStatusPrefix)$($status.Added)" -BackgroundColor $s.AddedLocalBackgroundColor -ForegroundColor $s.AddedLocalForegroundColor } - if($status.Modified) { - Write-Prompt "$($s.ModifiedStatusPrefix)$($status.Modified)" -BackgroundColor $s.ModifiedBackgroundColor -ForegroundColor $s.ModifiedForegroundColor + if($s.ShowStatusWhenZero -or $status.Modified) { + Write-Prompt "$($s.ModifiedStatusPrefix)$($status.Modified)" -BackgroundColor $s.ModifiedLocalBackgroundColor -ForegroundColor $s.ModifiedLocalForegroundColor } - if($status.Deleted) { - Write-Prompt "$($s.DeletedStatusPrefix)$($status.Deleted)" -BackgroundColor $s.DeletedBackgroundColor -ForegroundColor $s.DeletedForegroundColor + if($s.ShowStatusWhenZero -or $status.Deleted) { + Write-Prompt "$($s.DeletedStatusPrefix)$($status.Deleted)" -BackgroundColor $s.DeletedLocalBackgroundColor -ForegroundColor $s.DeletedLocalForegroundColor } - - if ($status.Untracked) { - Write-Prompt "$($s.UntrackedStatusPrefix)$($status.Untracked)" -BackgroundColor $s.UntrackedBackgroundColor -ForegroundColor $s.UntrackedForegroundColor + if($s.ShowStatusWhenZero -or $status.Untracked) { + Write-Prompt "$($s.UntrackedStatusPrefix)$($status.Untracked)" -BackgroundColor $s.UntrackedLocalBackgroundColor -ForegroundColor $s.UntrackedLocalForegroundColor } - - if($status.Missing) { - Write-Prompt "$($s.MissingStatusPrefix)$($status.Missing)" -BackgroundColor $s.MissingBackgroundColor -ForegroundColor $s.MissingForegroundColor + if($s.ShowStatusWhenZero -or $status.Missing) { + Write-Prompt "$($s.MissingStatusPrefix)$($status.Missing)" -BackgroundColor $s.MissingLocalBackgroundColor -ForegroundColor $s.MissingLocalForegroundColor } - - if($status.Renamed) { - Write-Prompt "$($s.RenamedStatusPrefix)$($status.Renamed)" -BackgroundColor $s.RenamedBackgroundColor -ForegroundColor $s.RenamedForegroundColor + if($s.ShowStatusWhenZero -or $status.Renamed) { + Write-Prompt "$($s.RenamedStatusPrefix)$($status.Renamed)" -BackgroundColor $s.RenamedLocalBackgroundColor -ForegroundColor $s.RenamedLocalForegroundColor } if($s.ShowTags -and ($status.Tags.Length -or $status.ActiveBookmark.Length)) { - write-host $s.BeforeTagText -NoNewLine + Write-Prompt $s.TagPrefix -ForegroundColor $s.TagForegroundColor -BackgroundColor $s.TagBackgroundColor if($status.ActiveBookmark.Length) { Write-Prompt $status.ActiveBookmark -ForegroundColor $s.BranchForegroundColor -BackgroundColor $s.TagBackgroundColor if($status.Tags.Length) { - Write-Prompt " " -ForegroundColor $s.TagSeparatorColor -BackgroundColor $s.TagBackgroundColor + Write-Prompt " " -ForegroundColor $s.TagSeparatorForegroundColor -BackgroundColor $s.TagBackgroundColor } } $tagCounter=0 $status.Tags | % { - $color = $s.TagForegroundColor - - Write-Prompt $_ -ForegroundColor $color -BackgroundColor $s.TagBackgroundColor - - if($tagCounter -lt ($status.Tags.Length -1)) { - Write-Prompt ", " -ForegroundColor $s.TagSeparatorColor -BackgroundColor $s.TagBackgroundColor - } - $tagCounter++; + Write-Prompt $_ -ForegroundColor $s.TagForegroundColor -BackgroundColor $s.TagBackgroundColor + + if($tagCounter -lt ($status.Tags.Length -1)) { + Write-Prompt $s.TagSeparator -ForegroundColor $s.TagSeparatorForegroundColor -BackgroundColor $s.TagBackgroundColor + } + $tagCounter++; } } if($s.ShowPatches) { $patches = Get-MqPatches if($patches.All.Length) { - write-host $s.BeforePatchText -NoNewLine + write-host $s.PatchPrefix -NoNewLine $patchCounter = 0 @@ -104,7 +99,7 @@ function Write-HgStatus($status = (get-hgStatus $global:PoshHgSettings.GetFileSt } # Should match https://github.com/dahlbyk/posh-git/blob/master/GitPrompt.ps1 -if((Get-Variable -Scope Global -Name VcsPromptStatuses -ErrorAction SilentlyContinue) -eq $null) { +if(!(Test-Path Variable:Global:VcsPromptStatuses)) { $Global:VcsPromptStatuses = @() } function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } } @@ -113,3 +108,5 @@ function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } } $Global:VcsPromptStatuses += { Write-HgStatus } +# but we don't want any duplicate hooks (if people import the module twice) +$Global:VcsPromptStatuses = $Global:VcsPromptStatuses | Select -Unique \ No newline at end of file diff --git a/HgUtils.ps1 b/HgUtils.ps1 index f654d9a..b1905e0 100644 --- a/HgUtils.ps1 +++ b/HgUtils.ps1 @@ -7,7 +7,14 @@ function isHgDirectory() { return $true; } - + # Support blacklist + foreach ($repo in $Global:VcsStatusSettings.RepositoriesInWhichToDisableFileStatus) + { + if ($Pwd -like "$repo*") { + return $false + } + } + # Test within parent dirs $checkIn = (Get-Item .).parent @@ -23,7 +30,8 @@ function isHgDirectory() { return $false } -function Get-HgStatus($getFileStatus=$true, $getBookmarkStatus=$true) { +function Get-HgStatus($EnableFileStatus=$true, $getBookmarkStatus=$true) { + if(isHgDirectory) { $untracked = 0 $added = 0 @@ -36,7 +44,7 @@ function Get-HgStatus($getFileStatus=$true, $getBookmarkStatus=$true) { $behind = $false $multipleHeads = $false - if ($getFileStatus -eq $false) { + if ($EnableFileStatus -eq $false) { hg parent | foreach { switch -regex ($_) { 'tag:\s*(.*)' { $tags = $matches[1].Replace("(empty repository)", "").Split(" ", [StringSplitOptions]::RemoveEmptyEntries) } diff --git a/Settings.ps1 b/Settings.ps1 index 78b8441..948c304 100644 --- a/Settings.ps1 +++ b/Settings.ps1 @@ -1,65 +1,206 @@ -$global:PoshHgSettings = New-Object PSObject -Property @{ - #Retreival settings - GetFileStatus = $true - GetBookmarkStatus = $true - +# Inspired by Mark Embling +# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration +function Set-VcsStatusSettings { + [CmdletBinding()] +param( + [ConsoleColor]$DefaultForegroundColor = $Host.UI.RawUI.ForegroundColor, + [ConsoleColor]$DefaultBackgroundColor = $Host.UI.RawUI.BackgroundColor, + + # Retrieval settings + [Switch]$EnablePromptStatus = !$Global:GitMissing, + [Switch]$EnableFileStatus = $true, + [Switch]$ShowStatusWhenZero = $true, + [String[]]$RepositoriesInWhichToDisableFileStatus = @( ), # Array of repository paths + #Before prompt - BeforeText = ' [' - BeforeForegroundColor = [ConsoleColor]::Yellow - BeforeBackgroundColor = $Host.UI.RawUI.BackgroundColor - + [String]$BeforeText = ' [', + [ConsoleColor]$BeforeForegroundColor = $([ConsoleColor]::Yellow), + [ConsoleColor]$BeforeBackgroundColor = $DefaultBackgroundColor, + #After prompt - AfterText = ']' - AfterForegroundColor = [ConsoleColor]::Yellow - AfterBackgroundColor = $Host.UI.RawUI.BackgroundColor - - # Current branch - BranchForegroundColor = [ConsoleColor]::Cyan - BranchBackgroundColor = $Host.UI.RawUI.BackgroundColor + [String]$AfterText = '] ', + [ConsoleColor]$AfterForegroundColor = $([ConsoleColor]::Yellow), + [ConsoleColor]$AfterBackgroundColor = $DefaultBackgroundColor, + + # Branches + [ConsoleColor]$BranchForegroundColor = $([ConsoleColor]::Cyan), + [ConsoleColor]$BranchBackgroundColor = $DefaultBackgroundColor, # Current branch when not updated - Branch2ForegroundColor = [ConsoleColor]::Red - Branch2BackgroundColor = $host.UI.RawUI.BackgroundColor - # Current branch when there are multiple heads - Branch3ForegroundColor = [ConsoleColor]::Magenta - Branch3BackgroundColor = $host.UI.RawUI.BackgroundColor + [ConsoleColor]$BranchBehindForegroundColor = $([ConsoleColor]::Red), + [ConsoleColor]$BranchBehindBackgroundColor = $DefaultBackgroundColor, + # Current branch when we're both + [ConsoleColor]$BranchBeheadForegroundColor = $([ConsoleColor]::Magenta), + [ConsoleColor]$BranchBeheadBackgroundColor = $DefaultBackgroundColor, + + # Working DirectoryColors + [String]$AddedStatusPrefix = ' +', + [ConsoleColor]$AddedLocalForegroundColor = $([ConsoleColor]::Green), + [ConsoleColor]$AddedLocalBackgroundColor = $DefaultBackgroundColor, + + [String]$ModifiedStatusPrefix = ' ~', + [ConsoleColor]$ModifiedLocalForegroundColor = $([ConsoleColor]::Blue), + [ConsoleColor]$ModifiedLocalBackgroundColor = $DefaultBackgroundColor, + + [String]$DeletedStatusPrefix = ' -', + [ConsoleColor]$DeletedLocalForegroundColor = $([ConsoleColor]::Red), + [ConsoleColor]$DeletedLocalBackgroundColor = $DefaultBackgroundColor, + + [String]$UntrackedStatusPrefix = ' !', + [ConsoleColor]$UntrackedLocalForegroundColor = $([ConsoleColor]::Magenta), + [ConsoleColor]$UntrackedLocalBackgroundColor = $DefaultBackgroundColor, + + # Mercurial Specific ============================ + [String]$RenamedStatusPrefix = ' ^', + [ConsoleColor]$RenamedLocalForegroundColor = $([ConsoleColor]::Yellow), + [ConsoleColor]$RenamedLocalBackgroundColor = $DefaultBackgroundColor, + + [String]$MissingStatusPrefix = ' !', + [ConsoleColor]$MissingLocalForegroundColor = $([ConsoleColor]::Cyan), + [ConsoleColor]$MissingLocalBackgroundColor = $DefaultBackgroundColor, - # Working directory status - AddedForegroundColor = [ConsoleColor]::Green - AddedBackgroundColor = $Host.UI.RawUI.BackgroundColor - ModifiedForegroundColor = [ConsoleColor]::Blue - ModifiedBackgroundColor = $Host.UI.RawUI.BackgroundColor - DeletedForegroundColor = [ConsoleColor]::Red - DeletedBackgroundColor = $Host.UI.RawUI.BackgroundColor - UntrackedForegroundColor = [ConsoleColor]::Magenta - UntrackedBackgroundColor = $Host.UI.RawUI.BackgroundColor - MissingForegroundColor = [ConsoleColor]::Cyan - MissingBackgroundColor = $Host.UI.RawUI.BackgroundColor - RenamedForegroundColor = [ConsoleColor]::Yellow - RenamedBackgroundColor = $Host.UI.RawUI.BackgroundColor + [Switch]$GetBookmarkStatus = $true , #Tag list - ShowTags = $true - BeforeTagText = ' ' - TagForegroundColor = [ConsoleColor]::DarkGray - TagBackgroundColor = $Host.UI.RawUI.BackgroundColor - TagSeparator = ", " - TagSeparatorColor = [ConsoleColor]::White + [Switch]$ShowTags = $true, + [String]$TagPrefix = ' ', + [String]$TagSeparator = ", ", + [ConsoleColor]$TagForegroundColor = $([ConsoleColor]::DarkGray), + [ConsoleColor]$TagSeparatorForegroundColor = $([ConsoleColor]::White), + [ConsoleColor]$TagBackgroundColor = $DefaultBackgroundColor, # MQ Integration - ShowPatches = $false - BeforePatchText = ' patches: ' - UnappliedPatchForegroundColor = [ConsoleColor]::DarkGray - UnappliedPatchBackgroundColor = $Host.UI.RawUI.BackgroundColor - AppliedPatchForegroundColor = [ConsoleColor]::DarkYellow - AppliedPatchBackgroundColor = $Host.UI.RawUI.BackgroundColor - PatchSeparator = ' › ' - PatchSeparatorColor = [ConsoleColor]::White - - # Status Count Prefixes for prompt - AddedStatusPrefix = ' +' - ModifiedStatusPrefix = ' ~' - DeletedStatusPrefix = ' -' - UntrackedStatusPrefix = ' ?' - MissingStatusPrefix = ' !' - RenamedStatusPrefix = ' ^' -} \ No newline at end of file + [Switch]$ShowPatches = $false, + [String]$PatchPrefix = ' patches: ', + [ConsoleColor]$UnappliedPatchForegroundColor = $([ConsoleColor]::DarkGray), + [ConsoleColor]$UnappliedPatchBackgroundColor = $DefaultBackgroundColor, + [ConsoleColor]$AppliedPatchForegroundColor = $([ConsoleColor]::DarkYellow), + [ConsoleColor]$AppliedPatchBackgroundColor = $DefaultBackgroundColor, + [String]$PatchSeparator = ' › ', + [ConsoleColor]$PatchSeparatorColor = $([ConsoleColor]::White ) + +) + + if($global:VcsStatusSettings) { + ## Sync the Background Colors: + ## If the DefaultBackgroundColor is changed + if($PSBoundParameters.ContainsKey("DefaultBackgroundColor") -and ($global:VcsStatusSettings.DefaultBackgroundColor -ne $DefaultBackgroundColor)) { + ## Any other background colors + foreach($Background in $global:VcsStatusSettings.PsObject.Properties | Where { $_.Name -like "*BackgroundColor"} | % { $_.Name }) { + # Which haven't been set + if(!$PSBoundParameters.ContainsKey($Background)) { + if((!$global:VcsStatusSettings.$Background) -or ($global:VcsStatusSettings.$Background -eq $global:VcsStatusSettings.DefaultBackgroundColor)) { + # And are currently synced with the DefaultBackgroundColor + $PSBoundParameters.Add($Background, $DefaultBackgroundColor) + } + } + } + } + + foreach($key in $PSBoundParameters.Keys) { + $global:VcsStatusSettings | Add-Member NoteProperty $key $PSBoundParameters.$key -Force + } + ## Mercurial Specific: Set them if they've never been set: + if(!(Get-Member -In $global:VcsStatusSettings -Name ShowTags)){ + $global:VcsStatusSettings | Add-Member NoteProperty RenamedStatusPrefix $RenamedStatusPrefix -Force + $global:VcsStatusSettings | Add-Member NoteProperty RenamedLocalForegroundColor $RenamedLocalForegroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty RenamedLocalBackgroundColor $RenamedLocalBackgroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty MissingStatusPrefix $MissingStatusPrefix -Force + $global:VcsStatusSettings | Add-Member NoteProperty MissingLocalForegroundColor $MissingLocalForegroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty MissingLocalBackgroundColor $MissingLocalBackgroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty GetBookmarkStatus $GetBookmarkStatus -Force + $global:VcsStatusSettings | Add-Member NoteProperty ShowTags $ShowTags -Force + $global:VcsStatusSettings | Add-Member NoteProperty TagPrefix $TagPrefix -Force + $global:VcsStatusSettings | Add-Member NoteProperty TagSeparator $TagSeparator -Force + $global:VcsStatusSettings | Add-Member NoteProperty TagForegroundColor $TagForegroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty TagSeparatorForegroundColor $TagSeparatorForegroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty TagBackgroundColor $TagBackgroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty ShowPatches $ShowPatches -Force + $global:VcsStatusSettings | Add-Member NoteProperty PatchPrefix $PatchPrefix -Force + $global:VcsStatusSettings | Add-Member NoteProperty UnappliedPatchForegroundColor $UnappliedPatchForegroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty UnappliedPatchBackgroundColor $UnappliedPatchBackgroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty AppliedPatchForegroundColor $AppliedPatchForegroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty AppliedPatchBackgroundColor $AppliedPatchBackgroundColor -Force + $global:VcsStatusSettings | Add-Member NoteProperty PatchSeparator $PatchSeparator -Force + $global:VcsStatusSettings | Add-Member NoteProperty PatchSeparatorColor $PatchSeparatorColor -Force + } + + } else { + $global:VcsStatusSettings = New-Object PSObject -Property @{ + DefaultBackgroundColor = $DefaultBackgroundColor + + # Retreival settings + EnablePromptStatus = $EnablePromptStatus + EnableFileStatus = $EnableFileStatus + RepositoriesInWhichToDisableFileStatus = $RepositoriesInWhichToDisableFileStatus + + #Before prompt + BeforeText = $BeforeText + BeforeForegroundColor = $BeforeForegroundColor + BeforeBackgroundColor = $BeforeBackgroundColor + + #After prompt + AfterText = $AfterText + AfterForegroundColor = $AfterForegroundColor + AfterBackgroundColor = $AfterBackgroundColor + + BranchForegroundColor = $BranchForegroundColor + BranchBackgroundColor = $BranchBackgroundColor + BranchAheadForegroundColor = $BranchAheadForegroundColor + BranchAheadBackgroundColor = $BranchAheadBackgroundColor + BranchBehindForegroundColor = $BranchBehindForegroundColor + BranchBehindBackgroundColor = $BranchBehindBackgroundColor + + BranchBeheadForegroundColor = $BranchBeheadForegroundColor + BranchBeheadBackgroundColor = $BranchBeheadBackgroundColor + + # WorkingColors + AddedStatusPrefix = $AddedStatusPrefix + AddedLocalForegroundColor = $AddedLocalForegroundColor + AddedLocalBackgroundColor = $AddedLocalBackgroundColor + + ModifiedStatusPrefix = $ModifiedStatusPrefix + ModifiedLocalForegroundColor = $ModifiedLocalForegroundColor + ModifiedLocalBackgroundColor = $ModifiedLocalBackgroundColor + + DeletedStatusPrefix = $DeletedStatusPrefix + DeletedLocalForegroundColor = $DeletedLocalForegroundColor + DeletedLocalBackgroundColor = $DeletedLocalBackgroundColor + + UntrackedStatusPrefix = $UntrackedStatusPrefix + UntrackedLocalForegroundColor = $UntrackedLocalForegroundColor + UntrackedLocalBackgroundColor = $UntrackedLocalBackgroundColor + + Debug = $DebugPreference -eq "Continue" + + # Mercurial Specific ============================ + RenamedStatusPrefix = $RenamedStatusPrefix + RenamedLocalForegroundColor = $RenamedLocalForegroundColor + RenamedLocalBackgroundColor = $RenamedLocalBackgroundColor + + MissingStatusPrefix = $MissingStatusPrefix + MissingLocalForegroundColor = $MissingLocalForegroundColor + MissingLocalBackgroundColor = $MissingLocalBackgroundColor + + GetBookmarkStatus = $GetBookmarkStatus + + ShowTags = $ShowTags + TagPrefix = $TagPrefix + TagSeparator = $TagSeparator + TagForegroundColor = $TagForegroundColor + TagSeparatorForegroundColor = $TagSeparatorForegroundColor + TagBackgroundColor = $TagBackgroundColor + + ShowPatches = $ShowPatches + PatchPrefix = $PatchPrefix + UnappliedPatchForegroundColor = $UnappliedPatchForegroundColor + UnappliedPatchBackgroundColor = $UnappliedPatchBackgroundColor + AppliedPatchForegroundColor = $AppliedPatchForegroundColor + AppliedPatchBackgroundColor = $AppliedPatchBackgroundColor + PatchSeparator = $PatchSeparator + PatchSeparatorColor = $PatchSeparatorColor + } + } +} + +# Make sure this runs at least once (when the module is initially imported) +Set-VcsStatusSettings diff --git a/posh-hg.psm1 b/posh-hg.psm1 index 1e3811b..65154a8 100644 --- a/posh-hg.psm1 +++ b/posh-hg.psm1 @@ -6,7 +6,10 @@ Push-Location $psScriptRoot Pop-Location Export-ModuleMember -Function @( + 'Set-VcsStatusSettings', 'Get-HgStatus', + 'Write-HgStatus', + 'Write-VcsStatus', 'TabExpansion', 'Get-MqPatches', 'PopulateHgCommands'