Skip to content

Instantly share code, notes, and snippets.

@kenwarner
Created February 7, 2016 01:57
Show Gist options
  • Save kenwarner/c078143df988ac4f06b6 to your computer and use it in GitHub Desktop.
Save kenwarner/c078143df988ac4f06b6 to your computer and use it in GitHub Desktop.
GitPrompt.ps1 with more color options
# Inspired by Mark Embling
# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration
$global:GitPromptSettings = New-Object PSObject -Property @{
DefaultForegroundColor = $Host.UI.RawUI.ForegroundColor
DefaultBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeText = ' ['
BeforeForegroundColor = [ConsoleColor]::Yellow
BeforeBackgroundColor = $DefaultBackgroundColor
DelimText = ' |'
DelimForegroundColor = [ConsoleColor]::Yellow
DelimBackgroundColor = $DefaultBackgroundColor
AfterText = ']'
AfterForegroundColor = [ConsoleColor]::Yellow
AfterBackgroundColor = $DefaultBackgroundColor
BranchForegroundColor = [ConsoleColor]::Cyan
BranchBackgroundColor = $DefaultBackgroundColor
BranchAheadForegroundColor = [ConsoleColor]::Green
BranchAheadBackgroundColor = $DefaultBackgroundColor
BranchBehindForegroundColor = [ConsoleColor]::Red
BranchBehindBackgroundColor = $DefaultBackgroundColor
BranchBehindAndAheadForegroundColor = [ConsoleColor]::Yellow
BranchBehindAndAheadBackgroundColor = $DefaultBackgroundColor
BeforeIndexText = ""
BeforeIndexForegroundColor= [ConsoleColor]::DarkGreen
BeforeIndexBackgroundColor= $DefaultBackgroundColor
IndexBackgroundColor = $DefaultBackgroundColor
IndexForegroundColor = [ConsoleColor]::DarkGreen
IndexZeroAddedForegroundColor = $DefaultForegroundColor
IndexZeroModifiedForegroundColor = $DefaultForegroundColor
IndexZeroDeletedForegroundColor = $DefaultForegroundColor
IndexZeroUnmergedForegroundColor = $DefaultForegroundColor
IndexNonzeroAddedForegroundColor = $DefaultForegroundColor
IndexNonzeroModifiedForegroundColor = $DefaultForegroundColor
IndexNonzeroDeletedForegroundColor = $DefaultForegroundColor
IndexNonzeroUnmergedForegroundColor = $DefaultForegroundColor
WorkingBackgroundColor = $DefaultBackgroundColor
WorkingForegroundColor = [ConsoleColor]::DarkRed
WorkingZeroAddedForegroundColor = $DefaultForegroundColor
WorkingZeroModifiedForegroundColor = $DefaultForegroundColor
WorkingZeroDeletedForegroundColor = $DefaultForegroundColor
WorkingZeroUnmergedForegroundColor = $DefaultForegroundColor
WorkingNonzeroAddedForegroundColor = $DefaultForegroundColor
WorkingNonzeroModifiedForegroundColor = $DefaultForegroundColor
WorkingNonzeroDeletedForegroundColor = $DefaultForegroundColor
WorkingNonzeroUnmergedForegroundColor = $DefaultForegroundColor
AddedForegroundColor = $DefaultForegroundColor
ModifiedForegroundColor = $DefaultForegroundColor
DeletedForegroundColor = $DefaultForegroundColor
UnmergedForegroundColor = $DefaultForegroundColor
UntrackedText = ' !'
UntrackedForegroundColor = [ConsoleColor]::DarkRed
UntrackedBackgroundColor = $DefaultBackgroundColor
ShowStatusWhenZero = $true
AutoRefreshIndex = $true
EnablePromptStatus = !$Global:GitMissing
EnableFileStatus = $true
RepositoriesInWhichToDisableFileStatus = @( ) # Array of repository paths
DescribeStyle = ''
EnableWindowTitle = 'posh~git ~ '
Debug = $false
}
$WindowTitleSupported = $true
if (Get-Module NuGet) {
$WindowTitleSupported = $false
}
function Write-Prompt($Object, $ForegroundColor, $BackgroundColor = -1) {
if ($BackgroundColor -lt 0) {
Write-Host $Object -NoNewLine -ForegroundColor $ForegroundColor
} else {
Write-Host $Object -NoNewLine -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor
}
}
function Write-GitStatus($status) {
$s = $global:GitPromptSettings
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
} elseif ($status.BehindBy -gt 0) {
# We are behind remote
$branchBackgroundColor = $s.BranchBehindBackgroundColor
$branchForegroundColor = $s.BranchBehindForegroundColor
} elseif ($status.AheadBy -gt 0) {
# We are ahead of remote
$branchBackgroundColor = $s.BranchAheadBackgroundColor
$branchForegroundColor = $s.BranchAheadForegroundColor
}
Write-Prompt $status.Branch -BackgroundColor $branchBackgroundColor -ForegroundColor $branchForegroundColor
if($s.EnableFileStatus -and $status.HasIndex) {
Write-Prompt $s.BeforeIndexText -BackgroundColor $s.BeforeIndexBackgroundColor -ForegroundColor $s.BeforeIndexForegroundColor
if($s.ShowStatusWhenZero -or $status.Index.Added) {
if ($status.Index.Added.Count -ne 0) {
Write-Prompt " +$($status.Index.Added.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexNonzeroAddedForegroundColor
}
else {
Write-Prompt " +$($status.Index.Added.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexZeroAddedForegroundColor
}
}
if($s.ShowStatusWhenZero -or $status.Index.Modified) {
if ($status.Index.Modified.Count -ne 0) {
Write-Prompt " ~$($status.Index.Modified.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexNonzeroModifiedForegroundColor
}
else {
Write-Prompt " ~$($status.Index.Modified.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexZeroModifiedForegroundColor
}
}
if($s.ShowStatusWhenZero -or $status.Index.Deleted) {
if ($status.Index.Deleted.Count -ne 0) {
Write-Prompt " -$($status.Index.Deleted.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexNonzeroDeletedForegroundColor
}
else {
Write-Prompt " -$($status.Index.Deleted.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexZeroDeletedForegroundColor
}
}
if ($status.Index.Unmerged) {
if ($status.Index.Unmerged.Count -ne 0) {
Write-Prompt " !$($status.Index.Unmerged.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexNonzeroUnmergedForegroundColor
}
else {
Write-Prompt " !$($status.Index.Unmerged.Count)" -BackgroundColor $s.IndexBackgroundColor -ForegroundColor $s.IndexZeroUnmergedForegroundColor
}
}
if($status.HasWorking) {
Write-Prompt $s.DelimText -BackgroundColor $s.DelimBackgroundColor -ForegroundColor $s.DelimForegroundColor
}
}
if($s.EnableFileStatus -and $status.HasWorking) {
if($s.ShowStatusWhenZero -or $status.Working.Added) {
if ($status.Working.Added.Count -ne 0) {
Write-Prompt " +$($status.Working.Added.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingNonzeroAddedForegroundColor
}
else {
Write-Prompt " +$($status.Working.Added.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingZeroAddedForegroundColor
}
}
if($s.ShowStatusWhenZero -or $status.Working.Modified) {
if ($status.Working.Modified.Count -ne 0) {
Write-Prompt " ~$($status.Working.Modified.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingNonzeroModifiedForegroundColor
}
else {
Write-Prompt " ~$($status.Working.Modified.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingZeroModifiedForegroundColor
}
}
if($s.ShowStatusWhenZero -or $status.Working.Deleted) {
if ($status.Working.Deleted.Count -ne 0) {
Write-Prompt " -$($status.Working.Deleted.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingNonzeroDeletedForegroundColor
}
else {
Write-Prompt " -$($status.Working.Deleted.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingZeroDeletedForegroundColor
}
}
if ($status.Working.Unmerged) {
if ($status.Working.Unmerged.Count -ne 0) {
Write-Prompt " !$($status.Working.Unmerged.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingNonzeroUnmergedForegroundColor
}
else {
Write-Prompt " !$($status.Working.Unmerged.Count)" -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingZeroUnmergedForegroundColor
}
}
}
if ($status.HasUntracked) {
Write-Prompt $s.UntrackedText -BackgroundColor $s.UntrackedBackgroundColor -ForegroundColor $s.UntrackedForegroundColor
}
Write-Prompt $s.AfterText -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor
if ($WindowTitleSupported -and $s.EnableWindowTitle) {
if( -not $Global:PreviousWindowTitle ) {
$Global:PreviousWindowTitle = $Host.UI.RawUI.WindowTitle
}
$repoName = Split-Path -Leaf (Split-Path $status.GitDir)
$prefix = if ($s.EnableWindowTitle -is [string]) { $s.EnableWindowTitle } else { '' }
$Host.UI.RawUI.WindowTitle = "$prefix$repoName [$($status.Branch)]"
}
} elseif ( $Global:PreviousWindowTitle ) {
$Host.UI.RawUI.WindowTitle = $Global:PreviousWindowTitle
}
}
if(!(Test-Path Variable:Global:VcsPromptStatuses)) {
$Global:VcsPromptStatuses = @()
}
function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } }
# Add scriptblock that will execute for Write-VcsStatus
$Global:VcsPromptStatuses += {
$Global:GitStatus = Get-GitStatus
Write-GitStatus $GitStatus
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment