配置浅色主题

为深色背景终端选择的默认颜色适用于 PowerShell 和 PSReadLine。 但是,某些用户可能会选择使用浅色背景和深色文本。 由于大多数默认颜色未设置背景,因此在浅色背景上使用浅色前景颜色会产生不可读的文本。

从 PowerShell 7.2 开始,PowerShell 会将彩色输出添加到默认控制台体验。 Windows PowerShell 中没有原生支持的 $PSStyle 功能。 但是,使用 PowerShell 库中的 PSStyle 模块,可以使用本文中所述的相同技术设置颜色值。

所使用的颜色在变量中 $PSStyle 定义,设计用于深色背景。 您可以更改这些颜色,使其在浅色背景的终端上效果更佳。

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 转义序列。 使用 cmdlet Set-PSReadLineOption,可以将颜色更改为更适合浅色背景的值。

为浅色主题定义颜色

可以将 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)
}

注释

可以使用该方法 FromRGB() 为所需颜色创建 ANSI 转义序列。 有关 $PSStyle的详细信息,请参阅 about_ANSI_Terminals。 有关 ANSI 转义序列的详细信息,请参阅维基百科中 ANSI 转义代码 一文。

在个人资料中设置颜色主题

若要在每个 PowerShell 会话中使用所需的颜色设置,必须将配置设置添加到 PowerShell 配置文件脚本。 有关示例,请参阅 自定义 shell 环境

$ISETheme 变量和以下命令 Set-PSReadLineOption 添加到配置文件。

Set-PSReadLineOption -Colors $ISETheme

以下设置对浅色背景的终端效果更好。

$PSStyle.Formatting.FormatAccent       = $PSStyle.Foreground.Green
$PSStyle.Formatting.TableHeader        = $PSStyle.Foreground.Green
$PSStyle.Formatting.ErrorAccent        = $PSStyle.Foreground.Cyan
$PSStyle.Formatting.Error              = $PSStyle.Foreground.Red
$PSStyle.Formatting.Warning            = $PSStyle.Foreground.Yellow
$PSStyle.Formatting.Verbose            = $PSStyle.Foreground.Yellow
$PSStyle.Formatting.Debug              = $PSStyle.Foreground.Yellow
$PSStyle.Progress.Style                = $PSStyle.Foreground.Yellow
$PSStyle.FileInfo.Directory            = $PSStyle.Background.FromRgb(0x2f6aff) +
                                         $PSStyle.Foreground.BrightWhite
$PSStyle.FileInfo.SymbolicLink         = $PSStyle.Foreground.Cyan
$PSStyle.FileInfo.Executable           = $PSStyle.Foreground.BrightMagenta
$PSStyle.FileInfo.Extension['.ps1']    = $PSStyle.Foreground.Cyan
$PSStyle.FileInfo.Extension['.ps1xml'] = $PSStyle.Foreground.Cyan
$PSStyle.FileInfo.Extension['.psd1']   = $PSStyle.Foreground.Cyan
$PSStyle.FileInfo.Extension['.psm1']   = $PSStyle.Foreground.Cyan

为无障碍设计选择颜色

ISE 颜色主题可能不适用于色盲或限制其查看颜色的其他条件的用户。

万维网联盟(W3C)对如何使用颜色以提高可访问性提出了推荐。 Web 内容辅助功能指南 (WCAG) 2.1 建议“文本和文本图像的视觉呈现具有至少 4.5:1 的对比度。有关详细信息,请参阅成功条件 1.4.3 对比度(最小值)。

对比度网站提供了一个工具,可用于选取前景和背景色并测量对比度。 可以使用此工具查找最适合你的颜色组合。