Skip to content

Commit

Permalink
Merge pull request #673 from ExE-Boss/fix/no-style-overreset
Browse files Browse the repository at this point in the history
fix(status): Only reset changed colors
  • Loading branch information
rkeithhill authored Jun 23, 2019
2 parents 1ac6b37 + 67c9f3d commit 5d2ffbe
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/AnsiUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function Test-VirtualTerminalSequece([psobject[]]$Object, [switch]$Force) {
}

function Get-VirtualTerminalSequence ($color, [int]$offset = 0) {
# Don't output ANSI escape sequences if the `$color` parameter is `$null`,
# they would be broken anyway
if ($null -eq $color) {
return $null;
}

if ($color -is [byte]) {
return "${AnsiEscape}$(38 + $offset);5;${color}m"
}
Expand Down
28 changes: 25 additions & 3 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,32 @@ function Write-Prompt {
$str = $Object.ToAnsiString()
}
else {
# If we know which colors were changed, we can reset only these and leave others be.
$reset = [System.Collections.Generic.List[string]]::new()
$e = [char]27 + "["
$fg = Get-ForegroundVirtualTerminalSequence $fgColor
$bg = Get-BackgroundVirtualTerminalSequence $bgColor
$str = "${fg}${bg}${Object}${e}0m"

$fg = $fgColor
if (($null -ne $fg) -and !(Test-VirtualTerminalSequece $fg)) {
$fg = Get-ForegroundVirtualTerminalSequence $fg
$reset.Add('39')
}

$bg = $bgColor
if (($null -ne $bg) -and !(Test-VirtualTerminalSequece $bg)) {
$bg = Get-BackgroundVirtualTerminalSequence $bg
$reset.Add('49')
}

$str = "${Object}"
if (Test-VirtualTerminalSequece $str -Force) {
$reset.Clear()
$reset.Add('0')
}

$str = "${fg}${bg}" + $str
if ($reset.Count -gt 0) {
$str += "${e}$($reset -join ';')m"
}
}

return $(if ($StringBuilder) { $StringBuilder.Append($str) } else { $str })
Expand Down
23 changes: 16 additions & 7 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,34 @@ class PoshGitTextSpan {
# $GitPromptSettings.AnsiConsole is $true. It is also used by the default ToString()
# implementation to display any ANSI seqs when AnsiConsole is $true.
[string] ToAnsiString() {
# If we know which colors were changed, we can reset only these and leave others be.
$reset = [System.Collections.Generic.List[string]]::new()
$e = [char]27 + "["

$bg = $this.BackgroundColor
if (($null -ne $bg) -and !(Test-VirtualTerminalSequece $bg)) {
$bg = Get-BackgroundVirtualTerminalSequence $bg
}

$fg = $this.ForegroundColor
if (($null -ne $fg) -and !(Test-VirtualTerminalSequece $fg)) {
$fg = Get-ForegroundVirtualTerminalSequence $fg
$reset.Add('39')
}

$bg = $this.BackgroundColor
if (($null -ne $bg) -and !(Test-VirtualTerminalSequece $bg)) {
$bg = Get-BackgroundVirtualTerminalSequence $bg
$reset.Add('49')
}

$txt = $this.Text
$str = "${fg}${bg}${txt}"

# ALWAYS terminate a VT sequence in case the host supports VT (regardless of AnsiConsole setting),
# or the host display can get messed up.
if (Test-VirtualTerminalSequece $str -Force) {
$str += "${e}0m"
if (Test-VirtualTerminalSequece $txt -Force) {
$reset.Clear()
$reset.Add('0')
}

if ($reset.Count -gt 0) {
$str += "${e}$($reset -join ';')m"
}

return $str
Expand Down
4 changes: 2 additions & 2 deletions test/Ansi.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Describe 'ANSI Tests' {
It 'Setting BackgroundColor to 0x0 results in Black background' {
$ts = & $module.NewBoundScriptBlock({[PoshGitTextSpan]::new("TEST", 0xFF0000, 0)})
$ansiStr = $ts.toAnsiString()
$ansiStr | Should BeExactly "${csi}38;2;255;0;0m${csi}48;2;0;0;0mTEST${csi}0m"
$ansiStr | Should BeExactly "${csi}38;2;255;0;0m${csi}48;2;0;0;0mTEST${csi}39;49m"
}
It 'Setting ForegroundColor to 0x0 results in Black foreground' {
$ts = & $module.NewBoundScriptBlock({[PoshGitTextSpan]::new("TEST", 0, 0xFFFFFF)})
$ansiStr = $ts.toAnsiString()
$ansiStr | Should BeExactly "${csi}38;2;0;0;0m${csi}48;2;255;255;255mTEST${csi}0m"
$ansiStr | Should BeExactly "${csi}38;2;0;0;0m${csi}48;2;255;255;255mTEST${csi}39;49m"
}
}
}
20 changes: 10 additions & 10 deletions test/DefaultPrompt.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,37 +162,37 @@ Describe 'Default Prompt Tests - ANSI' {
$GitPromptSettings.DefaultPromptSuffix.ForegroundColor = [ConsoleColor]::DarkBlue
$GitPromptSettings.DefaultPromptSuffix.BackgroundColor = 0xFF6000 # Orange
$res = &$prompt
$res | Should BeExactly "$(Get-PromptConnectionInfo)$(GetHomePath)${csi}34m${csi}48;2;255;96;0m`n> ${csi}0m"
$res | Should BeExactly "$(Get-PromptConnectionInfo)$(GetHomePath)${csi}34m${csi}48;2;255;96;0m`n> ${csi}39;49m"
}
It 'Returns the expected prompt string with expanded DefaultPromptSuffix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptSuffix.Text = ' - $(6*7)> '
$GitPromptSettings.DefaultPromptSuffix.ForegroundColor = [ConsoleColor]::DarkBlue
$GitPromptSettings.DefaultPromptSuffix.BackgroundColor = 0xFF6000 # Orange
$res = &$prompt
$res | Should BeExactly "$(Get-PromptConnectionInfo)$(GetHomePath)${csi}34m${csi}48;2;255;96;0m - 42> ${csi}0m"
$res | Should BeExactly "$(Get-PromptConnectionInfo)$(GetHomePath)${csi}34m${csi}48;2;255;96;0m - 42> ${csi}39;49m"
}
It 'Returns the expected prompt string with changed DefaultPromptPrefix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptPrefix.Text = 'PS '
$GitPromptSettings.DefaultPromptPrefix.BackgroundColor = [ConsoleColor]::White
$res = &$prompt
$res | Should BeExactly "${csi}107mPS ${csi}0m$(GetHomePath)> "
$res | Should BeExactly "${csi}107mPS ${csi}49m$(GetHomePath)> "
}
It 'Returns the expected prompt string with expanded DefaultPromptPrefix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptPrefix.Text = '[$(hostname)] '
$GitPromptSettings.DefaultPromptPrefix.BackgroundColor = 0xF5F5F5
$res = &$prompt
$res | Should BeExactly "${csi}48;2;245;245;245m[$(hostname)] ${csi}0m$(GetHomePath)> "
$res | Should BeExactly "${csi}48;2;245;245;245m[$(hostname)] ${csi}49m$(GetHomePath)> "
}
It 'Returns the expected prompt path colors' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$GitPromptSettings.DefaultPromptPath.ForegroundColor = [ConsoleColor]::DarkCyan
$GitPromptSettings.DefaultPromptPath.BackgroundColor = [ConsoleColor]::DarkRed
$res = &$prompt
$res | Should BeExactly "$(Get-PromptConnectionInfo)${csi}36m${csi}41m$(GetHomePath)${csi}0m> "
$res | Should BeExactly "$(Get-PromptConnectionInfo)${csi}36m${csi}41m$(GetHomePath)${csi}39;49m> "
}
It 'Returns the expected prompt string with prefix, suffix and abbrev home set' {
Set-Location $Home -ErrorAction Stop
Expand All @@ -202,7 +202,7 @@ Describe 'Default Prompt Tests - ANSI' {
$GitPromptSettings.DefaultPromptSuffix.ForegroundColor = [ConsoleColor]::DarkBlue
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$res = &$prompt
$res | Should BeExactly "${csi}38;2;245;245;245m[$(hostname)] ${csi}0m$(GetHomePath)${csi}34m - 42> ${csi}0m"
$res | Should BeExactly "${csi}38;2;245;245;245m[$(hostname)] ${csi}39m$(GetHomePath)${csi}34m - 42> ${csi}39m"
}
It 'Returns the expected prompt string with prompt timing enabled' {
Set-Location $Home -ErrorAction Stop
Expand All @@ -211,7 +211,7 @@ Describe 'Default Prompt Tests - ANSI' {
$res = &$prompt
$escapedHome = [regex]::Escape((GetHomePath))
$rexcsi = [regex]::Escape($csi)
$res | Should Match "$escapedHome${rexcsi}95m \d+ms${rexcsi}0m> "
$res | Should Match "$escapedHome${rexcsi}95m \d+ms${rexcsi}39m> "
}
}

Expand All @@ -238,7 +238,7 @@ A test/Foo.Tests.ps1
$res = &$prompt
Assert-MockCalled git -ModuleName posh-git
$path = GetHomeRelPath $PSScriptRoot
$res | Should BeExactly "$(Get-PromptConnectionInfo)$path ${csi}93m[${csi}0m${csi}96mmaster${csi}0m${csi}32m${csi}49m +1${csi}0m${csi}32m${csi}49m ~0${csi}0m${csi}32m${csi}49m -0${csi}0m${csi}93m |${csi}0m${csi}31m${csi}49m +0${csi}0m${csi}31m${csi}49m ~1${csi}0m${csi}31m${csi}49m -1${csi}0m${csi}31m !${csi}0m${csi}93m]${csi}0m> "
$res | Should BeExactly "$(Get-PromptConnectionInfo)$path ${csi}93m[${csi}39m${csi}96mmaster${csi}39m${csi}32m +1${csi}39m${csi}32m ~0${csi}39m${csi}32m -0${csi}39m${csi}93m |${csi}39m${csi}31m +0${csi}39m${csi}31m ~1${csi}39m${csi}31m -1${csi}39m${csi}31m !${csi}39m${csi}93m]${csi}39m> "
}

It 'Returns the expected prompt string with changed PathStatusSeparator' {
Expand All @@ -258,7 +258,7 @@ A test/Foo.Tests.ps1
$res = [string](&$prompt *>&1)
Assert-MockCalled git -ModuleName posh-git -Scope It
$path = GetHomeRelPath $PSScriptRoot
$res | Should BeExactly "$(Get-PromptConnectionInfo)$path${csi}107m !! ${csi}0m${csi}93m[${csi}0m${csi}96mmaster${csi}0m${csi}93m]${csi}0m> "
$res | Should BeExactly "$(Get-PromptConnectionInfo)$path${csi}107m !! ${csi}49m${csi}93m[${csi}39m${csi}96mmaster${csi}39m${csi}93m]${csi}39m> "
}
It 'Returns the expected prompt string with expanded PathStatusSeparator' {
Mock -ModuleName posh-git -CommandName git {
Expand All @@ -277,7 +277,7 @@ A test/Foo.Tests.ps1
$res = [string](&$prompt *>&1)
Assert-MockCalled git -ModuleName posh-git -Scope It
$path = GetHomeRelPath $PSScriptRoot
$res | Should BeExactly "$(Get-PromptConnectionInfo)$path${csi}107m [$(hostname)] ${csi}0m${csi}93m[${csi}0m${csi}96mmaster${csi}0m${csi}93m]${csi}0m> "
$res | Should BeExactly "$(Get-PromptConnectionInfo)$path${csi}107m [$(hostname)] ${csi}49m${csi}93m[${csi}39m${csi}96mmaster${csi}39m${csi}93m]${csi}39m> "
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/GitPrompt.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ M test/Baz.Tests.ps1

It 'Returns status output from Write-VcsStatus as string' {
$res = Write-VcsStatus
$res | Should BeExactly " ${csi}93m[${csi}0m${csi}96mmaster${csi}0m${csi}32m${csi}49m +1${csi}0m${csi}32m${csi}49m ~0${csi}0m${csi}32m${csi}49m -0${csi}0m${csi}96m ~${csi}0m${csi}93m]${csi}0m"
$res | Should BeExactly " ${csi}93m[${csi}39m${csi}96mmaster${csi}39m${csi}32m +1${csi}39m${csi}32m ~0${csi}39m${csi}32m -0${csi}39m${csi}96m ~${csi}39m${csi}93m]${csi}39m"
}
}
}

0 comments on commit 5d2ffbe

Please sign in to comment.