Skip to content

Commit

Permalink
fix(status): Only reset changed colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Apr 3, 2019
1 parent 1572423 commit 4cb398f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 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
27 changes: 24 additions & 3 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,31 @@ function Write-Prompt {
$str = $Object.ToAnsiString()
}
else {
# If we know which colors were changed, we can reset only these and leave others be.
[System.Collections.Generic.List[string]]$reset = @()
$e = [char]27 + "["
$fg = Get-ForegroundVirtualTerminalSequence $fgColor
$bg = Get-BackgroundVirtualTerminalSequence $bgColor
$str = "${fg}${bg}${Object}${e}0m"

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

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

$str = "${Object}"
if (Test-VirtualTerminalSequece $str -Force) {
$reset = @('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
12 changes: 10 additions & 2 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,33 @@ 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.
[System.Collections.Generic.List[string]]$reset = @()
$e = [char]27 + "["

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

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

$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 = @('0')
}

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

return $str
Expand Down

0 comments on commit 4cb398f

Please sign in to comment.