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

Fixes two bugs - one ANSI and one PS Core #532

Merged
merged 2 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 65 additions & 4 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() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense for PoshGitCellColor to have a RenderAnsi(), such that this function is essentially EscapeAnsiString $this.RenderAnsi()? That function could then be used in PoshGitTextSpan.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, would ToAnsiString() be a better name than RenderAnsi()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I get for coding and watching the playoffs. :-) Renamed RenderAnsi() to ToAnsiString(). Can we do the [PoshGitCellColor] tweaks - ToAnsiString() and ToEscapedString() simplification in another PR?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that works. While you're at it, fix the typo in EscapseAnsiString. 😏

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! Yeah, will do.

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,6 +131,32 @@ class PoshGitTextSpan {
$this.CustomAnsi = $null
}

[string] ToEscapedString() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this whole method equivalent to EscapeAnsiString $this.RenderAnsi()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Done.

if ($global:GitPromptSettings.AnsiConsole) {
$ansiTerm = EscapseAnsiString ([char]27 + "[0m")

if ($this.CustomAnsi) {
$escAnsi = EscapseAnsiString $this.CustomAnsi
$str = "${escAnsi}$($this.Text)$ansiTerm"
}
else {
$color = [PoshGitCellColor]::new($this.ForegroundColor, $this.BackgroundColor)
$escAnsi = $color.ToEscapedString()
if ($escAnsi) {
$str = "${escAnsi}$($this.Text)$ansiTerm"
}
else {
$str = $this.Text
}
}
}
else {
$str = $this.Text
}

return $str
}

[string] ToString() {
if ($global:GitPromptSettings.AnsiConsole) {
if ($this.CustomAnsi) {
Expand Down Expand Up @@ -144,7 +200,12 @@ 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
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