mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 20:09:39 -08:00
parent
50d98f5e68
commit
328601be78
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -168,21 +168,22 @@ type SystemInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type TemplateCache struct {
|
type TemplateCache struct {
|
||||||
Root bool
|
Root bool
|
||||||
PWD string
|
PWD string
|
||||||
Folder string
|
Folder string
|
||||||
Shell string
|
Shell string
|
||||||
ShellVersion string
|
ShellVersion string
|
||||||
UserName string
|
UserName string
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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}}`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue