Skip to content

Commit

Permalink
Merge pull request #532 from dahlbyk/rkeithhill/fix-ansi-crossplat-bugs
Browse files Browse the repository at this point in the history
Fixes two bugs - one ANSI and one PS Core
  • Loading branch information
dahlbyk authored Jan 15, 2018
2 parents 8603e38 + 83a4653 commit 6bbee22
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function Write-Prompt {

if ($s.AnsiConsole) {
if ($Object -is [PoshGitTextSpan]) {
$str = $Object.RenderAnsi()
$str = $Object.ToAnsiString()
}
else {
$e = [char]27 + "["
Expand Down
104 changes: 75 additions & 29 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PoshGitCellColor {
$this.BackgroundColor = $BackgroundColor
}

hidden [string] ToString($color) {
hidden static [string] ToString($color) {
$ansiTerm = "$([char]0x1b)[0m"
$colorSwatch = " "

Expand All @@ -44,11 +44,41 @@ class PoshGitCellColor {
return $str
}

[string] ToEscapedString() {
if (!$global:GitPromptSettings.AnsiConsole) {
return ""
}

$str = ""

if ($this.ForegroundColor) {
if (Test-VirtualTerminalSequece $this.ForegroundColor) {
$str += EscapseAnsiString $this.ForegroundColor
}
else {
$seq = Get-ForegroundVirtualTerminalSequence $this.ForegroundColor
$str += EscapseAnsiString $seq
}
}

if ($this.BackgroundColor) {
if (Test-VirtualTerminalSequece $this.BackgroundColor) {
$str += EscapseAnsiString $this.BackgroundColor
}
else {
$seq = Get-BackgroundVirtualTerminalSequence $this.BackgroundColor
$str += EscapseAnsiString $seq
}
}

return $str
}

[string] ToString() {
$str = "ForegroundColor: "
$str += $this.ToString($this.ForegroundColor) + ", "
$str += [PoshGitCellColor]::ToString($this.ForegroundColor) + ", "
$str += "BackgroundColor: "
$str += $this.ToString($this.BackgroundColor)
$str += [PoshGitCellColor]::ToString($this.BackgroundColor)
return $str
}
}
Expand Down Expand Up @@ -101,31 +131,7 @@ class PoshGitTextSpan {
$this.CustomAnsi = $null
}

[string] ToString() {
if ($global:GitPromptSettings.AnsiConsole) {
if ($this.CustomAnsi) {
$e = [char]27 + "["
$ansi = $this.CustomAnsi
$escAnsi = EscapseAnsiString $this.CustomAnsi
$txt = $this.RenderAnsi()
$str = "Text: '$txt',`t CustomAnsi: '${ansi}${escAnsi}${e}0m'"
}
else {
$color = [PoshGitCellColor]::new($this.ForegroundColor, $this.BackgroundColor)
$txt = $this.RenderAnsi()
$str = "Text: '$txt',`t $($color.ToString())"
}
}
else {
$color = [PoshGitCellColor]::new($this.ForegroundColor, $this.BackgroundColor)
$txt = $this.Text
$str = "Text: '$txt',`t $($color.ToString())"
}

return $str
}

[string] RenderAnsi() {
[string] ToAnsiString() {
$e = [char]27 + "["
$txt = $this.Text

Expand All @@ -144,7 +150,47 @@ class PoshGitTextSpan {
$fg = Get-ForegroundVirtualTerminalSequence $fg
}

$str = "${fg}${bg}${txt}${e}0m"
if (($null -ne $fg) -or ($null -ne $bg)) {
$str = "${fg}${bg}${txt}${e}0m"
}
else {
$str = $txt
}
}

return $str
}

[string] ToEscapedString() {
if ($global:GitPromptSettings.AnsiConsole) {
$str = EscapseAnsiString $this.ToAnsiString()
}
else {
$str = $this.Text
}

return $str
}

[string] ToString() {
if ($global:GitPromptSettings.AnsiConsole) {
if ($this.CustomAnsi) {
$e = [char]27 + "["
$ansi = $this.CustomAnsi
$escAnsi = EscapseAnsiString $this.CustomAnsi
$txt = $this.ToAnsiString()
$str = "Text: '$txt',`t CustomAnsi: '${ansi}${escAnsi}${e}0m'"
}
else {
$color = [PoshGitCellColor]::new($this.ForegroundColor, $this.BackgroundColor)
$txt = $this.ToAnsiString()
$str = "Text: '$txt',`t $($color.ToString())"
}
}
else {
$color = [PoshGitCellColor]::new($this.ForegroundColor, $this.BackgroundColor)
$txt = $this.Text
$str = "Text: '$txt',`t $($color.ToString())"
}

return $str
Expand Down
7 changes: 6 additions & 1 deletion src/Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ function Add-PoshGitToProfile {
Adapted from http://www.west-wind.com/Weblog/posts/197245.aspx
#>
function Get-FileEncoding($Path) {
$bytes = [byte[]](Get-Content $Path -Encoding byte -ReadCount 4 -TotalCount 4)
if ($PSVersionTable.PSVersion.Major -ge 6) {
$bytes = [byte[]](Get-Content $Path -AsByteStream -ReadCount 4 -TotalCount 4)
}
else {
$bytes = [byte[]](Get-Content $Path -Encoding byte -ReadCount 4 -TotalCount 4)
}

if (!$bytes) { return 'utf8' }

Expand Down

0 comments on commit 6bbee22

Please sign in to comment.