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

Merge settings with Posh-Hg because I'm tired of having dueling sets #111

Closed
wants to merge 2 commits into from
Closed
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
256 changes: 206 additions & 50 deletions GitPrompt.ps1
Original file line number Diff line number Diff line change
@@ -1,58 +1,211 @@
# 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,

$global:GitPromptSettings = New-Object PSObject -Property @{
DefaultForegroundColor = $Host.UI.RawUI.ForegroundColor
# Retrieval settings
[Switch]$EnablePromptStatus = !$Global:GitMissing,
[Switch]$EnableFileStatus = $true,
[Switch]$ShowStatusWhenZero = $true,
[String[]]$RepositoriesInWhichToDisableFileStatus = @( ), # Array of repository paths

BeforeText = ' ['
BeforeForegroundColor = [ConsoleColor]::Yellow
BeforeBackgroundColor = $Host.UI.RawUI.BackgroundColor
DelimText = ' |'
DelimForegroundColor = [ConsoleColor]::Yellow
DelimBackgroundColor = $Host.UI.RawUI.BackgroundColor
#Before prompt
[String]$BeforeText = ' [',
[ConsoleColor]$BeforeForegroundColor = $([ConsoleColor]::Yellow),
[ConsoleColor]$BeforeBackgroundColor = $DefaultBackgroundColor,

AfterText = ']'
AfterForegroundColor = [ConsoleColor]::Yellow
AfterBackgroundColor = $Host.UI.RawUI.BackgroundColor
#After prompt
[String]$AfterText = '] ',
[ConsoleColor]$AfterForegroundColor = $([ConsoleColor]::Yellow),
[ConsoleColor]$AfterBackgroundColor = $DefaultBackgroundColor,

BranchForegroundColor = [ConsoleColor]::Cyan
BranchBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchAheadForegroundColor = [ConsoleColor]::Green
BranchAheadBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchBehindForegroundColor = [ConsoleColor]::Red
BranchBehindBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchBehindAndAheadForegroundColor = [ConsoleColor]::Yellow
BranchBehindAndAheadBackgroundColor = $Host.UI.RawUI.BackgroundColor
# Branches
[ConsoleColor]$BranchForegroundColor = $([ConsoleColor]::Cyan),
[ConsoleColor]$BranchBackgroundColor = $DefaultBackgroundColor,
# Current branch when not updated
[ConsoleColor]$BranchBehindForegroundColor = $([ConsoleColor]::DarkRed),
[ConsoleColor]$BranchBehindBackgroundColor = $DefaultBackgroundColor,
# Current branch when we're both
[ConsoleColor]$BranchBeheadForegroundColor = $([ConsoleColor]::Yellow),
[ConsoleColor]$BranchBeheadBackgroundColor = $DefaultBackgroundColor,

BeforeIndexText = ""
BeforeIndexForegroundColor= [ConsoleColor]::DarkGreen
BeforeIndexBackgroundColor= $Host.UI.RawUI.BackgroundColor
# Working DirectoryColors
[String]$AddedStatusPrefix = ' +',
[ConsoleColor]$AddedLocalForegroundColor = $([ConsoleColor]::DarkRed),
[ConsoleColor]$AddedLocalBackgroundColor = $DefaultBackgroundColor,

IndexForegroundColor = [ConsoleColor]::DarkGreen
IndexBackgroundColor = $Host.UI.RawUI.BackgroundColor
[String]$ModifiedStatusPrefix = ' ~',
[ConsoleColor]$ModifiedLocalForegroundColor = $([ConsoleColor]::DarkRed),
[ConsoleColor]$ModifiedLocalBackgroundColor = $DefaultBackgroundColor,

WorkingForegroundColor = [ConsoleColor]::DarkRed
WorkingBackgroundColor = $Host.UI.RawUI.BackgroundColor
[String]$DeletedStatusPrefix = ' -',
[ConsoleColor]$DeletedLocalForegroundColor = $([ConsoleColor]::DarkRed),
[ConsoleColor]$DeletedLocalBackgroundColor = $DefaultBackgroundColor,

UntrackedText = ' !'
UntrackedForegroundColor = [ConsoleColor]::DarkRed
UntrackedBackgroundColor = $Host.UI.RawUI.BackgroundColor
[String]$UntrackedStatusPrefix = ' !',
[ConsoleColor]$UntrackedLocalForegroundColor = $([ConsoleColor]::DarkRed),
[ConsoleColor]$UntrackedLocalBackgroundColor = $DefaultBackgroundColor,

ShowStatusWhenZero = $true
# Git Specific ============================
# Current branch when we need to push
[ConsoleColor]$BranchAheadForegroundColor = $([ConsoleColor]::Green),
[ConsoleColor]$BranchAheadBackgroundColor = $DefaultBackgroundColor,

AutoRefreshIndex = $true
[String]$DelimText = ' |',
[ConsoleColor]$DelimForegroundColor = $([ConsoleColor]::Yellow),
[ConsoleColor]$DelimBackgroundColor = $DefaultBackgroundColor,

EnablePromptStatus = !$Global:GitMissing
EnableFileStatus = $true
RepositoriesInWhichToDisableFileStatus = @( ) # Array of repository paths
[String]$UnmergedStatusPrefix = ' ?',
[ConsoleColor]$UnmergedLocalBackgroundColor = $([ConsoleColor]::DarkRed),
[ConsoleColor]$UnmergedLocalForegroundColor = $DefaultBackgroundColor,

EnableWindowTitle = 'posh~git ~ '
[String]$BeforeIndexText = "",
[ConsoleColor]$BeforeIndexForegroundColor= $([ConsoleColor]::DarkGreen),
[ConsoleColor]$BeforeIndexBackgroundColor= $DefaultBackgroundColor,

Debug = $false
[ConsoleColor]$IndexForegroundColor = $([ConsoleColor]::DarkGreen),
[ConsoleColor]$IndexBackgroundColor = $DefaultBackgroundColor,

[Switch]$AutoRefreshIndex = $true,

[string]$EnableWindowTitle = 'posh~git ~ '

)

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
}
## Git Specific: Set them if they've never been set:
if(!(Get-Member -In $global:VcsStatusSettings -Name BranchAheadForegroundColor)){
$global:VcsStatusSettings | Add-Member NoteProperty BranchAheadForegroundColor $BranchAheadForegroundColor -Force
$global:VcsStatusSettings | Add-Member NoteProperty BranchAheadBackgroundColor $BranchAheadBackgroundColor -Force

$global:VcsStatusSettings | Add-Member NoteProperty DelimText $DelimText -Force
$global:VcsStatusSettings | Add-Member NoteProperty DelimForegroundColor $DelimForegroundColor -Force
$global:VcsStatusSettings | Add-Member NoteProperty DelimBackgroundColor $DelimBackgroundColor -Force

$global:VcsStatusSettings | Add-Member NoteProperty UnmergedStatusPrefix $UnmergedStatusPrefix -Force
$global:VcsStatusSettings | Add-Member NoteProperty UnmergedLocalBackgroundColor $UnmergedLocalBackgroundColor -Force
$global:VcsStatusSettings | Add-Member NoteProperty UnmergedLocalForegroundColor $UnmergedLocalForegroundColor -Force

$global:VcsStatusSettings | Add-Member NoteProperty BeforeIndexText $BeforeIndexText -Force
$global:VcsStatusSettings | Add-Member NoteProperty BeforeIndexForegroundColor $BeforeIndexForegroundColor -Force
$global:VcsStatusSettings | Add-Member NoteProperty BeforeIndexBackgroundColor $BeforeIndexBackgroundColor -Force

$global:VcsStatusSettings | Add-Member NoteProperty IndexForegroundColor $IndexForegroundColor -Force
$global:VcsStatusSettings | Add-Member NoteProperty IndexBackgroundColor $IndexBackgroundColor -Force


$global:VcsStatusSettings | Add-Member NoteProperty ShowStatusWhenZero $ShowStatusWhenZero -Force

$global:VcsStatusSettings | Add-Member NoteProperty AutoRefreshIndex $AutoRefreshIndex -Force

$global:VcsStatusSettings | Add-Member NoteProperty EnableWindowTitle $EnableWindowTitle -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"

#Delimiter
DelimText = $DelimText
DelimForegroundColor = $DelimForegroundColor
DelimBackgroundColor = $DelimBackgroundColor

UnmergedStatusPrefix = $UnmergedStatusPrefix
UnmergedLocalBackgroundColor = $UnmergedLocalBackgroundColor
UnmergedLocalForegroundColor = $UnmergedLocalForegroundColor

BeforeIndexText = $BeforeIndexText
BeforeIndexForegroundColor = $BeforeIndexForegroundColor
BeforeIndexBackgroundColor = $BeforeIndexBackgroundColor

IndexForegroundColor = $IndexForegroundColor
IndexBackgroundColor = $IndexBackgroundColor


ShowStatusWhenZero = $ShowStatusWhenZero

AutoRefreshIndex = $AutoRefreshIndex

EnableWindowTitle = $EnableWindowTitle
}
}

# Keep track of the DEFAULT background color....
if(!$Script:CurrentBackgroundColor -or $PSBoundParameters.ContainsKey("DefaultBackgroundColor")) {
$Script:CurrentBackgroundColor = $DefaultBackgroundColor
}
}

# Make sure this runs at least once (when the module is initially imported)
Set-VcsStatusSettings

$WindowTitleSupported = $true
if (Get-Module NuGet) {
if ((get-host).Name -eq "Package Manager Host") {
$WindowTitleSupported = $false
}

Expand All @@ -65,16 +218,16 @@ function Write-Prompt($Object, $ForegroundColor, $BackgroundColor = -1) {
}

function Write-GitStatus($status) {
$s = $global:GitPromptSettings
$s = $global:VcsStatusSettings
if ($status -and $s) {
Write-Prompt $s.BeforeText -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor

$branchBackgroundColor = $s.BranchBackgroundColor
$branchForegroundColor = $s.BranchForegroundColor
if ($status.BehindBy -gt 0 -and $status.AheadBy -gt 0) {
# We are behind and ahead of remote
$branchBackgroundColor = $s.BranchBehindAndAheadBackgroundColor
$branchForegroundColor = $s.BranchBehindAndAheadForegroundColor
$branchBackgroundColor = $s.BranchBeheadBackgroundColor
$branchForegroundColor = $s.BranchBeheadForegroundColor
} elseif ($status.BehindBy -gt 0) {
# We are behind remote
$branchBackgroundColor = $s.BranchBehindBackgroundColor
Expand All @@ -91,17 +244,17 @@ function Write-GitStatus($status) {
Write-Prompt $s.BeforeIndexText -BackgroundColor $s.BeforeIndexBackgroundColor -ForegroundColor $s.BeforeIndexForegroundColor

if($s.ShowStatusWhenZero -or $status.Index.Added) {
Write-Prompt " +$($status.Index.Added.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
Write-Prompt "$($s.AddedStatusPrefix)$($status.Index.Added.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}
if($s.ShowStatusWhenZero -or $status.Index.Modified) {
Write-Prompt " ~$($status.Index.Modified.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
Write-Prompt "$($s.ModifiedStatusPrefix)$($status.Index.Modified.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}
if($s.ShowStatusWhenZero -or $status.Index.Deleted) {
Write-Prompt " -$($status.Index.Deleted.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
Write-Prompt "$($s.DeletedStatusPrefix)$($status.Index.Deleted.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}

if ($status.Index.Unmerged) {
Write-Prompt " !$($status.Index.Unmerged.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
Write-Prompt "$($s.UnmergedStatusPrefix)$($status.Index.Unmerged.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexForegroundColor
}

if($status.HasWorking) {
Expand All @@ -111,22 +264,22 @@ function Write-GitStatus($status) {

if($s.EnableFileStatus -and $status.HasWorking) {
if($s.ShowStatusWhenZero -or $status.Working.Added) {
Write-Prompt " +$($status.Working.Added.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
Write-Prompt "$($s.AddedStatusPrefix)$($status.Working.Added.Count)" -BackgroundColor $s.AddedLocalBackgroundColor -ForegroundColor $s.AddedLocalForegroundColor
}
if($s.ShowStatusWhenZero -or $status.Working.Modified) {
Write-Prompt " ~$($status.Working.Modified.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
Write-Prompt "$($s.ModifiedStatusPrefix)$($status.Working.Modified.Count)" -BackgroundColor $s.ModifiedLocalBackgroundColor -ForegroundColor $s.ModifiedLocalForegroundColor
}
if($s.ShowStatusWhenZero -or $status.Working.Deleted) {
Write-Prompt " -$($status.Working.Deleted.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
Write-Prompt "$($s.DeletedStatusPrefix)$($status.Working.Deleted.Count)" -BackgroundColor $s.DeletedLocalBackgroundColor -ForegroundColor $s.DeletedLocalForegroundColor
}

if ($status.Working.Unmerged) {
Write-Prompt " !$($status.Working.Unmerged.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
Write-Prompt "$($s.UnmergedStatusPrefix)$($status.Working.Unmerged.Count)" -BackgroundColor $s.UnmergedLocalBackgroundColor -ForegroundColor $s.UnmergedLocalForegroundColor
}
}

if ($status.HasUntracked) {
Write-Prompt $s.UntrackedText -BackgroundColor $s.UntrackedBackgroundColor -ForegroundColor $s.UntrackedForegroundColor
Write-Prompt $s.UntrackedStatusPrefix -BackgroundColor $s.UntrackedLocalBackgroundColor -ForegroundColor $s.UntrackedLocalForegroundColor
}

Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor
Expand All @@ -144,13 +297,16 @@ function Write-GitStatus($status) {
}
}

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 { & $_ } }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that. I can't imagine how I left in my Invoke-Command version.


# Add scriptblock that will execute for Write-VcsStatus
$Global:VcsPromptStatuses += {
$Global:GitStatus = Get-GitStatus
Write-GitStatus $GitStatus
}
# but we don't want any duplicate hooks (if people import the module twice)
$Global:VcsPromptStatuses = @( $Global:VcsPromptStatuses | Select -Unique )
6 changes: 3 additions & 3 deletions GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
}

function Get-GitStatus($gitDir = (Get-GitDirectory)) {
$settings = $Global:GitPromptSettings
$settings = $Global:VcsStatusSettings
$enabled = (-not $settings) -or $settings.EnablePromptStatus
if ($enabled -and $gitDir)
{
Expand Down Expand Up @@ -188,7 +188,7 @@ function Get-GitStatus($gitDir = (Get-GitDirectory)) {
function InDisabledRepository {
$currentLocation = Get-Location

foreach ($repo in $Global:GitPromptSettings.RepositoriesInWhichToDisableFileStatus)
foreach ($repo in $Global:VcsStatusSettings.RepositoriesInWhichToDisableFileStatus)
{
if ($currentLocation -like "$repo*") {
return $true
Expand Down Expand Up @@ -319,4 +319,4 @@ function Update-AllBranches($Upstream = 'master', [switch]$Quiet) {
}
}
git checkout -q $head
}
}
6 changes: 2 additions & 4 deletions posh-git.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
if (Get-Module posh-git) { return }

Push-Location $psScriptRoot
.\CheckVersion.ps1 > $null

Expand All @@ -20,6 +18,8 @@ Export-ModuleMember `
-Alias @(
'??') `
-Function @(
'Set-VcsStatusSettings',
'Write-VcsStatus',
'Invoke-NullCoalescing',
'Write-GitStatus',
'Write-Prompt',
Expand All @@ -35,5 +35,3 @@ Export-ModuleMember `
'Get-SshPath',
'Update-AllBranches',
'tgit')


2 changes: 1 addition & 1 deletion profile.example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function global:prompt {
$realLASTEXITCODE = $LASTEXITCODE

# Reset color, which can be messed up by Enable-GitColors
$Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor
$Host.UI.RawUI.ForegroundColor = $VcsStatusSettings.DefaultForegroundColor

Write-Host($pwd.ProviderPath) -nonewline

Expand Down
Loading