Direct naar hoofdinhoud

Sjablonen

Onofficiรซle Beta-vertaling

Deze pagina is vertaald door PageTurner AI (beta). Niet officieel goedgekeurd door het project. Een fout gevonden? Probleem melden โ†’

Elk segment heeft een template-eigenschap om de weergegeven tekst aan te passen. Onder de motorkap gebruikt dit Go's text/template-functie uitgebreid met sprig, en biedt het enkele standaardeigenschappen om mee te werken.

Globale eigenschappenโ€‹

Deze eigenschappen kunnen overal gebruikt worden, in elk segment. Als een segment een eigenschap met dezelfde naam bevat, wordt de segmentwaarde gebruikt. Als je de globale eigenschap wilt gebruiken, kun je deze direct benaderen door .$ als voorvoegsel te gebruiken.

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.
.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

Segmentโ€‹

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

Omgevingsvariabelenโ€‹

NameTypeDescription
.Env.VarNamestringAny environment variable where VarName is the environment variable name
tip

Voor de onderstaande shells kun je een functie overschrijven om logica uit te voeren voordat de prompt wordt weergegeven. Dit kan bijvoorbeeld worden gebruikt om een omgevingsvariabele te vullen met een specifieke context.

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

Configuratievariabelenโ€‹

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

Voorbeeldโ€‹

{
"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 }} "
}
]
}
]
}

Sjabloonlogicaโ€‹

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).

Helperfunctiesโ€‹

Sprigโ€‹

Oh My Posh bevat alle sprig-functies, wat betekent dat je bewerkingen op strings, paden en vele andere manipulaties direct vanuit je sjabloon kunt uitvoeren. Bekijk hun documentatie voor beschikbare opties en gebruiksinstructies.

Aangepastโ€‹

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).

Sjablooneigenschappen over segmenten heenโ€‹

Om eigenschappen van een ander segment in een sjabloon te gebruiken, kun je {{ .Segments.Segment }} gebruiken waarbij .Segment de naam is van het gewenste segment met een hoofdletter aan het begin.

Als je bijvoorbeeld de .UpstreamGone-eigenschap van het git-segment wilt gebruiken in het status-segment, kun je dat als volgt doen:

{
"template": " {{ if .Segments.Git.UpstreamGone }}๎ฌ…{{ else if gt .Code 0 }}๏€{{ else }}๏€Œ{{ end }} "
}
let op

Let op: het segment waarnaar je verwijst moet wel in je configuratie staan. Het bovenstaande voorbeeld werkt niet als je configuratie geen git-segment bevat, omdat Oh My Posh alleen eigenschappen invult wanneer dat nodig is.

tip

Als je twee identieke segmenten voor verschillende doeleinden hebt, kun je de alias-eigenschap gebruiken om onderscheid te maken.

{
"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 }}"
}
]
}

Om te controleren of een specifiek segment actief is, kun je de .Segments.Contains-functie gebruiken, bijvoorbeeld:

{
"template": "{{ if .Segments.Contains \"Git\" }}๎ฌ…{{ else if gt .Code 0 }}๏€{{ else }}๏€Œ{{ end }} "
}

Tekstdecoratieโ€‹

Je kunt de volgende syntax gebruiken om tekst te decoreren:

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

Deze kan worden gebruikt in sjablonen en iconen/tekst binnen je configuratie.