Skip to content

Commit

Permalink
Merge pull request #513 from dahlbyk/rkeithhill/refactor-write-prompt
Browse files Browse the repository at this point in the history
Refactor Settings & Write-GitStatus function
  • Loading branch information
dahlbyk authored Jan 10, 2018
2 parents 1e9c231 + 2831c0c commit 64d2da4
Show file tree
Hide file tree
Showing 7 changed files with 1,143 additions and 335 deletions.
6 changes: 6 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Interactive Session",
"cwd": "${workspaceRoot}"
},
{
"type": "PowerShell",
"request": "launch",
Expand Down
41 changes: 36 additions & 5 deletions src/AnsiUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,60 @@ $AnsiEscape = [char]27 + "["
$ColorTranslatorType = 'System.Drawing.ColorTranslator' -as [Type]
$ColorType = 'System.Drawing.Color' -as [Type]

function EscapseAnsiString([string]$AnsiString) {
if ($PSVersionTable.PSVersion.Major -ge 6) {
$res = $AnsiString -replace "$([char]27)", '`e'
}
else {
$res = $AnsiString -replace "$([char]27)", '$([char]27)'
}

$res
}

function Test-VirtualTerminalSequece([psobject]$Object) {
if ($global:GitPromptSettings.AnsiConsole -and ($Object -is [string])) {
return $Object.Contains($AnsiEscape)
}
else {
return $false
}
}

function Get-VirtualTerminalSequence ($color, [int]$offset = 0) {
if ($color -is [byte]) {
return "${AnsiEscape}$(38 + $offset);5;${color}m"
}

if ($color -is [int]) {
$r = ($color -shr 16) -band 0xff
$g = ($color -shr 8) -band 0xff
$b = $color -band 0xff
return "${AnsiEscape}$(38 + $offset);2;${r};${g};${b}m"
}

if ($color -is [String]) {
try {
if ($ColorTranslatorType) {
$color = $ColorTranslatorType::FromHtml($color)
if ($null -ne ($color -as [System.ConsoleColor])) {
$color = [System.ConsoleColor]$color
}
else {
$color = [ConsoleColor]$color
elseif ($ColorTranslatorType) {
$color = $ColorTranslatorType::FromHtml($color)
}
}
catch {
Write-Debug $_
}
}

if ($ColorType -and ($color -is $ColorType)) {
return "${AnsiEscape}$(38 + $offset);2;$($color.R);$($color.G);$($color.B)m"
}
if (($color -is [ConsoleColor]) -and ($color -ge 0) -and ($color -le 15)) {

if (($color -is [System.ConsoleColor]) -and ($color -ge 0) -and ($color -le 15)) {
return "${AnsiEscape}$($ConsoleColorToAnsi[$color] + $offset)m"
}

return "${AnsiEscape}$($AnsiDefaultColor + $offset)m"
}

Expand Down
Loading

0 comments on commit 64d2da4

Please sign in to comment.