跳转到主内容

模板

非官方测试版翻译

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

每个段落都有 template 属性,用于自定义显示文本。 底层实现使用了 Go 语言的 text/template 功能,并扩展了 sprig 库, 同时提供了一些标准属性供开发者使用。

全局属性

这些属性可在任意段落中使用。若某段落包含同名属性, 将优先使用段落属性值。如需引用全局属性, 可通过 .$ 前缀直接访问。

NameTypeDescription
.Rootbooleanis the current user root/admin or not
.PWDstringthe current working directory (~ for $HOME)
.AbsolutePWDstringthe current working directory (unaltered)
.PSWDstringthe current non-filesystem working directory in PowerShell
.Folderstringthe current working folder
.Shellstringthe current shell name. The value may be overriden by [maps.shell_name][maps].
.ShellVersionstringthe current shell version
.SHLVLintthe current shell level
.UserNamestringthe current user name
.HostNamestringthe host name
.Codeintthe last exit code
.Jobsintnumber of background jobs (only available for zsh, PowerShell, and Nushell)
.OSstringthe operating system
.WSLbooleanin WSL yes/no
.Templatesstringthe templates result
.PromptCountintthe prompt counter, increments with 1 for every prompt invocation
.Versionstringthe Oh My Posh version
.SegmentSegmentthe current segment's metadata

段落

NameTypeDescription
.Segment.Indexintthe current segment's index (as rendered)
.Segment.Textstringthe segment's rendered text

环境变量

NameTypeDescription
.Env.VarNamestringAny environment variable where VarName is the environment variable name
技巧

对于以下列出的 shell,您可以重写函数在提示符渲染前执行自定义逻辑, 例如将特定上下文内容存入环境变量。

function Set-EnvVar([bool]$originalStatus) {
$env:POSH=$(Get-Date)
}
New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global -Force

配置变量

NameTypeDescription
.Var.VarNameanyAny config variable where VarName is the variable name

示例

{
"version": 4,
"var": {
"Hello": "hello",
"World": "world"
},
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
{
"type": "text",
"style": "plain",
"foreground": "p:white",
"template": "{{ .Var.Hello }} {{ .Var.World }} "
}
]
}
]
}

模板逻辑

TemplateDescription
{{.}}Root element.
{{.Var}}Variable in a struct, where Var is a variable.
{{- .Var -}}Remove extra white space surrounding the .Var variable and remove the newline. Either side is fine too.
{{ $planet := "Earth"}}{{ $planet }} Store a value in a custom variable to reference later. Note that .$ is used to reference the global/parent context, like in the full example below with $.
Hi {{if .Name}} {{.Name}} {{else}} visitor {{end}}If-else statement. If will evaluate whether or not the argument is empty. Using the elseif conditional is also an option. The not negation is available too.
{{if and .Arg1 .Arg2}} both complete. {{else}} incomplete. {{end}}The and function compares multiple arguments to return the boolean AND (if arg1 then arg2 else arg1). Both arguments are evaluated. The or function compares multiple arguments to return the boolean OR. Similar to if arg1 then arg1 else arg2, so arg2 will never be evaluated if arg1 is false (not empty).
{{with .Var}} {{end}}With statement, where Var is a variable. It skips the block if the variable is absent.
{{range .Array}} {{end}}Range statement, where Array is an array, slice, map, or channel.
{{ lt 3 4 }}This lt comparison function evaluates to true since 3 is less than 4 (other boolean operators: eq, ne, lt, le, gt, ge).

辅助函数

Sprig

Oh My Posh 内置所有 sprig 函数,您可以直接在模板中进行字符串操作、 路径处理等多种操作。具体用法和可用选项请参考 官方文档

自定义

TemplateDescription
{{ url .UpstreamIcon .UpstreamURL }}Create an OSC8 hyperlink to a website to open your default browser (needs terminal support).
{{ path .Path .Location }}Create an OSC8 file link to a folder to open your file explorer (needs terminal support).
{{ secondsRound 3600 }}Round seconds to a time indication. In this case the output is 1h.
{{ if glob "*.go" }}OK{{ else }}NOK{{ end }}Exposes filepath.Glob as a boolean template function.
{{ if matchP ".*\\.Repo$" .Path }}Repo{{ else }}No Repo{{ end }}Exposes regexp.MatchString as a boolean template function.
{{ replaceP "c.t" "cut code cat" "dog" }}Exposes regexp.ReplaceAllString as a string template function.
{{ .Code | hresult }}Transform a status code to its HRESULT value for easy troubleshooting. For example -1978335212 becomes 0x8A150014.
{{ readFile ".version.json" }}Read a file in the current directory. Returns a string.
{{ random (list \"a\" 2 .MyThirdItem) }}Selects a random element from a list. The list can be an array or slice containing any types (use sprig's list).

跨段落模板属性

要在模板中使用其他段落的属性,可通过 {{ .Segments.Segment }} 语法实现, 其中 .Segment 是目标段落名称(首字母大写)。

例如,若要在 status 段落中使用 git 段落的 .UpstreamGone 属性, 可这样配置:

{
"template": " {{ if .Segments.Git.UpstreamGone }}{{ else if gt .Code 0 }}{{ else }}{{ end }} "
}
注意

此功能要求被引用的段落必须存在于配置中。若配置未包含 git 段落, 上例将失效,因为 Oh My Posh 仅在需要时填充属性。

技巧

若存在功能相同但用途不同的两个段落,可通过段落的 alias 属性 加以区分。

{
"segments": [
{
"type": "git",
"alias": "GitMain",
"style": "plain",
"foreground": "#ffffff",
"options": {
"branch_icon": ""
}
},
{
"type": "git",
"alias": "GitSecondary",
"style": "plain",
"foreground": "#ffffff",
"options": {
"branch_icon": ""
}
},
{
"type": "text",
"style": "plain",
"foreground": "#ffffff",
"template": "{{ .Segments.GitMain.HEAD }} - {{ .Segments.GitSecondary.HEAD }}"
}
]
}

要检查特定段落是否激活,可使用 .Segments.Contains 函数,例如:

{
"template": "{{ if .Segments.Contains \"Git\" }}{{ else if gt .Code 0 }}{{ else }}{{ end }} "
}

文本装饰

可通过以下语法装饰文本:

SyntaxDescription
<b>bold</b>bold as bold text
<u>underline</u>underline as underlined text
<o>overline</o>overline as overlined text
<i>italic</i>italic as italic text
<s>strikethrough</s>strikethrough as strikethrough text
<d>dimmed</d>dimmed as dimmed text
<f>blink</f>blink as blinking (flashing) text
<r>reversed</r>reversed as reversed text

此语法适用于模板及配置中的图标/文本。