跳转到主内容

颜色配置

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

标准颜色

Oh My Posh 支持多种不同的颜色引用方式:

  • 真彩色(True color)使用 十六进制颜色值(例如 #CB4B16)。

  • 16种 ANSI 颜色名称: 包含8种基础ANSI颜色和default

    black red green yellow blue magenta cyan white default

    以及8种扩展ANSI颜色:

    darkGray lightRed lightGreen lightYellow lightBlue lightMagenta lightCyan lightWhite

  • 256色模式使用数字表示: 例如 0 代表黑色,1 代表红色,2 代表绿色等。

  • transparent 关键字可用于创建透明前景覆盖 或通过段落的前景色属性设置透明背景。

  • foreground 关键字可引用当前段落的前景色。

  • background 关键字可引用当前段落的背景色。

  • parentForeground 关键字可继承前一个活动段落的前景色。

  • parentBackground 关键字可继承前一个活动段落的背景色。

  • accent 关键字引用操作系统强调色(仅限 Windows 和 macOS)。

颜色模板

通过字符串模板数组根据当前上下文动态定义颜色。 底层使用 Go 的 text/template 功能(扩展了 sprig), 提供若干标准属性供使用。段落相关属性请查阅文档的模板属性 章节,通用模板属性详见此处

以下示例基于 AWS 段落

{
"type": "aws",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#111111",
"foreground_templates": [
"{{if contains \"default\" .Profile}}#FFA400{{end}}",
"{{if contains \"jan\" .Profile}}#f1184c{{end}}"
]
}

逻辑如下:当foreground_templates包含数组时,我们会逐行检查模板直到 返回非空字符串。例如当.Profile内容包含default时,第一个模板 返回#FFA400并作为最终颜色;若包含jan则返回#f1184c。当所有模板 均未返回值时,将使用#ffffff作为前景色回退值。

颜色覆盖

您可以在支持颜色设置的任意属性中覆盖文本的前景/背景色。 语法为自定义格式但直观易懂:<foreground,background>text</>。例如 <#ffffff,#000000>this is white with black background</> <#FF479C>but this is pink</>。 位于颜色起始符<#FF479C>和结束符</>之间的内容将应用对应颜色。

若需要输出与段落foreground不同的彩色括号:

{
"template": "<#CB4B16>┏[</>"
}

若需同时修改背景色:

{
"template": "<#CB4B16,#FFFFFF>┏[</>"
}

若仅修改背景色,省略上述字符串中的第一个颜色:

{
"template": "<,#FFFFFF>┏[</>"
}

调色板

若配置中定义了调色板,可在需要标准颜色的位置使用_调色板引用_p:<palette key>

定义调色板

调色板是具名的标准颜色集合。使用时在配置顶层定义"palette"对象:

{
"palette": {
"git-foreground": "#193549",
"git": "#FFFB38",
"git-modified": "#FF9248",
"git-diverged": "#FF4500",
"git-ahead": "#B388FF",
"git-behind": "#B388FF",
"red": "#FF0000",
"green": "#00FF00",
"blue": "#0000FF",
"white": "#FFFFFF",
"black": "#111111"
}
}

调色板键名可以是任意字符串,尽情发挥创意。 但颜色值必须遵循标准颜色格式规范。

使用调色板

现在可以在任何段配置foregroundforeground_templatesbackgroundbackground_templates属性中使用_调色板引用_, 以及其他接受标准颜色值的配置属性。_调色板引用_格式为p:<palette key>。参考Git段使用_调色板引用_的示例:

{
"type": "git",
"style": "powerline",
"powerline_symbol": "",
"foreground": "p:git-foreground",
"background": "p:git",
"background_templates": [
"{{ if or (.Working.Changed) (.Staging.Changed) }}p:git-modified{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}p:git-diverged{{ end }}",
"{{ if gt .Ahead 0 }}p:git-ahead{{ end }}",
"{{ if gt .Behind 0 }}p:git-behind{{ end }}"
]
}

将所有颜色集中定义可实现:

  • 导入现有配色配置(通常只需微调格式)
  • 批量修改多个段落的颜色
  • 整体配置更清晰有序 尽情发挥创意吧!

_调色板引用_与标准颜色

调色板与标准颜色可并存使用。你仍可在任意位置使用标准颜色, 这在为单个段落元素指定特殊颜色或进行_颜色覆盖_时非常实用(参考Battery段):

{
"type": "battery",
"style": "powerline",
"invert_powerline": true,
"powerline_symbol": "",
"foreground": "p:white",
"background": "p:black",
"options": {
"discharging_icon": "<#ffa500>-</> ",
"charging_icon": "+ ",
"charged_icon": "* "
}
}

无效引用处理

若使用了无效的_调色板引用_(例如将p:blue误拼为p:bleu), 调色板引擎将使用 Transparent 关键字作为回退值。因此若发现提示段落 显示颜色异常且正在使用调色板时,请务必检查引用是否正确。

递归解析

调色板支持递归解析_调色板引用_。你可以在调色板值中嵌套引用其他调色板颜色, 实现颜色定义的链式调用。例如以下配置中p:foregroundp:background 将正确解析为"#CAF0F8"和"#023E8A":

{
"palette": {
"light-blue": "#CAF0F8",
"dark-blue": "#023E8A",
"foreground": "p:light-blue",
"background": "p:dark-blue"
}
}

多调色板

如需根据条件(如深色/浅色模式)使用 palette,可定义多个 palettes 和一个解析为 palette 键的模板template 会在运行时实时计算,因此提示符可随时基于 template 的结果动态更新。

参考以下配置:

{
"palettes": {
"template": "{{ if eq .Shell \"pwsh\" }}latte{{ else }}frappe{{ end }}",
"list": {
"latte": {
"black": "#262B44",
"green": "#59C9A5",
"orange": "#F07623",
"red": "#e64553",
"white": "#E0DEF4",
"yellow": "#df8e1d",
"blue": "#7287fd"
},
"frappe": {
"black": "#262B44",
"green": "#59C9A5",
"orange": "#F07623",
"red": "#D81E5B",
"white": "#E0DEF4",
"yellow": "#F3AE35",
"blue": "#4B95E9"
}
}
}
}

此配置中,当shell为pwsh时使用latte调色板,其他情况使用frappe调色板。 你还可以将frappe设为默认palette,这样当template无法解析匹配项时将回退至默认调色板。 若未定义任何匹配且未设置palette,所有templates/colors中的调色板引用将回退至transparent

为避免颜色重复定义,可将共享颜色放在基础palette中,再通过多个调色板引用:

{
"palette": {
"black": "#262B44",
"green": "#59C9A5",
"orange": "#F07623"
},
"palettes": {
"template": "{{ if eq .Shell \"pwsh\" }}latte{{ else }}frappe{{ end }}",
"list": {
"latte": {
"red": "#e64553",
"white": "#E0DEF4",
"yellow": "#df8e1d",
"blue": "#7287fd"
},
"frappe": {
"red": "#D81E5B",
"white": "#E0DEF4",
"yellow": "#F3AE35",
"blue": "#4B95E9"
}
}
}
}
信息

当基础调色板(palette)与多调色板(palettes)存在同名颜色时, 多调色板的颜色定义优先级更高。

循环配色

当需要为不同段落循环使用固定颜色序列(背景色+前景色)时, 可使用cycle属性。该属性定义的颜色列表会按顺序循环应用, 且优先级始终高于其他配色设置。

{
"cycle": [
{
"background": "p:blue",
"foreground": "p:white"
},
{
"background": "p:green",
"foreground": "p:black"
},
{
"background": "p:orange",
"foreground": "p:white"
}
]
}