feat(style): segment style as a template string

This commit is contained in:
NoF0rte 2022-12-28 14:15:30 -07:00 committed by Jan De Dobbeleer
parent 9e40a1a0dc
commit 9d81f5a362
4 changed files with 34 additions and 37 deletions

View file

@ -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()

View file

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

View file

@ -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"

View file

@ -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:<ul><li>`powerline`</li><li>`plain`</li><li>`diamond`</li><li>`accordion`</li></ul> |
| `style_condition` | `string` | conditionally change the segments style using a template. Defaults to using the style property value. Must evaluate to a valid template style. |
| `style` | `string` | see [Style][style] below. Possible values:<ul><li>`powerline`</li><li>`plain`</li><li>`diamond`</li><li>`accordion`</li><li>a go [text/template][go-text-template] [template][templates] resolving to the above values</li></ul> |
| `powerline_symbol` | `string` | character to use when `"style": "powerline"` |
| `invert_powerline` | `boolean` | if `true` swaps the foreground and background colors. Can be useful when the character you want does not exist in the perfectly mirrored variant for example - defaults to `false` |
| `leading_diamond` | `string` | character to use at the start of the segment. Will take the background color of the segment as its foreground color |
@ -58,7 +57,7 @@ understand how to configure a segment.
## Style
Style defines how a prompt is rendered. Looking at the most prompt
themes out there, we identified 3 types. All of these require a different configuration and depending on the look
themes out there, we identified 4 types. All of these require a different configuration and depending on the look
you want to achieve you might need to understand/use them all.
### Powerline