diff --git a/PSConsoleTheme/Private/Out-Colors.ps1 b/PSConsoleTheme/Private/Out-Colors.ps1 index cf3c353..49c8817 100644 --- a/PSConsoleTheme/Private/Out-Colors.ps1 +++ b/PSConsoleTheme/Private/Out-Colors.ps1 @@ -18,15 +18,28 @@ function Out-Colors { $formatter.Deserialize($memStream) } - if (Get-Module PSReadline) { + if ($PSReadline = Get-Module PSReadLine) { $colorMap = CloneObject $Script:PSColorMap $options = Get-PSReadlineOption - $tokens = $options | Get-Member -MemberType Property -Name *ForegroundColor ` - | ForEach-Object { $_.Name -replace '(.+)ForegroundColor', '$1' } - foreach ($t in $tokens) { - $color = Invoke-Expression "`$options.$($t)ForegroundColor.ToString()" - $colorMap[$color].Tokens += $t + if ($PSReadline.Version.Major -ge 2) { + $tokens = $options | Get-Member -MemberType Property -Name *Color ` + | ForEach-Object { $_.Name -replace '(.+)Color', '$1' } + + foreach ($t in $tokens) { + $ansiColor = Invoke-Expression "`$options.$($t)Color.ToString()" + $ansiColor = [regex]::Replace($ansiColor, '.*\[((?:\d{1,3};?)+)m', '$1') + $color = ($colorMap.GetEnumerator() | Where-Object { $_.Value.Ansi.FG -in ($ansiColor -split ';') } | Select-Object -First 1).Key + $colorMap[$color].Tokens += $t + } + } else { + $tokens = $options | Get-Member -MemberType Property -Name *ForegroundColor ` + | ForEach-Object { $_.Name -replace '(.+)ForegroundColor', '$1' } + + foreach ($t in $tokens) { + $color = Invoke-Expression "`$options.$($t)ForegroundColor.ToString()" + $colorMap[$color].Tokens += $t + } } } @@ -188,7 +201,7 @@ $Script:PSColorMap = @{ 'DarkGray' = @{ 'Ansi' = @{ 'Name' = 'Bright Black' - 'FG' = '30;1' + 'FG' = '90' 'BG' = '100' } 'Cmd' = @{ @@ -200,7 +213,7 @@ $Script:PSColorMap = @{ 'Red' = @{ 'Ansi' = @{ 'Name' = 'Bright Red' - 'FG' = '31;1' + 'FG' = '91' 'BG' = '101' } 'Cmd' = @{ @@ -212,7 +225,7 @@ $Script:PSColorMap = @{ 'Green' = @{ 'Ansi' = @{ 'Name' = 'Bright Green' - 'FG' = '32;1' + 'FG' = '92' 'BG' = '102' } 'Cmd' = @{ @@ -224,7 +237,7 @@ $Script:PSColorMap = @{ 'Yellow' = @{ 'Ansi' = @{ 'Name' = 'Bright Yellow' - 'FG' = '33;1' + 'FG' = '93' 'BG' = '103' } 'Cmd' = @{ @@ -236,7 +249,7 @@ $Script:PSColorMap = @{ 'Blue' = @{ 'Ansi' = @{ 'Name' = 'Bright Blue' - 'FG' = '34;1' + 'FG' = '94' 'BG' = '104' } 'Cmd' = @{ @@ -248,7 +261,7 @@ $Script:PSColorMap = @{ 'Magenta' = @{ 'Ansi' = @{ 'Name' = 'Bright Magenta' - 'FG' = '35;1' + 'FG' = '95' 'BG' = '105' } 'Cmd' = @{ @@ -260,7 +273,7 @@ $Script:PSColorMap = @{ 'Cyan' = @{ 'Ansi' = @{ 'Name' = 'Bright Cyan' - 'FG' = '36;1' + 'FG' = '96' 'BG' = '106' } 'Cmd' = @{ @@ -272,7 +285,7 @@ $Script:PSColorMap = @{ 'White' = @{ 'Ansi' = @{ 'Name' = 'Bright White' - 'FG' = '37;1' + 'FG' = '97' 'BG' = '107' } 'Cmd' = @{ diff --git a/PSConsoleTheme/Private/Set-TokenColorConfiguration.ps1 b/PSConsoleTheme/Private/Set-TokenColorConfiguration.ps1 index 41441c5..b4ce362 100644 --- a/PSConsoleTheme/Private/Set-TokenColorConfiguration.ps1 +++ b/PSConsoleTheme/Private/Set-TokenColorConfiguration.ps1 @@ -77,23 +77,32 @@ function Set-TokenColorConfiguration { Set-TokenColorConfiguration -Reset } - if (Get-Module PSReadLine) { + + $psr2colors = @{} + + if ($PSReadline = Get-Module PSReadLine) { Write-Debug "$action Readline Tokens" # Breaking changes are coming in PSReadLine 2.0. Colors should be set via the -Color parameter with a hashtable foreach ($token in @('ContinuationPrompt', 'DefaultToken', 'Comment', 'Keyword', 'String', 'Operator', 'Variable', 'Command', 'Parameter', 'Type', 'Number', 'Member', 'Emphasis', 'Error')) { + Write-Debug "TOKEN $token" if ($tokenColors.foreground -and (Get-Member $token -InputObject ($tokenColors.foreground))) { - if ($token -in @('ContinuationPrompt', 'Emphasis', 'Error')) { - $expression = "Set-PSReadlineOption -$($token)ForegroundColor $($tokenColors.foreground.($token))" - Write-Debug $expression - Invoke-Expression $expression - } - elseif ($token -eq 'DefaultToken') { - Write-Debug "Set-PSReadlineOption 'None' -ForegroundColor $($tokenColors.foreground.($token))" - Set-PSReadlineOption 'None' -ForegroundColor $tokenColors.foreground.($token) - } - else { - Write-Debug "Set-PSReadlineOption $token -ForegroundColor $($tokenColors.foreground.($token))" - Set-PSReadlineOption $token -ForegroundColor $tokenColors.foreground.($token) + if ($PSReadline.Version.Major -ge 2) { + Write-Debug "Add Foreground $($token): $($tokenColors.foreground.($token))" + $psr2colors.Add($token, "$([char]0x1b)[$($Script:PSColorMap[$tokenColors.foreground.($token)].Ansi.FG)") + } else { + if ($token -in @('ContinuationPrompt', 'Emphasis', 'Error')) { + $expression = "Set-PSReadlineOption -$($token)ForegroundColor $($tokenColors.foreground.($token))" + Write-Debug $expression + Invoke-Expression $expression + } + elseif ($token -eq 'DefaultToken') { + Write-Debug "Set-PSReadlineOption 'None' -ForegroundColor $($tokenColors.foreground.($token))" + Set-PSReadlineOption 'None' -ForegroundColor $tokenColors.foreground.($token) + } + else { + Write-Debug "Set-PSReadlineOption $token -ForegroundColor $($tokenColors.foreground.($token))" + Set-PSReadlineOption $token -ForegroundColor $tokenColors.foreground.($token) + } } } @@ -102,21 +111,33 @@ function Set-TokenColorConfiguration { $background = $tokenColors.background.($token) } if ($background) { - if ($token -in @('ContinuationPrompt', 'Emphasis', 'Error')) { - $expression = "Set-PSReadlineOption -$($token)BackgroundColor $background" - Write-Debug $expression - Invoke-Expression $expression - } - elseif ($token -eq 'DefaultToken') { - Write-Debug "Set-PSReadlineOption 'None' -BackgroundColor $background" - Set-PSReadlineOption 'None' -BackgroundColor $background - } - else { - Write-Debug "Set-PSReadlineOption $token -BackgroundColor $background" - Set-PSReadlineOption $token -BackgroundColor $background + if ($PSReadline.Version.Major -ge 2) { + Write-Debug "Add Background $($token): $background" + $psr2colors[$token] += ";$($Script:PSColorMap[$background].Ansi.BG)" + } else { + if ($token -in @('ContinuationPrompt', 'Emphasis', 'Error')) { + $expression = "Set-PSReadlineOption -$($token)BackgroundColor $background" + Write-Debug $expression + Invoke-Expression $expression + } + elseif ($token -eq 'DefaultToken') { + Write-Debug "Set-PSReadlineOption 'None' -BackgroundColor $background" + Set-PSReadlineOption 'None' -BackgroundColor $background + } + else { + Write-Debug "Set-PSReadlineOption $token -BackgroundColor $background" + Set-PSReadlineOption $token -BackgroundColor $background + } } } } + + if (($PSReadline.Version.Major -ge 2) -and ($psr2colors.Count -gt 0)) { + foreach ($key in @($psr2colors.Keys)) { + $psr2colors[$key] += 'm' + } + Set-PSReadLineOption -Colors $psr2colors + } } Write-Debug "$action Write Tokens"