feat(templates): segment alias

resolves #2811
This commit is contained in:
Jan De Dobbeleer 2022-09-20 20:09:38 +02:00 committed by Jan De Dobbeleer
parent 0e25f1613c
commit aecd8d6bfd
4 changed files with 49 additions and 1 deletions

View file

@ -32,6 +32,7 @@ type Segment struct {
TemplatesLogic template.Logic `json:"templates_logic,omitempty"`
Properties properties.Map `json:"properties,omitempty"`
Interactive bool `json:"interactive,omitempty"`
Alias string `json:"alias,omitempty"`
writer SegmentWriter
Enabled bool `json:"-"`
@ -384,7 +385,11 @@ func (segment *Segment) SetEnabled(env environment.Environment) {
}
if segment.writer.Enabled() {
segment.Enabled = true
env.TemplateCache().AddSegmentData(string(segment.Type), segment.writer)
name := segment.Alias
if len(name) == 0 {
name = string(segment.Type)
}
env.TemplateCache().AddSegmentData(name, segment.writer)
}
}

View file

@ -358,6 +358,12 @@
"title": "Allow the use of interactive prompt escape sequences",
"description": "https://ohmyposh.dev/docs/configuration/segment#interactive",
"default": false
},
"alias": {
"type": "string",
"title": "Give the segment an alias for use in templates",
"description": "https://ohmyposh.dev/docs/configuration/segment#alias",
"default": ""
}
},
"allOf": [

View file

@ -50,6 +50,7 @@ understand how to configure a segment.
| `templates_logic` | `string` | <ul><li>`first_match`: return the first non-whitespace string and skip everything else</li><li>`join`:evaluate all templates and join all non-whitespace strings (**default**)</li></ul> |
| `properties` | `[]Property` | see [Properties][properties] below |
| `interactive` | `boolean` | when is true, the segment text is not escaped to allow the use of interactive prompt escape sequences - defaults to `false` |
| `alias` | `string` | for use with [cross segment template properties][cstp] |
## Style
@ -154,3 +155,4 @@ This means that for user Bill, who has a user account `Bill` on Windows and `bil
[regex]: https://www.regular-expressions.info/tutorial.html
[templates]: /docs/configuration/templates
[color-templates]: /docs/configuration/colors#color-templates
[cstp]: templates.mdx#cross-segment-template-properties

View file

@ -109,6 +109,41 @@ For this to work, the segment you refer to needs to be in your config. The above
your config does not contain a git segment as Oh My Posh only populates the properties when it needs to.
:::
:::tip
If you have two identical segments for a different purpose, you can make use of the `alias` property on the segment
to distinct between both. For example:
```json
{
"type": "command",
// highlight-next-line
"alias": "Hello",
"style": "plain",
"foreground": "#ffffff",
"properties": {
"command": "echo Hello"
}
},
{
"type": "command",
// highlight-next-line
"alias": "World",
"style": "plain",
"foreground": "#ffffff",
"properties": {
"command": "echo World"
}
},
{
"type": "text",
"style": "plain",
"foreground": "#ffffff",
// highlight-next-line
"template": "{{ .Segments.Hello.Output }} {{ .Segments.World.Output }}"
}
```
:::
## Text decoration
You can make use of the following syntax to decorate text: