fix(template): save Segments correctly

resolves #4143
This commit is contained in:
Jan De Dobbeleer 2023-08-08 09:09:05 +02:00 committed by Jan De Dobbeleer
parent 50d98f5e68
commit 328601be78
5 changed files with 34 additions and 20 deletions

View file

@ -56,7 +56,7 @@ func (fc *fileCache) Close() {
if !fc.dirty { if !fc.dirty {
return return
} }
cache := fc.cache.List() cache := fc.cache.SimpleMap()
if dump, err := json.MarshalIndent(cache, "", " "); err == nil { if dump, err := json.MarshalIndent(cache, "", " "); err == nil {
cacheFilePath := filepath.Join(fc.cachePath, CacheFile) cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
_ = os.WriteFile(cacheFilePath, dump, 0644) _ = os.WriteFile(cacheFilePath, dump, 0644)

View file

@ -26,7 +26,7 @@ func (cm *ConcurrentMap) Contains(key string) bool {
return ok return ok
} }
func (cm *ConcurrentMap) List() map[string]any { func (cm *ConcurrentMap) SimpleMap() SimpleMap {
list := make(map[string]any) list := make(map[string]any)
(*sync.Map)(cm).Range(func(key, value any) bool { (*sync.Map)(cm).Range(func(key, value any) bool {
list[key.(string)] = value list[key.(string)] = value
@ -34,3 +34,13 @@ func (cm *ConcurrentMap) List() map[string]any {
}) })
return list return list
} }
type SimpleMap map[string]any
func (m SimpleMap) ConcurrentMap() *ConcurrentMap {
var cm ConcurrentMap
for k, v := range m {
cm.Set(k, v)
}
return &cm
}

View file

@ -177,12 +177,13 @@ type TemplateCache struct {
HostName string HostName string
Code int Code int
Env map[string]string Env map[string]string
Var map[string]interface{} Var SimpleMap
OS string OS string
WSL bool WSL bool
PromptCount int PromptCount int
SHLVL int SHLVL int
Segments *ConcurrentMap Segments *ConcurrentMap
SegmentsCache SimpleMap
initialized bool initialized bool
sync.RWMutex sync.RWMutex
@ -745,7 +746,9 @@ func (env *Shell) saveTemplateCache() {
if !canSave { if !canSave {
return return
} }
templateCache, err := json.Marshal(env.TemplateCache()) cache := env.TemplateCache()
cache.SegmentsCache = cache.Segments.SimpleMap()
templateCache, err := json.Marshal(cache)
if err == nil { if err == nil {
env.fileCache.Set(TEMPLATECACHE, string(templateCache), 1440) env.fileCache.Set(TEMPLATECACHE, string(templateCache), 1440)
} }
@ -769,6 +772,7 @@ func (env *Shell) LoadTemplateCache() {
env.Error(err) env.Error(err)
return return
} }
tmplCache.Segments = tmplCache.SegmentsCache.ConcurrentMap()
tmplCache.initialized = true tmplCache.initialized = true
env.tmplCache = &tmplCache env.tmplCache = &tmplCache
} }

View file

@ -163,7 +163,7 @@ func (t *Text) cleanTemplate() {
// as we can't provide a clean way to access the list // as we can't provide a clean way to access the list
// of segments, we need to replace the property with // of segments, we need to replace the property with
// the list of segments so they can be accessed directly // the list of segments so they can be accessed directly
property = strings.Replace(property, ".Segments", ".Segments.List", 1) property = strings.Replace(property, ".Segments", ".Segments.SimpleMap", 1)
result += property result += property
} else { } else {
// check if we have the same property in Data // check if we have the same property in Data

View file

@ -327,7 +327,7 @@ func TestCleanTemplate(t *testing.T) {
}, },
{ {
Case: "Replace a direct call to .Segments with .Segments.List", Case: "Replace a direct call to .Segments with .Segments.List",
Expected: `{{.Segments.List.Git.Repo}}`, Expected: `{{.Segments.SimpleMap.Git.Repo}}`,
Template: `{{.Segments.Git.Repo}}`, Template: `{{.Segments.Git.Repo}}`,
}, },
} }