From 45a32b9ef66fff4cff0ef9d3191224d05f6c744a Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Fri, 3 Mar 2017 22:34:22 -0600 Subject: [PATCH 1/7] Add basic ANSI 256-color and 24-bit color support 24-bit support depends on System.Drawing for parsing HTML color strings. --- src/AnsiUtils.ps1 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/AnsiUtils.ps1 b/src/AnsiUtils.ps1 index ce1877a05..1c343cb94 100644 --- a/src/AnsiUtils.ps1 +++ b/src/AnsiUtils.ps1 @@ -20,10 +20,23 @@ $ConsoleColorToAnsi = @( $AnsiDefaultColor = 39 $AnsiEscape = [char]27 + "[" +[Reflection.Assembly]::LoadWithPartialName('System.Drawing') > $null +$ColorTranslatorType = ([System.Management.Automation.PSTypeName]'System.Drawing.ColorTranslator').Type +$ColorType = ([System.Management.Automation.PSTypeName]'System.Drawing.Color').Type + function Get-VirtualTerminalSequence ($color, [int]$offset = 0) { if (($color -is [ConsoleColor]) -and ($color -ge 0) -and ($color -le 15)) { return "${AnsiEscape}$($ConsoleColorToAnsi[$color] + $offset)m" } + if ($color -is [byte]) { + return "${AnsiEscape}$(38 + $offset);5;${color}m" + } + if ($ColorTranslatorType -and ($color -is [String])) { + $color = $ColorTranslatorType::FromHtml($color) + } + if ($ColorType -and ($color -is $ColorType)) { + return "${AnsiEscape}$(38 + $offset);2;$($color.R);$($color.G);$($color.B)m" + } return "${AnsiEscape}$($AnsiDefaultColor + $offset)m" } From 303fbd025b1874cc18fb3979172cc8b863df7e89 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sat, 4 Mar 2017 09:45:06 -0600 Subject: [PATCH 2/7] Make Write-Prompt more forgiving --- src/GitPrompt.ps1 | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/GitPrompt.ps1 b/src/GitPrompt.ps1 index ce3f9b2bd..d74676842 100644 --- a/src/GitPrompt.ps1 +++ b/src/GitPrompt.ps1 @@ -127,12 +127,25 @@ 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) { + 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){ From c3b4327d04ae090198b9d3d31249d1b8f0e02286 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sat, 4 Mar 2017 09:51:15 -0600 Subject: [PATCH 3/7] Use DefaultForegroundColor for prompt text The setting exists for legacy reasons: 02956e1177ae0c348f22927bc4440352172f2a32. It was removed in #202 and restored in #209 to avoid a breaking change. Since we still have it, I suppose we might as well support using it to control the prompt text color. --- src/GitPrompt.ps1 | 5 +++++ src/posh-git.psm1 | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/GitPrompt.ps1 b/src/GitPrompt.ps1 index d74676842..7c15a96d7 100644 --- a/src/GitPrompt.ps1 +++ b/src/GitPrompt.ps1 @@ -128,6 +128,11 @@ if (Get-Module NuGet) { } 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 } diff --git a/src/posh-git.psm1 b/src/posh-git.psm1 index ea0687976..a056318f1 100644 --- a/src/posh-git.psm1 +++ b/src/posh-git.psm1 @@ -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 @@ -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 From d6b7d344d6de77602208d4800ca015454913ca65 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sat, 4 Mar 2017 09:57:47 -0600 Subject: [PATCH 4/7] Stop capturing $Host.UI.RawUI colors --- src/GitPrompt.ps1 | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/GitPrompt.ps1 b/src/GitPrompt.ps1 index 7c15a96d7..7a41774ef 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 From 7dac82218cb638577fef6d2b864fb0a487f280d4 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sat, 4 Mar 2017 11:24:48 -0600 Subject: [PATCH 5/7] Handle parsing bad color strings --- src/AnsiUtils.ps1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/AnsiUtils.ps1 b/src/AnsiUtils.ps1 index 1c343cb94..25fc3e82e 100644 --- a/src/AnsiUtils.ps1 +++ b/src/AnsiUtils.ps1 @@ -32,7 +32,12 @@ function Get-VirtualTerminalSequence ($color, [int]$offset = 0) { return "${AnsiEscape}$(38 + $offset);5;${color}m" } if ($ColorTranslatorType -and ($color -is [String])) { - $color = $ColorTranslatorType::FromHtml($color) + try { + $color = $ColorTranslatorType::FromHtml($color) + } + catch { + Write-Debug $_ + } } if ($ColorType -and ($color -is $ColorType)) { return "${AnsiEscape}$(38 + $offset);2;$($color.R);$($color.G);$($color.B)m" From 99f130d3875d082e9ad675864c89f55d408f93df Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sun, 5 Mar 2017 15:22:07 -0600 Subject: [PATCH 6/7] Use `-as [Type]` for safe conversion --- src/AnsiUtils.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AnsiUtils.ps1 b/src/AnsiUtils.ps1 index 25fc3e82e..9aa92df68 100644 --- a/src/AnsiUtils.ps1 +++ b/src/AnsiUtils.ps1 @@ -21,8 +21,8 @@ $AnsiDefaultColor = 39 $AnsiEscape = [char]27 + "[" [Reflection.Assembly]::LoadWithPartialName('System.Drawing') > $null -$ColorTranslatorType = ([System.Management.Automation.PSTypeName]'System.Drawing.ColorTranslator').Type -$ColorType = ([System.Management.Automation.PSTypeName]'System.Drawing.Color').Type +$ColorTranslatorType = 'System.Drawing.ColorTranslator' -as [Type] +$ColorType = 'System.Drawing.Color' -as [Type] function Get-VirtualTerminalSequence ($color, [int]$offset = 0) { if (($color -is [ConsoleColor]) -and ($color -ge 0) -and ($color -le 15)) { From 72572fcd130e0c953b99b0bd9b16e7c382fd3ebf Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sun, 5 Mar 2017 15:35:13 -0600 Subject: [PATCH 7/7] Use strings as ConsoleColor if no System.Drawing --- src/AnsiUtils.ps1 | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/AnsiUtils.ps1 b/src/AnsiUtils.ps1 index 9aa92df68..2c87f036e 100644 --- a/src/AnsiUtils.ps1 +++ b/src/AnsiUtils.ps1 @@ -25,15 +25,17 @@ $ColorTranslatorType = 'System.Drawing.ColorTranslator' -as [Type] $ColorType = 'System.Drawing.Color' -as [Type] function Get-VirtualTerminalSequence ($color, [int]$offset = 0) { - if (($color -is [ConsoleColor]) -and ($color -ge 0) -and ($color -le 15)) { - return "${AnsiEscape}$($ConsoleColorToAnsi[$color] + $offset)m" - } if ($color -is [byte]) { return "${AnsiEscape}$(38 + $offset);5;${color}m" } - if ($ColorTranslatorType -and ($color -is [String])) { + if ($color -is [String]) { try { - $color = $ColorTranslatorType::FromHtml($color) + if ($ColorTranslatorType) { + $color = $ColorTranslatorType::FromHtml($color) + } + else { + $color = [ConsoleColor]$color + } } catch { Write-Debug $_ @@ -42,6 +44,9 @@ function Get-VirtualTerminalSequence ($color, [int]$offset = 0) { 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" + } return "${AnsiEscape}$($AnsiDefaultColor + $offset)m" }