diff --git a/src/AnsiUtils.ps1 b/src/AnsiUtils.ps1 index ce1877a05..2c87f036e 100644 --- a/src/AnsiUtils.ps1 +++ b/src/AnsiUtils.ps1 @@ -20,7 +20,30 @@ $ConsoleColorToAnsi = @( $AnsiDefaultColor = 39 $AnsiEscape = [char]27 + "[" +[Reflection.Assembly]::LoadWithPartialName('System.Drawing') > $null +$ColorTranslatorType = 'System.Drawing.ColorTranslator' -as [Type] +$ColorType = 'System.Drawing.Color' -as [Type] + function Get-VirtualTerminalSequence ($color, [int]$offset = 0) { + if ($color -is [byte]) { + return "${AnsiEscape}$(38 + $offset);5;${color}m" + } + if ($color -is [String]) { + try { + if ($ColorTranslatorType) { + $color = $ColorTranslatorType::FromHtml($color) + } + else { + $color = [ConsoleColor]$color + } + } + catch { + Write-Debug $_ + } + } + if ($ColorType -and ($color -is $ColorType)) { + return "${AnsiEscape}$(38 + $offset);2;$($color.R);$($color.G);$($color.B)m" + } if (($color -is [ConsoleColor]) -and ($color -ge 0) -and ($color -le 15)) { return "${AnsiEscape}$($ConsoleColorToAnsi[$color] + $offset)m" } diff --git a/src/GitPrompt.ps1 b/src/GitPrompt.ps1 index f820be79b..658dd2509 100644 --- a/src/GitPrompt.ps1 +++ b/src/GitPrompt.ps1 @@ -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 = '~' @@ -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 @@ -133,13 +133,18 @@ function Write-Prompt { param( [parameter(Mandatory = $true)] $Object, - [parameter(Mandatory = $true)] - $ForegroundColor, + [parameter()] + $ForegroundColor = $null, [parameter()] $BackgroundColor = $null, [parameter(ValueFromPipeline = $true)] [Text.StringBuilder] $Builder) + $s = $global:GitPromptSettings + if ($s -and !$ForegroundColor) { + $ForegroundColor = $s.DefaultForegroundColor + } + if ($GitPromptSettings.AnsiConsole) { $e = [char]27 + "[" $f = Get-ForegroundVirtualTerminalSequence $ForegroundColor @@ -149,11 +154,17 @@ function Write-Prompt { } return "${f}${b}${Object}${e}0m" } - if ($BackgroundColor -lt 0) { - Write-Host $Object -NoNewLine -ForegroundColor $ForegroundColor - } else { - Write-Host $Object -NoNewLine -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor + $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 if ($Builder) { return $Builder } diff --git a/src/posh-git.psm1 b/src/posh-git.psm1 index d0f23b130..5d0b91926 100644 --- a/src/posh-git.psm1 +++ b/src/posh-git.psm1 @@ -78,11 +78,11 @@ if ($ForcePoshGitPrompt -or !$currentPromptDef -or ($currentPromptDef -eq $defau $defaultPromptPrefix = [string]$GitPromptSettings.DefaultPromptPrefix if ($defaultPromptPrefix) { $expandedDefaultPromptPrefix = $ExecutionContext.SessionState.InvokeCommand.ExpandString($defaultPromptPrefix) - $res += Write-Prompt $expandedDefaultPromptPrefix -ForegroundColor $GitPromptSettings.DefaultForegroundColor + $res += Write-Prompt $expandedDefaultPromptPrefix } # Write the abbreviated current path - $res += Write-Prompt $currentPath -ForegroundColor $GitPromptSettings.DefaultForegroundColor + $res += Write-Prompt $currentPath # Write the Git status summary information $res += Write-VcsStatus @@ -103,7 +103,7 @@ if ($ForcePoshGitPrompt -or !$currentPromptDef -or ($currentPromptDef -eq $defau if ($GitPromptSettings.DefaultPromptEnableTiming) { $sw.Stop() $elapsed = $sw.ElapsedMilliseconds - $res += Write-Prompt " ${elapsed}ms" -ForegroundColor $GitPromptSettings.DefaultForegroundColor + $res += Write-Prompt " ${elapsed}ms" } $global:LASTEXITCODE = $origLastExitCode