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

Write-Prompt with $null colors, plus respecting DefaultForegroundColor #454

Merged
merged 3 commits into from
Mar 12, 2017
Merged
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
66 changes: 42 additions & 24 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
# http://www.markembling.info/view/my-ideal-powershell-prompt-with-git-integration

$global:GitPromptSettings = [pscustomobject]@{
DefaultForegroundColor = $Host.UI.RawUI.ForegroundColor
DefaultForegroundColor = $null

BeforeText = ' ['
BeforeForegroundColor = [ConsoleColor]::Yellow
BeforeBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeBackgroundColor = $null

DelimText = ' |'
DelimForegroundColor = [ConsoleColor]::Yellow
DelimBackgroundColor = $Host.UI.RawUI.BackgroundColor
DelimBackgroundColor = $null

AfterText = ']'
AfterForegroundColor = [ConsoleColor]::Yellow
AfterBackgroundColor = $Host.UI.RawUI.BackgroundColor
AfterBackgroundColor = $null

FileAddedText = '+'
FileModifiedText = '~'
Expand All @@ -24,62 +24,62 @@ $global:GitPromptSettings = [pscustomobject]@{
LocalDefaultStatusSymbol = $null
LocalDefaultStatusForegroundColor = [ConsoleColor]::DarkGreen
LocalDefaultStatusForegroundBrightColor = [ConsoleColor]::Green
LocalDefaultStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
LocalDefaultStatusBackgroundColor = $null

LocalWorkingStatusSymbol = '!'
LocalWorkingStatusForegroundColor = [ConsoleColor]::DarkRed
LocalWorkingStatusForegroundBrightColor = [ConsoleColor]::Red
LocalWorkingStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
LocalWorkingStatusBackgroundColor = $null

LocalStagedStatusSymbol = '~'
LocalStagedStatusForegroundColor = [ConsoleColor]::Cyan
LocalStagedStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
LocalStagedStatusBackgroundColor = $null

BranchUntrackedSymbol = $null
BranchForegroundColor = [ConsoleColor]::Cyan
BranchBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchBackgroundColor = $null

BranchGoneStatusSymbol = [char]0x00D7 # × Multiplication sign
BranchGoneStatusForegroundColor = [ConsoleColor]::DarkCyan
BranchGoneStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchGoneStatusBackgroundColor = $null

BranchIdenticalStatusToSymbol = [char]0x2261 # ≡ Three horizontal lines
BranchIdenticalStatusToForegroundColor = [ConsoleColor]::Cyan
BranchIdenticalStatusToBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchIdenticalStatusToBackgroundColor = $null

BranchAheadStatusSymbol = [char]0x2191 # ↑ Up arrow
BranchAheadStatusForegroundColor = [ConsoleColor]::Green
BranchAheadStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchAheadStatusBackgroundColor = $null

BranchBehindStatusSymbol = [char]0x2193 # ↓ Down arrow
BranchBehindStatusForegroundColor = [ConsoleColor]::Red
BranchBehindStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchBehindStatusBackgroundColor = $null

BranchBehindAndAheadStatusSymbol = [char]0x2195 # ↕ Up & Down arrow
BranchBehindAndAheadStatusForegroundColor = [ConsoleColor]::Yellow
BranchBehindAndAheadStatusBackgroundColor = $Host.UI.RawUI.BackgroundColor
BranchBehindAndAheadStatusBackgroundColor = $null

BeforeIndexText = ""
BeforeIndexForegroundColor = [ConsoleColor]::DarkGreen
BeforeIndexForegroundBrightColor = [ConsoleColor]::Green
BeforeIndexBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeIndexBackgroundColor = $null

IndexForegroundColor = [ConsoleColor]::DarkGreen
IndexForegroundBrightColor = [ConsoleColor]::Green
IndexBackgroundColor = $Host.UI.RawUI.BackgroundColor
IndexBackgroundColor = $null

WorkingForegroundColor = [ConsoleColor]::DarkRed
WorkingForegroundBrightColor = [ConsoleColor]::Red
WorkingBackgroundColor = $Host.UI.RawUI.BackgroundColor
WorkingBackgroundColor = $null

EnableStashStatus = $false
BeforeStashText = ' ('
BeforeStashBackgroundColor = $Host.UI.RawUI.BackgroundColor
BeforeStashBackgroundColor = $null
BeforeStashForegroundColor = [ConsoleColor]::Red
AfterStashText = ')'
AfterStashBackgroundColor = $Host.UI.RawUI.BackgroundColor
AfterStashBackgroundColor = $null
AfterStashForegroundColor = [ConsoleColor]::Red
StashBackgroundColor = $Host.UI.RawUI.BackgroundColor
StashBackgroundColor = $null
StashForegroundColor = [ConsoleColor]::Red

ShowStatusWhenZero = $true
Expand Down Expand Up @@ -127,12 +127,30 @@ 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-Prompt($Object, $ForegroundColor = $null, $BackgroundColor = $null) {
$s = $global:GitPromptSettings
if ($s -and !$ForegroundColor) {
$ForegroundColor = $s.DefaultForegroundColor
}

if ($BackgroundColor -is [string]) {
$BackgroundColor = [ConsoleColor]$BackgroundColor
}
if ($ForegroundColor -is [string]) {
$ForegroundColor = [ConsoleColor]$ForegroundColor
}

$writeHostParams = @{
Object = $Object;
NoNewLine = $true;
}
if (($BackgroundColor -ge 0) -and ($BackgroundColor -le 15)) {
$writeHostParams.BackgroundColor = $BackgroundColor
}
if (($ForegroundColor -ge 0) -and ($ForegroundColor -le 15)) {
$writeHostParams.ForegroundColor = $ForegroundColor
}
Write-Host @writeHostParams
}

function Format-BranchName($branchName){
Expand Down
6 changes: 3 additions & 3 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ if ($ForcePoshGitPrompt -or !$currentPromptDef -or ($currentPromptDef -eq $defau
$defaultPromptPrefix = [string]$GitPromptSettings.DefaultPromptPrefix
if ($defaultPromptPrefix) {
$expandedDefaultPromptPrefix = $ExecutionContext.SessionState.InvokeCommand.ExpandString($defaultPromptPrefix)
Write-Host $expandedDefaultPromptPrefix -NoNewline
Write-Prompt $expandedDefaultPromptPrefix
}

# Write the abbreviated current path
Write-Host $currentPath -NoNewline
Write-Prompt $currentPath

# Write the Git status summary information
Write-VcsStatus
Expand All @@ -110,7 +110,7 @@ if ($ForcePoshGitPrompt -or !$currentPromptDef -or ($currentPromptDef -eq $defau
if ($GitPromptSettings.DefaultPromptEnableTiming) {
$sw.Stop()
$elapsed = $sw.ElapsedMilliseconds
Write-Host " ${elapsed}ms" -NoNewline
Write-Prompt " ${elapsed}ms"
}

$global:LASTEXITCODE = $origLastExitCode
Expand Down