diff --git a/src/engine/block.go b/src/engine/block.go index 2195ed8d..2d73a180 100644 --- a/src/engine/block.go +++ b/src/engine/block.go @@ -131,7 +131,7 @@ func (b *Block) setSegmentsText() { func (b *Block) RenderSegments() (string, int) { for _, segment := range b.Segments { - if !segment.Enabled && segment.Style != Accordion { + if !segment.Enabled && segment.style() != Accordion { continue } b.setActiveSegment(segment) @@ -217,7 +217,7 @@ func (b *Block) Debug() (int, []*SegmentTiming) { segment.SetEnabled(b.env) segment.SetText() segmentTiming.active = segment.Enabled - if segmentTiming.active || segment.Style == Accordion { + if segmentTiming.active || segment.style() == Accordion { b.setActiveSegment(segment) b.renderActiveSegment() segmentTiming.text, _ = b.writer.String() diff --git a/src/engine/segment.go b/src/engine/segment.go index bb5f6ef5..521c6f54 100644 --- a/src/engine/segment.go +++ b/src/engine/segment.go @@ -22,7 +22,6 @@ type Segment struct { Type SegmentType `json:"type,omitempty"` Tips []string `json:"tips,omitempty"` Style SegmentStyle `json:"style,omitempty"` - StyleCondition string `json:"style_condition,omitempty"` PowerlineSymbol string `json:"powerline_symbol,omitempty"` InvertPowerline bool `json:"invert_powerline,omitempty"` Foreground string `json:"foreground,omitempty"` @@ -68,6 +67,20 @@ type SegmentWriter interface { // SegmentStyle the style of segment, for more information, see the constants type SegmentStyle string +func (s *SegmentStyle) Resolve(env platform.Environment, context interface{}) SegmentStyle { + txtTemplate := &template.Text{ + Context: context, + Env: env, + } + txtTemplate.Template = string(*s) + value, err := txtTemplate.Render() + // default to Plain + if err != nil || len(value) == 0 { + return Plain + } + return SegmentStyle(value) +} + // SegmentType the type of segment, for more information, see the constants type SegmentType string @@ -283,6 +296,14 @@ var Segments = map[SegmentType]SegmentWriter{ YTM: &segments.Ytm{}, } +func (segment *Segment) style() SegmentStyle { + if len(segment.styleCache) != 0 { + return segment.styleCache + } + segment.styleCache = segment.Style.Resolve(segment.env, segment.writer) + return segment.styleCache +} + func (segment *Segment) shouldIncludeFolder() bool { if segment.env == nil { return true @@ -346,30 +367,6 @@ func (segment *Segment) background() string { return segment.backgroundCache } -func (segment *Segment) style() SegmentStyle { - if len(segment.styleCache) == 0 { - segment.styleCache = segment.getStyle(segment.StyleCondition, segment.Style) - } - - return segment.styleCache -} - -func (segment *Segment) getStyle(condition string, defaultStyle SegmentStyle) SegmentStyle { - if condition == "" { - return defaultStyle - } - txtTemplate := &template.Text{ - Context: segment.writer, - Env: segment.env, - } - txtTemplate.Template = condition - value, err := txtTemplate.Render() - if err != nil || value == "" { - return defaultStyle - } - return SegmentStyle(value) -} - func (segment *Segment) mapSegmentWithWriter(env platform.Environment) error { segment.env = env diff --git a/themes/schema.json b/themes/schema.json index c0b11950..7ae0f050 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -283,15 +283,16 @@ ] }, "style": { - "type": "string", "title": "Segment Style", "description": "https://ohmyposh.dev/docs/configuration/segment#style", - "enum": ["plain", "powerline", "diamond", "accordion"] - }, - "style_condition": { - "type": "string", - "title": "Segment Style Condition", - "description": "Conditionally change the segments style using a template. Defaults to using the style property value. Must evaluate to a valid template style." + "anyOf": [ + { + "enum": ["plain", "powerline", "diamond", "accordion"] + }, + { + "type": "string" + } + ] }, "foreground": { "$ref": "#/definitions/color" diff --git a/website/docs/configuration/segment.mdx b/website/docs/configuration/segment.mdx index a234895f..a682310c 100644 --- a/website/docs/configuration/segment.mdx +++ b/website/docs/configuration/segment.mdx @@ -36,8 +36,7 @@ understand how to configure a segment. | Name | Type | Description | | ---------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `type` | `string` | takes the `string` value referencing which segment logic it needs to run (see [segments][segments] for possible values) | -| `style` | `string` | see [Style][style] below. Possible values: