refactor: add config version 3

This commit is contained in:
Jan De Dobbeleer 2024-10-28 13:59:40 +01:00 committed by Jan De Dobbeleer
parent d479ef88fd
commit cea18715bb
129 changed files with 172 additions and 887 deletions

View file

@ -17,7 +17,7 @@ const (
AUTOUPGRADE = "autoupgrade"
UPGRADENOTICE = "upgradenotice"
Version = 2
Version = 3
)
// Config holds all the theme for rendering the prompt

View file

@ -10,12 +10,14 @@ import (
func Default(env runtime.Environment, warning bool) *Config {
exitBackgroundTemplate := "{{ if gt .Code 0 }}p:red{{ end }}"
exitTemplate := " {{ if gt .Code 0 }}\uf00d{{ else }}\uf00c{{ end }} "
if warning {
exitBackgroundTemplate = "p:red"
exitTemplate = " CONFIG ERROR "
}
cfg := &Config{
Version: 2,
Version: 3,
FinalSpace: true,
Blocks: []*Block{
{
@ -194,6 +196,7 @@ func Default(env runtime.Environment, warning bool) *Config {
},
},
}
cfg.env = env
return cfg
}

View file

@ -1,277 +1,33 @@
package config
import (
"fmt"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/segments"
)
const (
colorBackground = properties.Property("color_background")
prefix = properties.Property("prefix")
postfix = properties.Property("postfix")
segmentTemplate = properties.Property("template")
"github.com/jandedobbeleer/oh-my-posh/src/cache"
)
func (cfg *Config) Migrate() {
for _, block := range cfg.Blocks {
for _, segment := range block.Segments {
segment.migrate(cfg.env, cfg.Version)
segment.migrate(cfg.Version)
}
}
for _, segment := range cfg.Tooltips {
segment.migrate(cfg.env, cfg.Version)
}
if strings.Contains(cfg.ConsoleTitleTemplate, ".Path") {
cfg.ConsoleTitleTemplate = strings.ReplaceAll(cfg.ConsoleTitleTemplate, ".Path", ".PWD")
}
cfg.updated = true
cfg.Version = Version
}
func (segment *Segment) migrate(env runtime.Environment, version int) {
if version < 1 {
segment.migrationOne(env)
func (segment *Segment) migrate(version int) {
// configs older than 2 are no longer supported
if version != 2 {
return
}
if version < 2 {
segment.migrationTwo(env)
}
}
func (segment *Segment) migrationOne(env runtime.Environment) {
if err := segment.MapSegmentWithWriter(env); err != nil {
return
// Cache settings
delete(segment.Properties, "cache_version")
cacheTimeout := segment.Properties.GetInt("cache_timeout", 0)
if cacheTimeout != 0 {
segment.Cache = &cache.Config{
Duration: cache.ToDuration(cacheTimeout * 60),
Strategy: cache.Folder,
}
}
// General properties that need replacement
segment.migratePropertyKey("display_version", properties.FetchVersion)
delete(segment.Properties, "enable_hyperlink")
switch segment.Type { //nolint:exhaustive
case TEXT:
segment.migratePropertyKey("text", segmentTemplate)
segment.migrateTemplate()
case GIT:
hasTemplate := segment.hasProperty(segmentTemplate)
segment.migratePropertyKey("display_status", segments.FetchStatus)
segment.migratePropertyKey("display_stash_count", segments.FetchStashCount)
segment.migratePropertyKey("display_worktree_count", segments.FetchWorktreeCount)
segment.migratePropertyKey("display_upstream_icon", segments.FetchUpstreamIcon)
segment.migrateTemplate()
segment.migrateIconOverride("local_working_icon", " \uF044 ")
segment.migrateIconOverride("local_staged_icon", " \uF046 ")
segment.migrateIconOverride("stash_count_icon", " \uf0c7 ")
segment.migrateIconOverride("worktree_count_icon", " \uf1bb ")
segment.migrateIconOverride("status_separator_icon", " |")
if segment.Properties.GetBool("status_colors_enabled", false) {
background := segment.Properties.GetBool(colorBackground, true)
segment.migrateColorOverride("local_changes_color", "{{ if or (.Working.Changed) (.Staging.Changed) }}%s{{ end }}", background)
segment.migrateColorOverride("ahead_and_behind_color", "{{ if and (gt .Ahead 0) (gt .Behind 0) }}%s{{ end }}", background)
segment.migrateColorOverride("behind_color", "{{ if gt .Ahead 0 }}%s{{ end }}", background)
segment.migrateColorOverride("ahead_color", "{{ if gt .Behind 0 }}%s{{ end }}", background)
}
if !hasTemplate {
segment.migrateInlineColorOverride("working_color", "{{ .Working.String }}")
segment.migrateInlineColorOverride("staging_color", "{{ .Staging.String }}")
}
// legacy properties
delete(segment.Properties, "display_branch_status")
delete(segment.Properties, "display_status_detail")
delete(segment.Properties, "status_colors_enabled")
case BATTERY:
segment.migrateTemplate()
background := segment.Properties.GetBool(colorBackground, false)
segment.migrateColorOverride("charged_color", `{{ if eq "Full" .State.String }}%s{{ end }}`, background)
segment.migrateColorOverride("charging_color", `{{ if eq "Charging" .State.String }}%s{{ end }}`, background)
segment.migrateColorOverride("discharging_color", `{{ if eq "Discharging" .State.String }}%s{{ end }}`, background)
stateList := []string{`"Discharging"`}
if segment.Properties.GetBool("display_charging", true) {
stateList = append(stateList, `"Charging"`)
}
if segment.Properties.GetBool("display_charged", true) {
stateList = append(stateList, `"Full"`)
}
if len(stateList) < 3 {
enabledTemplate := "{{ $stateList := list %s }}{{ if has .State.String $stateList }}{{ .Icon }}{{ .Percentage }}{{ end }}"
template := segment.Properties.GetString(segmentTemplate, segment.writer.Template())
template = strings.ReplaceAll(template, "{{ .Icon }}{{ .Percentage }}", fmt.Sprintf(enabledTemplate, strings.Join(stateList, " ")))
segment.Properties[segmentTemplate] = template
}
// legacy properties
delete(segment.Properties, "display_charging")
delete(segment.Properties, "display_charged")
delete(segment.Properties, "battery_icon")
case PYTHON:
segment.migrateTemplate()
segment.migratePropertyKey("display_virtual_env", segments.FetchVirtualEnv)
case SESSION:
hasTemplate := segment.hasProperty(segmentTemplate)
segment.migrateTemplate()
segment.migrateIconOverride("ssh_icon", "\ueba9 ")
template := segment.Properties.GetString(segmentTemplate, segment.writer.Template())
template = strings.ReplaceAll(template, ".ComputerName", ".HostName")
if !segment.Properties.GetBool("display_host", true) {
template = strings.ReplaceAll(template, "@{{ .HostName }}", "")
}
if !segment.Properties.GetBool("display_user", true) {
template = strings.ReplaceAll(template, "@", "")
template = strings.ReplaceAll(template, "{{ .UserName }}", "")
}
segment.Properties[segmentTemplate] = template
segment.migrateIconOverride("user_info_separator", "@")
if !hasTemplate {
segment.migrateInlineColorOverride("user_color", "{{ .UserName }}")
segment.migrateInlineColorOverride("host_color", "{{ .HostName }}")
}
case NODE:
segment.migrateTemplate()
segment.migratePropertyKey("display_package_manager", segments.FetchPackageManager)
enableVersionMismatch := "enable_version_mismatch"
if segment.Properties.GetBool(properties.Property(enableVersionMismatch), false) {
delete(segment.Properties, properties.Property(enableVersionMismatch))
background := segment.Properties.GetBool(colorBackground, false)
segment.migrateColorOverride("version_mismatch_color", "{{ if .Mismatch }}%s{{ end }}", background)
}
case EXIT:
template := segment.Properties.GetString(segmentTemplate, "{{ if gt .Code 0 }}\uf00d {{ .Meaning }}{{ else }}\uf42e{{ end }}")
if strings.Contains(template, ".Text") {
template = strings.ReplaceAll(template, ".Text", ".Meaning")
segment.Properties[segmentTemplate] = template
}
displayExitCode := properties.Property("display_exit_code")
if !segment.Properties.GetBool(displayExitCode, true) {
delete(segment.Properties, displayExitCode)
template = strings.ReplaceAll(template, " {{ .Meaning }}", "")
}
alwaysNumeric := properties.Property("always_numeric")
if segment.Properties.GetBool(alwaysNumeric, false) {
delete(segment.Properties, alwaysNumeric)
template = strings.ReplaceAll(template, ".Meaning", ".Code")
}
segment.Properties[segmentTemplate] = template
segment.migrateTemplate()
segment.migrateIconOverride("success_icon", "\uf42e")
segment.migrateIconOverride("error_icon", "\uf00d")
background := segment.Properties.GetBool(colorBackground, false)
segment.migrateColorOverride("error_color", "{{ if gt .Code 0 }}%s{{ end }}", background)
default:
segment.migrateTemplate()
}
delete(segment.Properties, colorBackground)
}
func (segment *Segment) migrationTwo(env runtime.Environment) {
if err := segment.MapSegmentWithWriter(env); err != nil {
return
}
if !segment.hasProperty(segmentTemplate) {
return
}
template := segment.Properties.GetString(segmentTemplate, segment.writer.Template())
segment.Template = template
delete(segment.Properties, segmentTemplate)
}
func (segment *Segment) hasProperty(property properties.Property) bool {
for key := range segment.Properties {
if key == property {
return true
}
}
return false
}
func (segment *Segment) migratePropertyValue(property properties.Property, value any) {
if !segment.hasProperty(property) {
return
}
segment.Properties[property] = value
}
func (segment *Segment) migratePropertyKey(oldProperty, newProperty properties.Property) {
if !segment.hasProperty(oldProperty) {
return
}
value := segment.Properties[oldProperty]
delete(segment.Properties, oldProperty)
segment.Properties[newProperty] = value
}
func (segment *Segment) migrateTemplate() {
defer segment.migratePreAndPostFix()
if segment.hasProperty(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
}
segment.Properties[segmentTemplate] = segment.writer.Template()
}
func (segment *Segment) migrateIconOverride(property properties.Property, overrideValue string) {
if !segment.hasProperty(property) {
return
}
template := segment.Properties.GetString(segmentTemplate, segment.writer.Template())
if strings.Contains(template, overrideValue) {
template = strings.ReplaceAll(template, overrideValue, segment.Properties.GetString(property, ""))
}
segment.Properties[segmentTemplate] = template
delete(segment.Properties, property)
}
func (segment *Segment) migrateColorOverride(property properties.Property, template string, background bool) {
if !segment.hasProperty(property) {
return
}
color := segment.Properties.GetColor(property, "")
delete(segment.Properties, property)
if len(color) == 0 {
return
}
colorTemplate := fmt.Sprintf(template, color)
if background {
segment.BackgroundTemplates = append(segment.BackgroundTemplates, colorTemplate)
return
}
segment.ForegroundTemplates = append(segment.ForegroundTemplates, colorTemplate)
}
func (segment *Segment) migrateInlineColorOverride(property properties.Property, old string) {
if !segment.hasProperty(property) {
return
}
color := segment.Properties.GetColor(property, "")
delete(segment.Properties, property)
if len(color) == 0 {
return
}
colorTemplate := fmt.Sprintf("<%s>%s</>", color, old)
template := segment.Properties.GetString(segmentTemplate, segment.writer.Template())
template = strings.ReplaceAll(template, old, colorTemplate)
segment.Properties[segmentTemplate] = template
}
func (segment *Segment) migratePreAndPostFix() {
template := segment.Properties.GetString(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[segmentTemplate] = template
}

View file

@ -1,481 +0,0 @@
package config
import (
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/jandedobbeleer/oh-my-posh/src/segments"
"github.com/stretchr/testify/assert"
testify_ "github.com/stretchr/testify/mock"
)
const (
Foo = "foo"
Bar = "bar"
FooBar = "foobar"
)
func TestHasProperty(t *testing.T) {
cases := []struct {
Props properties.Map
Case string
Property properties.Property
Expected bool
}{
{Case: "Match", Expected: true, Property: Foo, Props: properties.Map{Foo: "bar"}},
{Case: "No Match", Expected: false, Property: Foo, Props: properties.Map{Bar: "foo"}},
{Case: "No properties", Expected: false},
}
for _, tc := range cases {
segment := &Segment{
Properties: tc.Props,
}
got := segment.hasProperty(tc.Property)
assert.Equal(t, tc.Expected, got, tc.Case)
}
}
func TestMigratePropertyValue(t *testing.T) {
cases := []struct {
Expected any
Props properties.Map
Case string
Property properties.Property
}{
{Case: "Match", Expected: "foo", Property: Foo, Props: properties.Map{Foo: "bar"}},
{Case: "No Match", Expected: nil, Property: Foo, Props: properties.Map{Bar: "foo"}},
}
for _, tc := range cases {
segment := &Segment{
Properties: tc.Props,
}
segment.migratePropertyValue(tc.Property, tc.Expected)
assert.Equal(t, tc.Expected, segment.Properties[tc.Property], tc.Case)
}
}
func TestMigratePropertyKey(t *testing.T) {
cases := []struct {
Expected any
Props properties.Map
Case string
OldProperty properties.Property
NewProperty properties.Property
}{
{Case: "Match", Expected: "bar", OldProperty: Foo, NewProperty: Bar, Props: properties.Map{Foo: "bar"}},
{Case: "No match", Expected: nil, OldProperty: Foo, NewProperty: Bar, Props: properties.Map{FooBar: "bar"}},
{Case: "No migration", Expected: "bar", OldProperty: Foo, NewProperty: Bar, Props: properties.Map{Bar: "bar"}},
}
for _, tc := range cases {
segment := &Segment{
Properties: tc.Props,
}
segment.migratePropertyKey(tc.OldProperty, tc.NewProperty)
assert.Equal(t, tc.Expected, segment.Properties[tc.NewProperty], tc.Case)
assert.NotContains(t, segment.Properties, tc.OldProperty, tc.Case)
}
}
type MockedWriter struct {
template string
text string
}
func (m *MockedWriter) Enabled() bool {
return true
}
func (m *MockedWriter) Template() string {
return m.template
}
func (m *MockedWriter) Text() string {
return m.text
}
func (m *MockedWriter) SetText(text string) {
m.text = text
}
func (m *MockedWriter) Init(_ properties.Properties, _ runtime.Environment) {}
func TestIconOverride(t *testing.T) {
cases := []struct {
Props properties.Map
Case string
Expected string
Property properties.Property
}{
{
Case: "Match",
Expected: "hello bar bar",
Property: Foo,
Props: properties.Map{
Foo: " bar ",
segmentTemplate: "hello foo bar",
},
},
{
Case: "No match",
Expected: "hello foo bar",
Property: Foo,
Props: properties.Map{
Bar: " bar ",
segmentTemplate: "hello foo bar",
},
},
}
for _, tc := range cases {
segment := &Segment{
Properties: tc.Props,
writer: &MockedWriter{
template: tc.Props.GetString(segmentTemplate, ""),
},
}
segment.migrateIconOverride(tc.Property, " foo ")
assert.Equal(t, tc.Expected, segment.Properties[segmentTemplate], tc.Case)
}
}
func TestColorMigration(t *testing.T) {
cases := []struct {
Props properties.Map
Case string
Expected string
Property properties.Property
Template string
Background bool
NoOverride bool
}{
{
Case: "Foreground override",
Expected: "hello green bar",
Template: "hello %s bar",
Property: Foo,
Props: properties.Map{
Foo: "green",
},
},
{
Case: "Background override",
Expected: "hello green bar",
Template: "hello %s bar",
Property: Foo,
Background: true,
Props: properties.Map{
Foo: "green",
},
},
{
Case: "No override",
Expected: "hello green bar",
Template: "hello %s bar",
Property: Foo,
NoOverride: true,
},
}
for _, tc := range cases {
segment := &Segment{
Properties: tc.Props,
}
if tc.Background {
segment.Properties[colorBackground] = true
}
segment.migrateColorOverride(tc.Property, tc.Template, tc.Background)
templates := segment.ForegroundTemplates
if tc.Background {
templates = segment.BackgroundTemplates
}
if tc.NoOverride {
assert.Empty(t, templates, tc.Case)
return
}
lastElement := templates[len(templates)-1]
assert.Equal(t, tc.Expected, lastElement, tc.Case)
}
}
func TestSegmentTemplateMigration(t *testing.T) {
cases := []struct {
Props properties.Map
Case string
Expected string
Type SegmentType
}{
{
Case: "GIT",
Expected: " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} working {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} and{{ end }}{{ if .Staging.Changed }} staged {{ .Staging.String }}{{ end }} ", //nolint: lll
Type: GIT,
Props: properties.Map{
"local_working_icon": " working ",
"local_staged_icon": " staged ",
"worktree_count_icon": " worktree ",
"stash_count_icon": " stash ",
"status_separator_icon": " and",
},
},
{
Case: "GIT - Staging and Working Color",
Expected: " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} working <#123456>{{ .Working.String }}</>{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} and{{ end }}{{ if .Staging.Changed }} staged <#123456>{{ .Staging.String }}</>{{ end }} ", //nolint: lll
Type: GIT,
Props: properties.Map{
"local_working_icon": " working ",
"local_staged_icon": " staged ",
"worktree_count_icon": " worktree ",
"stash_count_icon": " stash ",
"status_separator_icon": " and",
"working_color": "#123456",
"staging_color": "#123456",
},
},
{
Case: "EXIT - No exit code",
Expected: " {{ if gt .Code 0 }}FAIL{{ else }}SUCCESS{{ end }} ",
Type: EXIT,
Props: properties.Map{
"display_exit_code": false,
"success_icon": "SUCCESS",
"error_icon": "FAIL",
},
},
{
Case: "EXIT - No spaces",
Expected: "{{ if gt .Code 0 }}💩{{ else }}🌵{{ end }}",
Type: EXIT,
Props: properties.Map{
"prefix": "",
"postfix": "",
"display_exit_code": false,
"always_enabled": true,
"success_icon": "🌵",
"error_icon": "💩",
},
},
{
Case: "EXIT - Always numeric",
Expected: " {{ if gt .Code 0 }}FAIL {{ .Code }}{{ else }}SUCCESS{{ end }} ",
Type: EXIT,
Props: properties.Map{
"always_numeric": true,
"success_icon": "SUCCESS",
"error_icon": "FAIL",
},
},
{
Case: "EXIT - .Text",
Expected: " {{ .Meaning }} ",
Type: EXIT,
Props: properties.Map{
"template": "{{ .Text }}",
},
},
{
Case: "BATTERY",
Expected: ` {{ if not .Error }}{{ $stateList := list "Discharging" "Full" }}{{ if has .State.String $stateList }}{{ .Icon }}{{ .Percentage }}{{ end }}{{ end }}{{ .Error }} `,
Type: BATTERY,
Props: properties.Map{
"display_charging": false,
},
},
{
Case: "SESSION",
Expected: " {{ if .SSHSession }}SSH {{ end }}{{ .UserName }}@{{ .HostName }} ",
Type: SESSION,
Props: properties.Map{
"ssh_icon": "SSH ",
},
},
{
Case: "SESSION no HOST",
Expected: " {{ if .SSHSession }}\ueba9 {{ end }}{{ .UserName }} ",
Type: SESSION,
Props: properties.Map{
"display_host": false,
},
},
{
Case: "SESSION no USER",
Expected: " {{ if .SSHSession }}\ueba9 {{ end }}{{ .HostName }} ",
Type: SESSION,
Props: properties.Map{
"display_user": false,
},
},
{
Case: "SESSION no USER nor HOST",
Expected: " {{ if .SSHSession }}\ueba9 {{ end }} ",
Type: SESSION,
Props: properties.Map{
"display_user": false,
"display_host": false,
},
},
{
Case: "SESSION - Color overrides",
Expected: " {{ if .SSHSession }}\ueba9 {{ end }}<#123456>{{ .UserName }}</>@<#789012>{{ .HostName }}</> ",
Type: SESSION,
Props: properties.Map{
"user_color": "#123456",
"host_color": "#789012",
},
},
{
Case: "SESSION - Existing template",
Expected: " {{ .UserName }}@{{ .HostName }} ",
Type: SESSION,
Props: properties.Map{
"template": "{{ .UserName }}@{{ .ComputerName }}",
},
},
}
for _, tc := range cases {
segment := &Segment{
Type: tc.Type,
Properties: tc.Props,
}
env := &mock.Environment{}
env.On("Debug", testify_.Anything).Return(nil)
segment.migrationOne(env)
assert.Equal(t, tc.Expected, segment.Properties[segmentTemplate], tc.Case)
}
}
func TestInlineColorOverride(t *testing.T) {
cases := []struct {
Props properties.Map
Case string
Expected string
Property properties.Property
}{
{
Case: "Match",
Expected: "hello <#123456>foo</> bar",
Property: Foo,
Props: properties.Map{
Foo: "#123456",
segmentTemplate: "hello foo bar",
},
},
{
Case: "No match",
Expected: "hello foo bar",
Property: Foo,
Props: properties.Map{
Bar: "#123456",
segmentTemplate: "hello foo bar",
},
},
}
for _, tc := range cases {
segment := &Segment{
Properties: tc.Props,
writer: &MockedWriter{
template: tc.Props.GetString(segmentTemplate, ""),
},
}
segment.migrateInlineColorOverride(tc.Property, "foo")
assert.Equal(t, tc.Expected, segment.Properties[segmentTemplate], tc.Case)
}
}
func TestMigratePreAndPostfix(t *testing.T) {
cases := []struct {
Props properties.Map
Case string
Expected string
}{
{
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: segments.NameTemplate,
Props: properties.Map{
"prefix": " ",
"template": "{{ .Name }}",
},
},
{
Case: "Postfix",
Expected: segments.NameTemplate,
Props: properties.Map{
"postfix": " ",
"template": "{{ .Name }} ",
},
},
}
for _, tc := range cases {
segment := &Segment{
Properties: tc.Props,
writer: &MockedWriter{
template: tc.Props.GetString(segmentTemplate, ""),
},
}
segment.migrateTemplate()
assert.Equal(t, tc.Expected, segment.Properties[segmentTemplate], tc.Case)
assert.NotContains(t, segment.Properties, "prefix", tc.Case)
assert.NotContains(t, segment.Properties, "postfix", tc.Case)
}
}
func TestMigrateConfig(t *testing.T) {
cases := []struct {
Case string
Expected string
Template string
}{
{Case: "Path", Expected: "{{ .PWD }}", Template: "{{ .Path }}"},
{Case: "No Path", Expected: "foo", Template: "foo"},
{Case: "Empty", Expected: "", Template: ""},
}
for _, tc := range cases {
cfg := &Config{
ConsoleTitleTemplate: tc.Template,
env: &mock.Environment{},
}
cfg.Migrate()
assert.Equal(t, tc.Expected, cfg.ConsoleTitleTemplate, tc.Case)
}
}
func TestMigrationTwo(t *testing.T) {
cases := []struct {
Case string
Expected string
Template string
}{
{
Case: "With template",
Expected: "{{ .Name }}",
Template: "{{ .Name }}",
},
{
Case: "No template",
Expected: "",
},
}
for _, tc := range cases {
segment := &Segment{
Type: SESSION,
Properties: properties.Map{},
writer: &MockedWriter{
template: tc.Template,
},
}
if tc.Template != "" {
segment.Properties[segmentTemplate] = tc.Template
}
segment.migrationTwo(&mock.Environment{})
assert.Equal(t, tc.Expected, segment.Template, tc.Case)
assert.NotContains(t, segment.Properties, segmentTemplate, tc.Case)
}
}

View file

@ -36,13 +36,12 @@ func (s *SegmentStyle) resolve(context any) SegmentStyle {
}
type Segment struct {
writer SegmentWriter
env runtime.Environment
writer SegmentWriter
env runtime.Environment
Properties properties.Map `json:"properties,omitempty" toml:"properties,omitempty"`
Cache *cache.Config `json:"cache,omitempty" toml:"cache,omitempty"`
Filler string `json:"filler,omitempty" toml:"filler,omitempty"`
styleCache SegmentStyle
Alias string `json:"alias,omitempty" toml:"alias,omitempty"`
LeadingPowerlineSymbol string `json:"leading_powerline_symbol,omitempty" toml:"leading_powerline_symbol,omitempty"`
name string
LeadingDiamond string `json:"leading_diamond,omitempty" toml:"leading_diamond,omitempty"`
TrailingDiamond string `json:"trailing_diamond,omitempty" toml:"trailing_diamond,omitempty"`
@ -51,21 +50,22 @@ type Segment struct {
TemplatesLogic template.Logic `json:"templates_logic,omitempty" toml:"templates_logic,omitempty"`
PowerlineSymbol string `json:"powerline_symbol,omitempty" toml:"powerline_symbol,omitempty"`
Background color.Ansi `json:"background" toml:"background"`
Alias string `json:"alias,omitempty" toml:"alias,omitempty"`
Filler string `json:"filler,omitempty" toml:"filler,omitempty"`
Type SegmentType `json:"type,omitempty" toml:"type,omitempty"`
Style SegmentStyle `json:"style,omitempty" toml:"style,omitempty"`
LeadingPowerlineSymbol string `json:"leading_powerline_symbol,omitempty" toml:"leading_powerline_symbol,omitempty"`
ForegroundTemplates template.List `json:"foreground_templates,omitempty" toml:"foreground_templates,omitempty"`
Tips []string `json:"tips,omitempty" toml:"tips,omitempty"`
BackgroundTemplates template.List `json:"background_templates,omitempty" toml:"background_templates,omitempty"`
MinWidth int `json:"min_width,omitempty" toml:"min_width,omitempty"`
MaxWidth int `json:"max_width,omitempty" toml:"max_width,omitempty"`
Duration time.Duration `json:"-" toml:"-"`
NameLength int `json:"-" toml:"-"`
Interactive bool `json:"interactive,omitempty" toml:"interactive,omitempty"`
Enabled bool `json:"-" toml:"-"`
Newline bool `json:"newline,omitempty" toml:"newline,omitempty"`
InvertPowerline bool `json:"invert_powerline,omitempty" toml:"invert_powerline,omitempty"`
styleCache SegmentStyle
ForegroundTemplates template.List `json:"foreground_templates,omitempty" toml:"foreground_templates,omitempty"`
Tips []string `json:"tips,omitempty" toml:"tips,omitempty"`
BackgroundTemplates template.List `json:"background_templates,omitempty" toml:"background_templates,omitempty"`
Templates template.List `json:"templates,omitempty" toml:"templates,omitempty"`
MinWidth int `json:"min_width,omitempty" toml:"min_width,omitempty"`
MaxWidth int `json:"max_width,omitempty" toml:"max_width,omitempty"`
Duration time.Duration `json:"-" toml:"-"`
NameLength int `json:"-" toml:"-"`
Interactive bool `json:"interactive,omitempty" toml:"interactive,omitempty"`
Enabled bool `json:"-" toml:"-"`
Newline bool `json:"newline,omitempty" toml:"newline,omitempty"`
InvertPowerline bool `json:"invert_powerline,omitempty" toml:"invert_powerline,omitempty"`
}
func (segment *Segment) Name() string {
@ -112,6 +112,10 @@ func (segment *Segment) Execute(env runtime.Environment) {
return
}
if len(segment.Templates) != 0 {
segment.Template = strings.Join(segment.Templates, "")
}
if segment.writer.Enabled() {
segment.Enabled = true
env.TemplateCache().AddSegmentData(segment.Name(), segment.writer)

View file

@ -254,5 +254,5 @@
"white": "#FFFFFF",
"ytm": "#1BD760"
},
"version": 2
"version": 3
}

View file

@ -223,5 +223,5 @@
],
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -113,5 +113,5 @@
"foreground": "#FEF5ED",
"template": "\ue285 "
},
"version": 2
"version": 3
}

View file

@ -101,5 +101,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -61,5 +61,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -63,5 +63,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -53,5 +53,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -46,5 +46,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -54,5 +54,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -273,5 +273,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -189,5 +189,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -49,5 +49,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -122,5 +122,5 @@
}
],
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.Folder | replace \"~\" \"🏚\" }} @ {{.HostName}}",
"version": 2
"version": 3
}

View file

@ -117,5 +117,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -162,5 +162,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -174,5 +174,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -138,5 +138,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -66,5 +66,5 @@
],
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.UserName}} \u2794 📁{{.Folder}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -67,5 +67,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -63,6 +63,6 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -63,6 +63,6 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -63,5 +63,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -63,5 +63,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -54,5 +54,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -82,13 +82,16 @@
"leading_diamond": "\uE0B6",
"properties": {
"url": "https://wakatime.com/api/v1/users/current/summaries?start=today&end=today&api_key={{ .Env.WAKATIME_API_KEY }}",
"http_timeout": 2000,
"cache_timeout": 5
"http_timeout": 2000
},
"style": "diamond",
"template": "{{ if and (.Env.WAKATIME_API_KEY) (eq \"False\" (title (default \"False\" .Env.DISABLE_SEGMENT_WAKATIME))) (gt .CumulativeTotal.Seconds 0) }}\uFA19 {{ secondsRound .CumulativeTotal.Seconds }}.{{ end }}",
"trailing_diamond": "\uE0B4 ",
"type": "wakatime"
"type": "wakatime",
"cache": {
"duration": "1minute",
"strategy": "session"
}
},
{
"background_templates": [
@ -291,5 +294,5 @@
"foreground": "p:c-secondary-ellipsis",
"template": " ... "
},
"version": 2
"version": 3
}

View file

@ -65,5 +65,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -132,5 +132,5 @@
"foreground": "#FEF5ED",
"template": "\ue285 "
},
"version": 2
"version": 3
}

View file

@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"version": 2,
"version": 3,
"console_title_template": "{{ if or .Root .Segments.Git.RepoName }}[ {{ if .Root }}Admin{{ end }}{{ if and .Root .Segments.Git.RepoName }}@{{ end }}{{ if .Segments.Git.RepoName }}{{ .Segments.Git.RepoName }}{{ end }} ]{{ end }} {{ .Folder }}",
"palette": {
"background-color": "transparent",

View file

@ -143,5 +143,5 @@
],
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -40,5 +40,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -105,5 +105,5 @@
}
],
"console_title_template": "{{if .Root}}(Admin) {{end}}{{.Folder}}",
"version": 2
"version": 3
}

View file

@ -49,5 +49,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -79,5 +79,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -77,5 +77,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -152,5 +152,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -114,5 +114,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -54,5 +54,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -51,5 +51,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -224,5 +224,5 @@
],
"console_title_template": "{{if .Root}}Admin: {{end}} {{.Folder}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -145,5 +145,5 @@
}
],
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.Folder | replace \"~\" \"🏚\" }} @ {{.HostName}}",
"version": 2
"version": 3
}

View file

@ -102,5 +102,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -120,5 +120,5 @@
}
],
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.Folder | replace \"~\" \"🏚\" }} @ {{.HostName}}",
"version": 2
"version": 3
}

View file

@ -131,5 +131,5 @@
],
"console_title_template": "{{ .Folder }}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -55,5 +55,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -75,5 +75,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -53,5 +53,5 @@
],
"console_title_template": "{{.Folder}}{{if .Root}} :: root{{end}} :: {{.Shell}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -126,5 +126,5 @@
],
"console_title_template": "{{if .Root}}[root] {{end}}{{.Shell}} in <{{.Folder}}>",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -131,5 +131,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -79,5 +79,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -171,5 +171,5 @@
"foreground": "#FEF5ED",
"template": "\ue285 "
},
"version": 2
"version": 3
}

View file

@ -79,5 +79,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -139,5 +139,5 @@
"foreground": "#FFD700",
"template": "{{if .Root}}\u26a1 {{end}}🚀 "
},
"version": 2
"version": 3
}

View file

@ -223,5 +223,5 @@
],
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -96,5 +96,5 @@
],
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.Folder | replace \"~\" \"🏠\"}} @ {{.HostName}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -144,5 +144,5 @@
],
"console_title_template": "{{if .Root}}root :: {{end}}{{.Shell}} :: {{.Folder}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -46,5 +46,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -106,5 +106,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -162,5 +162,5 @@
],
"console_title_template": "{{if .Root}}Admin: {{end}} {{.Folder}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -87,5 +87,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -210,5 +210,5 @@
"foreground": "#D6DEEB",
"template": "\u2570\u2500\u276f "
},
"version": 2
"version": 3
}

View file

@ -29,5 +29,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -130,5 +130,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -98,5 +98,5 @@
],
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -156,5 +156,5 @@
}
],
"pwd": "osc99",
"version": 2
"version": 3
}

View file

@ -56,5 +56,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -115,5 +115,5 @@
],
"console_title_template": "{{if .Root}}\u26a1 {{end}}{{.Folder}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -48,5 +48,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -92,5 +92,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -142,5 +142,5 @@
],
"console_title_template": "{{.UserName}}@{{.HostName}} : {{.Folder}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -109,5 +109,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -61,5 +61,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -111,5 +111,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -106,5 +106,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -64,5 +64,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -412,5 +412,5 @@
"foreground": "#d6deeb",
"template": "\ue285 "
},
"version": 2
"version": 3
}

View file

@ -79,5 +79,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -89,5 +89,5 @@
"foreground": "#100e23",
"template": " \uf061 "
},
"version": 2
"version": 3
}

View file

@ -65,5 +65,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -74,5 +74,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -67,5 +67,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -75,5 +75,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -113,5 +113,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -152,5 +152,5 @@
],
"console_title_template": "{{if .Root}}root :: {{end}}{{.Shell}} :: {{.Folder}}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -86,5 +86,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -122,6 +122,6 @@
"console_title_template": "{{ .Folder }}",
"final_space": true,
"osc99": true,
"version": 2
"version": 3
}

View file

@ -89,5 +89,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -43,5 +43,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -100,5 +100,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -212,5 +212,5 @@
],
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 2
"version": 3
}

View file

@ -56,5 +56,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -51,5 +51,5 @@
"type": "prompt"
}
],
"version": 2
"version": 3
}

View file

@ -86,5 +86,5 @@
],
"template": "\u276f "
},
"version": 2
"version": 3
}

View file

@ -124,5 +124,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -48,5 +48,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

View file

@ -39,5 +39,5 @@
}
],
"final_space": true,
"version": 2
"version": 3
}

Some files were not shown because too many files have changed in this diff Show more