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

WIP: Reorgs settings using PS v5 classes #499

Closed
wants to merge 4 commits into from
Closed
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
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
24 changes: 24 additions & 0 deletions src/AnsiUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,31 @@ $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]0x1B)", '`e'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pick either [char]0x1b or [char]27 and stay consistent.

}
else {
$res = $AnsiString -replace "$([char]0x1B)", '$([char]0x1B)'
}

$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 [String]) {
try {
if ($ColorTranslatorType) {
Expand All @@ -41,12 +62,15 @@ function Get-VirtualTerminalSequence ($color, [int]$offset = 0) {
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)) {
return "${AnsiEscape}$($ConsoleColorToAnsi[$color] + $offset)m"
}

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

Expand Down
Loading