当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 配置浅色主题

作者:精品下载站 日期:2024-12-14 03:04:39 浏览:15 分类:玩电脑

配置浅色主题


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 定义类似的主题。

以下哈希表定义了模仿 PowerShell ISE 中颜色的 PSReadLine 颜色。

$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 及更高版本中,您可以使用 $PSStyleFromRGB() 方法为所需的颜色创建 ANSI 转义序列。

有关 $PSStyle 的详细信息,请参阅 about_ANSI_Terminals。

有关 ANSI 转义序列的更多信息,请参阅 Wikipedia 中的 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) 针对使用颜色以实现可访问性提出了建议。 《网络内容可访问性指南》(WCAG) 2.1 建议“文本和文本图像的视觉呈现的对比度至少为 4.5:1”。有关详细信息,请参阅成功标准 1.4.3 对比度(最小值)。

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

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯