mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat(style): segment style as a template string
This commit is contained in:
parent
9e40a1a0dc
commit
9d81f5a362
|
@ -131,7 +131,7 @@ func (b *Block) setSegmentsText() {
|
||||||
|
|
||||||
func (b *Block) RenderSegments() (string, int) {
|
func (b *Block) RenderSegments() (string, int) {
|
||||||
for _, segment := range b.Segments {
|
for _, segment := range b.Segments {
|
||||||
if !segment.Enabled && segment.Style != Accordion {
|
if !segment.Enabled && segment.style() != Accordion {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
b.setActiveSegment(segment)
|
b.setActiveSegment(segment)
|
||||||
|
@ -217,7 +217,7 @@ func (b *Block) Debug() (int, []*SegmentTiming) {
|
||||||
segment.SetEnabled(b.env)
|
segment.SetEnabled(b.env)
|
||||||
segment.SetText()
|
segment.SetText()
|
||||||
segmentTiming.active = segment.Enabled
|
segmentTiming.active = segment.Enabled
|
||||||
if segmentTiming.active || segment.Style == Accordion {
|
if segmentTiming.active || segment.style() == Accordion {
|
||||||
b.setActiveSegment(segment)
|
b.setActiveSegment(segment)
|
||||||
b.renderActiveSegment()
|
b.renderActiveSegment()
|
||||||
segmentTiming.text, _ = b.writer.String()
|
segmentTiming.text, _ = b.writer.String()
|
||||||
|
|
|
@ -22,7 +22,6 @@ type Segment struct {
|
||||||
Type SegmentType `json:"type,omitempty"`
|
Type SegmentType `json:"type,omitempty"`
|
||||||
Tips []string `json:"tips,omitempty"`
|
Tips []string `json:"tips,omitempty"`
|
||||||
Style SegmentStyle `json:"style,omitempty"`
|
Style SegmentStyle `json:"style,omitempty"`
|
||||||
StyleCondition string `json:"style_condition,omitempty"`
|
|
||||||
PowerlineSymbol string `json:"powerline_symbol,omitempty"`
|
PowerlineSymbol string `json:"powerline_symbol,omitempty"`
|
||||||
InvertPowerline bool `json:"invert_powerline,omitempty"`
|
InvertPowerline bool `json:"invert_powerline,omitempty"`
|
||||||
Foreground string `json:"foreground,omitempty"`
|
Foreground string `json:"foreground,omitempty"`
|
||||||
|
@ -68,6 +67,20 @@ type SegmentWriter interface {
|
||||||
// SegmentStyle the style of segment, for more information, see the constants
|
// SegmentStyle the style of segment, for more information, see the constants
|
||||||
type SegmentStyle string
|
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
|
// SegmentType the type of segment, for more information, see the constants
|
||||||
type SegmentType string
|
type SegmentType string
|
||||||
|
|
||||||
|
@ -283,6 +296,14 @@ var Segments = map[SegmentType]SegmentWriter{
|
||||||
YTM: &segments.Ytm{},
|
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 {
|
func (segment *Segment) shouldIncludeFolder() bool {
|
||||||
if segment.env == nil {
|
if segment.env == nil {
|
||||||
return true
|
return true
|
||||||
|
@ -346,30 +367,6 @@ func (segment *Segment) background() string {
|
||||||
return segment.backgroundCache
|
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 {
|
func (segment *Segment) mapSegmentWithWriter(env platform.Environment) error {
|
||||||
segment.env = env
|
segment.env = env
|
||||||
|
|
||||||
|
|
|
@ -283,15 +283,16 @@
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"style": {
|
"style": {
|
||||||
"type": "string",
|
|
||||||
"title": "Segment Style",
|
"title": "Segment Style",
|
||||||
"description": "https://ohmyposh.dev/docs/configuration/segment#style",
|
"description": "https://ohmyposh.dev/docs/configuration/segment#style",
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
"enum": ["plain", "powerline", "diamond", "accordion"]
|
"enum": ["plain", "powerline", "diamond", "accordion"]
|
||||||
},
|
},
|
||||||
"style_condition": {
|
{
|
||||||
"type": "string",
|
"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."
|
]
|
||||||
},
|
},
|
||||||
"foreground": {
|
"foreground": {
|
||||||
"$ref": "#/definitions/color"
|
"$ref": "#/definitions/color"
|
||||||
|
|
|
@ -36,8 +36,7 @@ understand how to configure a segment.
|
||||||
| Name | Type | Description |
|
| Name | Type | Description |
|
||||||
| ---------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| ---------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| `type` | `string` | takes the `string` value referencing which segment logic it needs to run (see [segments][segments] for possible values) |
|
| `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` | `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> |
|
||||||
| `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. |
|
|
||||||
| `powerline_symbol` | `string` | character to use when `"style": "powerline"` |
|
| `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` |
|
| `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 |
|
| `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
|
||||||
|
|
||||||
Style defines how a prompt is rendered. Looking at the most prompt
|
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.
|
you want to achieve you might need to understand/use them all.
|
||||||
|
|
||||||
### Powerline
|
### Powerline
|
||||||
|
|
Loading…
Reference in a new issue