diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a8ffcab1..c4cd00039 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,10 @@ ### Removed -- SSH agent functionality has been removed from `posh-git` and put into another module focused solely on +- BREAKING: Removed SSH agent functionality from `posh-git` and put into another module focused solely on Git SSH support. See [posh-sshell](https://github.com/dahlbyk/posh-sshell). +- BREAKING: Removed `PoshGitTextSpan.CustomAnsi` property - now just put your custom VT sequences in the + `PoshGitTextSpan.Text` property. Be sure to terminate your VT sequences with `"$([char]27)[0m"` ### Added diff --git a/src/AnsiUtils.ps1 b/src/AnsiUtils.ps1 index 7830ad63b..d06b4bfe0 100644 --- a/src/AnsiUtils.ps1 +++ b/src/AnsiUtils.ps1 @@ -35,12 +35,14 @@ function EscapeAnsiString([string]$AnsiString) { $res } -function Test-VirtualTerminalSequece([psobject]$Object) { - if ($global:GitPromptSettings.AnsiConsole -and ($Object -is [string])) { - return $Object.Contains($AnsiEscape) - } - else { - return $false +function Test-VirtualTerminalSequece([psobject[]]$Object, [switch]$Force) { + foreach ($obj in $Object) { + if (($Force -or $global:GitPromptSettings.AnsiConsole) -and ($obj -is [string])) { + $obj.Contains($AnsiEscape) + } + else { + $false + } } } @@ -66,7 +68,7 @@ function Get-VirtualTerminalSequence ($color, [int]$offset = 0) { Write-Debug $_ } - # Hard to get here but DarkYellow is not an HTML color but is a ConsoleColor + # Hard to get here but DarkYellow is not an HTML color but it is a ConsoleColor if (($color -isnot $ColorType) -and ($null -ne ($consoleColor = $color -as [System.ConsoleColor]))) { $color = $consoleColor } diff --git a/src/PoshGitTypes.ps1 b/src/PoshGitTypes.ps1 index c078a1919..f3673cf4e 100644 --- a/src/PoshGitTypes.ps1 +++ b/src/PoshGitTypes.ps1 @@ -20,23 +20,31 @@ class PoshGitCellColor { $this.BackgroundColor = $BackgroundColor } - hidden static [string] ToString($color) { - $ansiTerm = "$([char]0x1b)[0m" + hidden [string] ToString($color) { + $ansiTerm = "$([char]27)[0m" $colorSwatch = " " + $str = "" if (!$color) { $str = "" } - elseif (Test-VirtualTerminalSequece $color) { - $txt = EscapeAnsiString $color - $str = "${color}${colorSwatch}${ansiTerm} $txt" + elseif (Test-VirtualTerminalSequece $color -Force) { + if ($global:GitPromptSettings.AnsiConsole) { + # Use '#' for FG color swatch since we are just applying the VT seqs as-is and + # a " " swatch char won't show anything for a FG color. + if ($color -eq $this.ForegroundColor) { + $colorSwatch = "#" + } + + $str = "${color}$colorSwatch${ansiTerm} " + } + + $str = "`"$(EscapeAnsiString $color)`"" } else { - $str = "" - if ($global:GitPromptSettings.AnsiConsole) { $bg = Get-BackgroundVirtualTerminalSequence $color - $str += "${bg}${colorSwatch}${ansiTerm} " + $str = "${bg}${colorSwatch}${ansiTerm} " } if ($color -is [int]) { @@ -82,9 +90,9 @@ class PoshGitCellColor { [string] ToString() { $str = "ForegroundColor: " - $str += [PoshGitCellColor]::ToString($this.ForegroundColor) + ", " + $str += $this.ToString($this.ForegroundColor) + ", " $str += "BackgroundColor: " - $str += [PoshGitCellColor]::ToString($this.BackgroundColor) + $str += $this.ToString($this.BackgroundColor) return $str } } @@ -93,48 +101,41 @@ class PoshGitTextSpan { [string]$Text [psobject]$BackgroundColor [psobject]$ForegroundColor - [string]$CustomAnsi PoshGitTextSpan() { $this.Text = "" $this.ForegroundColor = $null $this.BackgroundColor = $null - $this.CustomAnsi = $null } PoshGitTextSpan([string]$Text) { $this.Text = $Text $this.ForegroundColor = $null $this.BackgroundColor = $null - $this.CustomAnsi = $null } PoshGitTextSpan([string]$Text, [psobject]$ForegroundColor) { $this.Text = $Text $this.ForegroundColor = $ForegroundColor $this.BackgroundColor = $null - $this.CustomAnsi = $null } PoshGitTextSpan([string]$Text, [psobject]$ForegroundColor, [psobject]$BackgroundColor) { $this.Text = $Text $this.ForegroundColor = $ForegroundColor $this.BackgroundColor = $BackgroundColor - $this.CustomAnsi = $null } PoshGitTextSpan([PoshGitTextSpan]$PoshGitTextSpan) { $this.Text = $PoshGitTextSpan.Text $this.ForegroundColor = $PoshGitTextSpan.ForegroundColor $this.BackgroundColor = $PoshGitTextSpan.BackgroundColor - $this.CustomAnsi = $PoshGitTextSpan.CustomAnsi } PoshGitTextSpan([PoshGitCellColor]$PoshGitCellColor) { $this.Text = '' $this.ForegroundColor = $PoshGitCellColor.ForegroundColor $this.BackgroundColor = $PoshGitCellColor.BackgroundColor - $this.CustomAnsi = $null } [PoshGitTextSpan] Expand() { @@ -145,51 +146,43 @@ class PoshGitTextSpan { $execContext = Get-Variable ExecutionContext -ValueOnly $expandedText = $execContext.SessionState.InvokeCommand.ExpandString($this.Text) $newTextSpan = [PoshGitTextSpan]::new($expandedText, $this.ForegroundColor, $this.BackgroundColor) - $newTextSpan.CustomAnsi = $this.CustomAnsi return $newTextSpan } + # This method is used by Write-Prompt to render an instance of a PoshGitTextSpan when + # $GitPromptSettings.AnsiConsole is $true. It is also used by the default ToString() + # implementation to display any ANSI seqs when AnsiConsole is $true. [string] ToAnsiString() { $e = [char]27 + "[" - $txt = $this.Text - if ($this.CustomAnsi) { - $ansi = $this.CustomAnsi - $str = "${ansi}${txt}${e}0m" + $bg = $this.BackgroundColor + if (($null -ne $bg) -and !(Test-VirtualTerminalSequece $bg)) { + $bg = Get-BackgroundVirtualTerminalSequence $bg } - else { - $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 - } + $fg = $this.ForegroundColor + if (($null -ne $fg) -and !(Test-VirtualTerminalSequece $fg)) { + $fg = Get-ForegroundVirtualTerminalSequence $fg + } - if (($null -ne $fg) -or ($null -ne $bg)) { - $str = "${fg}${bg}${txt}${e}0m" - } - else { - $str = $txt - } + $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" } return $str } [string] ToEscapedString() { - if ($global:GitPromptSettings.AnsiConsole) { - $str = EscapeAnsiString $this.ToAnsiString() - } - else { - $str = $this.Text - } - + $str = EscapeAnsiString $this.ToAnsiString() return $str } + # This method is used when displaying the "value" of PoshGitTextSpan e.g. when evaluating $GitPromptSettings [string] ToString() { $sep = " " if ($this.Text.Length -lt 2) { @@ -197,23 +190,23 @@ class PoshGitTextSpan { } if ($global:GitPromptSettings.AnsiConsole) { - if ($this.CustomAnsi) { - $e = [char]27 + "[" - $ansi = $this.CustomAnsi - $escAnsi = EscapeAnsiString $this.CustomAnsi - $txt = $this.ToAnsiString() - $str = "Text: '$txt',${sep}CustomAnsi: '${ansi}${escAnsi}${e}0m'" + $txt = $this.ToAnsiString() + if (Test-VirtualTerminalSequece $txt) { + $escAnsi = "ANSI: `"$(EscapeAnsiString $txt)`"" + $str = "Text: `"$txt`",${sep}${escAnsi}" } else { - $color = [PoshGitCellColor]::new($this.ForegroundColor, $this.BackgroundColor) - $txt = $this.ToAnsiString() - $str = "Text: '$txt',${sep}$($color.ToString())" + $str = "Text: `"$txt`"" } } else { - $color = [PoshGitCellColor]::new($this.ForegroundColor, $this.BackgroundColor) $txt = $this.Text - $str = "Text: '$txt',${sep}$($color.ToString())" + if (Test-VirtualTerminalSequece $txt -Force) { + $txt = EscapeAnsiString $txt + } + + $color = [PoshGitCellColor]::new($this.ForegroundColor, $this.BackgroundColor) + $str = "Text: `"$txt`",${sep}$($color.ToString())" } return $str