Skip to content

Commit

Permalink
feat: Add support for PSReadline 2.0.0. Resolves #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmims committed Aug 29, 2018
1 parent 17c7ec9 commit 620057b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 39 deletions.
41 changes: 27 additions & 14 deletions PSConsoleTheme/Private/Out-Colors.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -188,7 +201,7 @@ $Script:PSColorMap = @{
'DarkGray' = @{
'Ansi' = @{
'Name' = 'Bright Black'
'FG' = '30;1'
'FG' = '90'
'BG' = '100'
}
'Cmd' = @{
Expand All @@ -200,7 +213,7 @@ $Script:PSColorMap = @{
'Red' = @{
'Ansi' = @{
'Name' = 'Bright Red'
'FG' = '31;1'
'FG' = '91'
'BG' = '101'
}
'Cmd' = @{
Expand All @@ -212,7 +225,7 @@ $Script:PSColorMap = @{
'Green' = @{
'Ansi' = @{
'Name' = 'Bright Green'
'FG' = '32;1'
'FG' = '92'
'BG' = '102'
}
'Cmd' = @{
Expand All @@ -224,7 +237,7 @@ $Script:PSColorMap = @{
'Yellow' = @{
'Ansi' = @{
'Name' = 'Bright Yellow'
'FG' = '33;1'
'FG' = '93'
'BG' = '103'
}
'Cmd' = @{
Expand All @@ -236,7 +249,7 @@ $Script:PSColorMap = @{
'Blue' = @{
'Ansi' = @{
'Name' = 'Bright Blue'
'FG' = '34;1'
'FG' = '94'
'BG' = '104'
}
'Cmd' = @{
Expand All @@ -248,7 +261,7 @@ $Script:PSColorMap = @{
'Magenta' = @{
'Ansi' = @{
'Name' = 'Bright Magenta'
'FG' = '35;1'
'FG' = '95'
'BG' = '105'
}
'Cmd' = @{
Expand All @@ -260,7 +273,7 @@ $Script:PSColorMap = @{
'Cyan' = @{
'Ansi' = @{
'Name' = 'Bright Cyan'
'FG' = '36;1'
'FG' = '96'
'BG' = '106'
}
'Cmd' = @{
Expand All @@ -272,7 +285,7 @@ $Script:PSColorMap = @{
'White' = @{
'Ansi' = @{
'Name' = 'Bright White'
'FG' = '37;1'
'FG' = '97'
'BG' = '107'
}
'Cmd' = @{
Expand Down
71 changes: 46 additions & 25 deletions PSConsoleTheme/Private/Set-TokenColorConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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"
Expand Down

0 comments on commit 620057b

Please sign in to comment.