Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance PoshGitTextSpan support for custom VT seqs #616

Merged
merged 6 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions src/AnsiUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
rkeithhill marked this conversation as resolved.
Show resolved Hide resolved
if (($Force -or $global:GitPromptSettings.AnsiConsole) -and ($obj -is [string])) {
$obj.Contains($AnsiEscape)
}
else {
$false
}
}
}

Expand All @@ -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
}
Expand Down
103 changes: 48 additions & 55 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<default>"
}
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]) {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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() {
Expand All @@ -145,75 +146,67 @@ 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) {
$sep = " " * (3 - $this.Text.Length)
}

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
Expand Down