mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: deprecate pre- and postfix
This commit is contained in:
parent
b4292e2e50
commit
83980a3dff
|
@ -3,7 +3,6 @@ package engine
|
||||||
import (
|
import (
|
||||||
"oh-my-posh/color"
|
"oh-my-posh/color"
|
||||||
"oh-my-posh/environment"
|
"oh-my-posh/environment"
|
||||||
"oh-my-posh/properties"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -111,23 +110,16 @@ func (b *Block) renderSegment(segment *Segment) {
|
||||||
b.writePowerline(false)
|
b.writePowerline(false)
|
||||||
switch b.activeSegment.Style {
|
switch b.activeSegment.Style {
|
||||||
case Plain, Powerline:
|
case Plain, Powerline:
|
||||||
b.renderText(segment.stringValue)
|
b.writer.Write(b.activeBackground, b.activeForeground, segment.stringValue)
|
||||||
case Diamond:
|
case Diamond:
|
||||||
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.LeadingDiamond)
|
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.LeadingDiamond)
|
||||||
b.renderText(segment.stringValue)
|
b.writer.Write(b.activeBackground, b.activeForeground, segment.stringValue)
|
||||||
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.TrailingDiamond)
|
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.TrailingDiamond)
|
||||||
}
|
}
|
||||||
b.previousActiveSegment = b.activeSegment
|
b.previousActiveSegment = b.activeSegment
|
||||||
b.writer.SetParentColors(b.activeBackground, b.activeForeground)
|
b.writer.SetParentColors(b.activeBackground, b.activeForeground)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) renderText(text string) {
|
|
||||||
defaultValue := " "
|
|
||||||
b.writer.Write(b.activeBackground, b.activeForeground, b.activeSegment.getValue(properties.Prefix, defaultValue))
|
|
||||||
b.writer.Write(b.activeBackground, b.activeForeground, text)
|
|
||||||
b.writer.Write(b.activeBackground, b.activeForeground, b.activeSegment.getValue(properties.Postfix, defaultValue))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) writePowerline(final bool) {
|
func (b *Block) writePowerline(final bool) {
|
||||||
resolvePowerlineSymbol := func() string {
|
resolvePowerlineSymbol := func() string {
|
||||||
var symbol string
|
var symbol string
|
||||||
|
|
|
@ -10,12 +10,18 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
colorBackground = properties.Property("color_background")
|
colorBackground = properties.Property("color_background")
|
||||||
|
|
||||||
|
prefix = properties.Property("prefix")
|
||||||
|
postfix = properties.Property("postfix")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cfg *Config) Migrate(env environment.Environment) {
|
func (cfg *Config) Migrate(env environment.Environment) {
|
||||||
for _, block := range cfg.Blocks {
|
for _, block := range cfg.Blocks {
|
||||||
block.migrate(env)
|
block.migrate(env)
|
||||||
}
|
}
|
||||||
|
for _, segment := range cfg.Tooltips {
|
||||||
|
segment.migrate(env)
|
||||||
|
}
|
||||||
cfg.updated = true
|
cfg.updated = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +104,8 @@ func (segment *Segment) migrate(env environment.Environment) {
|
||||||
segment.migrateColorOverride("version_mismatch_color", "{{ if .Mismatch }}%s{{ end }}")
|
segment.migrateColorOverride("version_mismatch_color", "{{ if .Mismatch }}%s{{ end }}")
|
||||||
}
|
}
|
||||||
case EXIT:
|
case EXIT:
|
||||||
template := segment.writer.Template()
|
segment.migrateTemplate()
|
||||||
|
template := segment.Properties.GetString(properties.SegmentTemplate, segment.writer.Template())
|
||||||
displayExitCode := properties.Property("display_exit_code")
|
displayExitCode := properties.Property("display_exit_code")
|
||||||
if !segment.Properties.GetBool(displayExitCode, true) {
|
if !segment.Properties.GetBool(displayExitCode, true) {
|
||||||
delete(segment.Properties, displayExitCode)
|
delete(segment.Properties, displayExitCode)
|
||||||
|
@ -144,7 +151,15 @@ func (segment *Segment) migratePropertyKey(oldProperty, newProperty properties.P
|
||||||
}
|
}
|
||||||
|
|
||||||
func (segment *Segment) migrateTemplate() {
|
func (segment *Segment) migrateTemplate() {
|
||||||
|
defer segment.migratePreAndPostFix()
|
||||||
if segment.hasProperty(properties.SegmentTemplate) {
|
if segment.hasProperty(properties.SegmentTemplate) {
|
||||||
|
// existing template, ensure to add default pre/postfix values
|
||||||
|
if !segment.hasProperty(prefix) {
|
||||||
|
segment.Properties[prefix] = " "
|
||||||
|
}
|
||||||
|
if !segment.hasProperty(postfix) {
|
||||||
|
segment.Properties[postfix] = " "
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
segment.Properties[properties.SegmentTemplate] = segment.writer.Template()
|
segment.Properties[properties.SegmentTemplate] = segment.writer.Template()
|
||||||
|
@ -191,3 +206,21 @@ func (segment *Segment) migrateInlineColorOverride(property properties.Property,
|
||||||
template = strings.ReplaceAll(template, old, colorTemplate)
|
template = strings.ReplaceAll(template, old, colorTemplate)
|
||||||
segment.Properties[properties.SegmentTemplate] = template
|
segment.Properties[properties.SegmentTemplate] = template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (segment *Segment) migratePreAndPostFix() {
|
||||||
|
template := segment.Properties.GetString(properties.SegmentTemplate, segment.writer.Template())
|
||||||
|
defaultValue := " "
|
||||||
|
if segment.hasProperty(prefix) {
|
||||||
|
prefix := segment.Properties.GetString(prefix, defaultValue)
|
||||||
|
template = strings.TrimPrefix(template, defaultValue)
|
||||||
|
template = prefix + template
|
||||||
|
delete(segment.Properties, "prefix")
|
||||||
|
}
|
||||||
|
if segment.hasProperty(postfix) {
|
||||||
|
postfix := segment.Properties.GetString(postfix, defaultValue)
|
||||||
|
template = strings.TrimSuffix(template, defaultValue)
|
||||||
|
template += postfix
|
||||||
|
delete(segment.Properties, "postfix")
|
||||||
|
}
|
||||||
|
segment.Properties[properties.SegmentTemplate] = template
|
||||||
|
}
|
||||||
|
|
|
@ -337,3 +337,49 @@ func TestInlineColorOverride(t *testing.T) {
|
||||||
assert.Equal(t, tc.Expected, segment.Properties[properties.SegmentTemplate], tc.Case)
|
assert.Equal(t, tc.Expected, segment.Properties[properties.SegmentTemplate], tc.Case)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMigratePreAndPostfix(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
Expected string
|
||||||
|
Props properties.Map
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Case: "Pre and Postfix",
|
||||||
|
Expected: "<background,transparent>\ue0b6</> \uf489 {{ .Name }} <transparent,background>\ue0b2</>",
|
||||||
|
Props: properties.Map{
|
||||||
|
"postfix": " <transparent,background>\ue0b2</>",
|
||||||
|
"prefix": "<background,transparent>\ue0b6</> \uf489 ",
|
||||||
|
"template": "{{ .Name }}",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "Prefix",
|
||||||
|
Expected: " {{ .Name }} ",
|
||||||
|
Props: properties.Map{
|
||||||
|
"prefix": " ",
|
||||||
|
"template": "{{ .Name }}",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "Postfix",
|
||||||
|
Expected: " {{ .Name }} ",
|
||||||
|
Props: properties.Map{
|
||||||
|
"postfix": " ",
|
||||||
|
"template": "{{ .Name }} ",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
segment := &Segment{
|
||||||
|
Properties: tc.Props,
|
||||||
|
writer: &MockedWriter{
|
||||||
|
template: tc.Props.GetString(properties.SegmentTemplate, ""),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
segment.migrateTemplate()
|
||||||
|
assert.Equal(t, tc.Expected, segment.Properties[properties.SegmentTemplate], tc.Case)
|
||||||
|
assert.NotContains(t, segment.Properties, "prefix", tc.Case)
|
||||||
|
assert.NotContains(t, segment.Properties, "postfix", tc.Case)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -169,13 +169,6 @@ func (segment *Segment) enabled() bool {
|
||||||
return segment.active
|
return segment.active
|
||||||
}
|
}
|
||||||
|
|
||||||
func (segment *Segment) getValue(property properties.Property, defaultValue string) string {
|
|
||||||
if value, ok := segment.Properties[property]; ok {
|
|
||||||
return properties.ParseString(value, defaultValue)
|
|
||||||
}
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
func (segment *Segment) shouldIncludeFolder() bool {
|
func (segment *Segment) shouldIncludeFolder() bool {
|
||||||
cwdIncluded := segment.cwdIncluded()
|
cwdIncluded := segment.cwdIncluded()
|
||||||
cwdExcluded := segment.cwdExcluded()
|
cwdExcluded := segment.cwdExcluded()
|
||||||
|
|
|
@ -23,10 +23,6 @@ type Property string
|
||||||
const (
|
const (
|
||||||
// Style indicates with style to use
|
// Style indicates with style to use
|
||||||
Style Property = "style"
|
Style Property = "style"
|
||||||
// Prefix adds a text prefix to the segment
|
|
||||||
Prefix Property = "prefix"
|
|
||||||
// Postfix adds a text postfix to the segment
|
|
||||||
Postfix Property = "postfix"
|
|
||||||
// IncludeFolders folders to be included for the segment logic
|
// IncludeFolders folders to be included for the segment logic
|
||||||
IncludeFolders Property = "include_folders"
|
IncludeFolders Property = "include_folders"
|
||||||
// ExcludeFolders folders to be excluded for the segment logic
|
// ExcludeFolders folders to be excluded for the segment logic
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"oh-my-posh/mock"
|
"oh-my-posh/mock"
|
||||||
"oh-my-posh/properties"
|
"oh-my-posh/properties"
|
||||||
"oh-my-posh/template"
|
"oh-my-posh/template"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -32,7 +33,7 @@ func renderTemplate(env *mock.MockedEnvironment, segmentTemplate string, context
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return err.Error()
|
||||||
}
|
}
|
||||||
return text
|
return strings.TrimSpace(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
Loading…
Reference in a new issue