解决方案取决于您使用的 PSReadLine 模块的版本:
- PSReadline v2.x,随 PowerShell (Core) 7+ 提供。
- PSReadline v1.x,在 Windows 10+ / Windows Sever 2016+ 中附带 Windows PowerShell 版本,尽管可以在中按需安装 2.x Windows PowerShell 5.0 和 5.1 (
Install-Module PSReadLine)
使用(Get-Module PSReadLine).Version.ToString() 查看您使用的版本。
PSReadline v2.x 解决方案:
要获取所有颜色属性的名称,通过过滤以color 结尾的名称(不区分大小写),使用反射 em>,PowerShell 通过 intrinsic .psobject property:
(Get-PSReadLineOption).psobject.Properties.Name -like '*color'
注意:
-
打印所有感兴趣的属性名称(包括它们的值)的快速方法是使用
Get-PSReadLineOption | Select-Object *color。但是,结果是[pscustomobject],而不是哈希表,为了将前者转换为后者,您需要再次使用下面显示的相同技术。
-
但是,作为通过Set-PSReadLineOption 设置颜色的替代方法(如下所示),您还可以使用底部部分中针对 PSReadLine v1.x 描述的方法(例如, (Get-PSReadlineOption).CommentColor = 'DarkGray' ),在这种情况下,您可以按原样使用 Get-PSReadLineOption | Select-Object *color 的输出中显示的属性名称。请注意,v2.x 和 v1.x 之间的属性名称不同;此外,与下面的 Set-PSReadLineOption -Colors 一样,设置 背景 颜色需要使用 VT 转义序列。
警告:这些属性名称需要转换才能用作您可以传递给Set-PSReadLineOption -Colors的哈希表的键(从 PSReadLine 2.1.0 开始):
后缀Color必须去掉,对于DefaultTokenColor,后缀TokenColor,下面的-replace操作就是这样做的:
(Get-PSReadLineOption).psobject.Properties.Name -like '*color' `
-replace '(Token)?Color$'
这会产生以下 color key names(从 PSReadLine 2.1.0 开始):
ContinuationPrompt
Default
Comment
Keyword
String
Operator
Variable
Command
Parameter
Type
Number
Member
Emphasis
Error
Selection
InlinePrediction
您现在可以传递一个 有选择地更新颜色的哈希表;例如下面将Comment (CommentColor) 颜色设置为深灰色(同时保持所有其他颜色不变):
Set-PSReadLineOption -Color @{ Comment = 'DarkGray' }
颜色值可以是:
要创建包含 所有 个颜色键的哈希表,使用当前属性值作为起点,请使用以下命令:
$allColors = @{}
(Get-PSReadLineOption).psobject.Properties.
Where({ $_.Name -like '*color' }).
ForEach({ $allColors[$_.Name -replace '(Token)?Color$'] = $_.Value })
注意:当您打印 $allColors 时,Value 列可能显示为空,但值存在,形式为 - 它们本身不可见 - 虚拟终端 (VT) 控件序列。
PSReadline v1.x 解决方案(Windows PowerShell 的旧版本):
与 v2.x 版本不同:
-
Set-PSReadlineOption 在 v1.x 中使用 组合 -TokenKind 和 -ForegroundColor / -BackgroundColor 参数用于各种颜色,对于一般颜色设置,单独参数(例如-ErrorForegroundColor)(而不是接受哈希表的单个v2.x -Colors参数);此外,所有颜色设置都分为 foreground 和 background 值。
-
颜色值仅限于[ConsoleColor]枚举类型的值,如'DarkGray'([ConsoleColor]::DarkGray)
对于设置颜色的统一方法,您可以放弃Set-PSReadLineOption 并直接设置Get-PSReadLineOption 返回的Microsoft.PowerShell.PSConsoleReadLineOptions 实例的属性。
例如,下面将 cmets 的前景色设置为深灰色:
(Get-PSReadlineOption).CommentForegroundColor = 'DarkGray'
这等效于以下 v1.x Set-PSReadLineOption 调用:
Set-PSReadLineOption -TokenKind Comment -ForegroundColor DarkGray
要获取所有颜色相关属性的名称,请使用与 v2.x 相同的方法:
(Get-PSReadLineOption).psobject.Properties.Name -like '*color'
这会产生以下颜色属性名称:
ContinuationPromptForegroundColor
ContinuationPromptBackgroundColor
DefaultTokenForegroundColor
CommentForegroundColor
KeywordForegroundColor
StringForegroundColor
OperatorForegroundColor
VariableForegroundColor
CommandForegroundColor
ParameterForegroundColor
TypeForegroundColor
NumberForegroundColor
MemberForegroundColor
DefaultTokenBackgroundColor
CommentBackgroundColor
KeywordBackgroundColor
StringBackgroundColor
OperatorBackgroundColor
VariableBackgroundColor
CommandBackgroundColor
ParameterBackgroundColor
TypeBackgroundColor
NumberBackgroundColor
MemberBackgroundColor
EmphasisForegroundColor
EmphasisBackgroundColor
ErrorForegroundColor
ErrorBackgroundColor