配置浅色主题
为深色背景终端选择 PowerShell 和 PSReadLine 的默认颜色。 但是,一些用户可能会选择使用浅色背景和深色文本。 由于大多数默认颜色不会设置背景,因此在浅色背景上使用浅色前景色会产生不可读的文本。
PSReadLine 允许你为 18 个不同的语法元素定义颜色。 可以使用 Get-PSReadLineOption
cmdlet 查看当前设置。
EditMode : Windows
AddToHistoryHandler : System.Func`2[System.String,System.Object]
HistoryNoDuplicates : True
HistorySavePath : C:\Users\user1\AppData\Roaming\Microsoft\Wind...
HistorySaveStyle : SaveIncrementally
HistorySearchCaseSensitive : False
HistorySearchCursorMovesToEnd : False
MaximumHistoryCount : 4096
ContinuationPrompt : >>
ExtraPromptLineCount : 0
PromptText : {> }
BellStyle : Audible
DingDuration : 50
DingTone : 1221
CommandsToValidateScriptBlockArguments : {ForEach-Object, %, Invoke-Command, icm...}
CommandValidationHandler :
CompletionQueryItems : 100
MaximumKillRingCount : 10
ShowToolTips : True
ViModeIndicator : None
WordDelimiters : ;:,.[]{}()/\|^&*-=+'"-—―
AnsiEscapeTimeout : 100
PredictionSource : HistoryAndPlugin
PredictionViewStyle : InlineView
CommandColor : "`e[93m"
CommentColor : "`e[32m"
ContinuationPromptColor : "`e[37m"
DefaultTokenColor : "`e[37m"
EmphasisColor : "`e[96m"
ErrorColor : "`e[91m"
InlinePredictionColor : "`e[38;5;238m"
KeywordColor : "`e[92m"
ListPredictionColor : "`e[33m"
ListPredictionSelectedColor : "`e[48;5;238m"
MemberColor : "`e[97m"
NumberColor : "`e[97m"
OperatorColor : "`e[90m"
ParameterColor : "`e[90m"
SelectionColor : "`e[30;47m"
StringColor : "`e[36m"
TypeColor : "`e[37m"
VariableColor : "`e[92m"
颜色设置存储为字符串,其中包含更改终端中颜色的 ANSI 转义序列。 使用 Set-PSReadLineOption
cmdlet,可以将颜色更改为更适合浅色背景的值。
定义浅色主题的颜色
可以将 PowerShell ISE 配置为对编辑器窗格和控制台窗格使用浅色主题。 还可以查看和更改 ISE 用于各种语法和输出类型的颜色。 可以使用这些颜色选项为 PSReadLine 定义类似的主题。
以下哈希表定义 PSReadLine 的颜色,这些颜色模拟 PowerShell ISE 中的颜色。
$ISETheme = @{
Command = $PSStyle.Foreground.FromRGB(0x0000FF)
Comment = $PSStyle.Foreground.FromRGB(0x006400)
ContinuationPrompt = $PSStyle.Foreground.FromRGB(0x0000FF)
Default = $PSStyle.Foreground.FromRGB(0x0000FF)
Emphasis = $PSStyle.Foreground.FromRGB(0x287BF0)
Error = $PSStyle.Foreground.FromRGB(0xE50000)
InlinePrediction = $PSStyle.Foreground.FromRGB(0x93A1A1)
Keyword = $PSStyle.Foreground.FromRGB(0x00008b)
ListPrediction = $PSStyle.Foreground.FromRGB(0x06DE00)
Member = $PSStyle.Foreground.FromRGB(0x000000)
Number = $PSStyle.Foreground.FromRGB(0x800080)
Operator = $PSStyle.Foreground.FromRGB(0x757575)
Parameter = $PSStyle.Foreground.FromRGB(0x000080)
String = $PSStyle.Foreground.FromRGB(0x8b0000)
Type = $PSStyle.Foreground.FromRGB(0x008080)
Variable = $PSStyle.Foreground.FromRGB(0xff4500)
ListPredictionSelected = $PSStyle.Background.FromRGB(0x93A1A1)
Selection = $PSStyle.Background.FromRGB(0x00BFFF)
}
注意
在 PowerShell 7.2 及更高版本中,可以使用 $PSStyle
的 FromRGB()
方法为所需颜色创建 ANSI 转义序列。
有关 $PSStyle
的详细信息,请参阅 about_ANSI_Terminals。
有关 ANSI 转义序列的详细信息,请参阅维基百科中的 ANSI 转义代码一文。
在配置文件中设置颜色主题
若要在每个 PowerShell 会话中获得所需的颜色设置,必须将配置设置添加到 PowerShell 配置文件脚本。 有关示例,请参阅自定义 shell 环境
将 $ISETheme
变量和以下 Set-PSReadLineOption
命令添加到配置文件。
Set-PSReadLineOption -Colors $ISETheme
从 PowerShell 7.2 开始,PowerShell 向默认控制台体验添加彩色输出。 使用的颜色在 $PSStyle
变量中定义,专用于深色背景。 以下设置更适用于浅色背景终端。
$PSStyle.Formatting.FormatAccent = "`e[32m"
$PSStyle.Formatting.TableHeader = "`e[32m"
$PSStyle.Formatting.ErrorAccent = "`e[36m"
$PSStyle.Formatting.Error = "`e[31m"
$PSStyle.Formatting.Warning = "`e[33m"
$PSStyle.Formatting.Verbose = "`e[33m"
$PSStyle.Formatting.Debug = "`e[33m"
$PSStyle.Progress.Style = "`e[33m"
$PSStyle.FileInfo.Directory = $PSStyle.Background.FromRgb(0x2f6aff) +
$PSStyle.Foreground.BrightWhite
$PSStyle.FileInfo.SymbolicLink = "`e[36m"
$PSStyle.FileInfo.Executable = "`e[95m"
$PSStyle.FileInfo.Extension['.ps1'] = "`e[36m"
$PSStyle.FileInfo.Extension['.ps1xml'] = "`e[36m"
$PSStyle.FileInfo.Extension['.psd1'] = "`e[36m"
$PSStyle.FileInfo.Extension['.psm1'] = "`e[36m"
为辅助功能选择颜色
ISE 颜色主题可能不适用于色盲用户或具有其他限制其查看颜色能力的情况的用户。
万维网联合会 (W3C) 提供了有关为辅助功能使用颜色的建议。 Web 内容辅助功能指南 (WCAG) 2.1 建议“文本和文本图像的视觉呈现的对比度至少为 4.5:1。”有关详细信息,请参阅成功标准 1.4.3 对比度(最小值)。
对比度网站提供了一个工具,可用于选取前景色和背景色并测量对比度。 可以使用此工具查找最适合你的颜色组合。