diff --git a/src/cache/command.go b/src/cache/command.go index 326a5c0f..2e249d62 100644 --- a/src/cache/command.go +++ b/src/cache/command.go @@ -1,9 +1,11 @@ package cache -import "github.com/jandedobbeleer/oh-my-posh/src/concurrent" +import ( + "github.com/jandedobbeleer/oh-my-posh/src/maps" +) type Command struct { - Commands *concurrent.Map + Commands *maps.Concurrent } func (c *Command) Set(command, path string) { diff --git a/src/cache/file.go b/src/cache/file.go index 3dfb060a..eb725b0c 100644 --- a/src/cache/file.go +++ b/src/cache/file.go @@ -6,7 +6,7 @@ import ( "path/filepath" "time" - "github.com/jandedobbeleer/oh-my-posh/src/concurrent" + "github.com/jandedobbeleer/oh-my-posh/src/maps" ) const ( @@ -14,13 +14,13 @@ const ( ) type File struct { - cache *concurrent.Map + cache *maps.Concurrent cachePath string dirty bool } func (fc *File) Init(cachePath string) { - fc.cache = concurrent.NewMap() + fc.cache = maps.NewConcurrent() fc.cachePath = cachePath cacheFilePath := filepath.Join(fc.cachePath, CacheFile) content, err := os.ReadFile(cacheFilePath) @@ -47,7 +47,7 @@ func (fc *File) Close() { return } - cache := fc.cache.ToSimpleMap() + cache := fc.cache.ToSimple() if dump, err := json.MarshalIndent(cache, "", " "); err == nil { cacheFilePath := filepath.Join(fc.cachePath, CacheFile) diff --git a/src/cache/template.go b/src/cache/template.go index cb2b78c1..7efdff15 100644 --- a/src/cache/template.go +++ b/src/cache/template.go @@ -3,7 +3,7 @@ package cache import ( "sync" - "github.com/jandedobbeleer/oh-my-posh/src/concurrent" + "github.com/jandedobbeleer/oh-my-posh/src/maps" ) type Template struct { @@ -17,13 +17,13 @@ type Template struct { HostName string Code int Env map[string]string - Var concurrent.SimpleMap + Var maps.Simple OS string WSL bool PromptCount int SHLVL int - Segments *concurrent.Map - SegmentsCache concurrent.SimpleMap + Segments *maps.Concurrent + SegmentsCache maps.Simple Initialized bool sync.RWMutex diff --git a/src/concurrent/map.go b/src/concurrent/map.go deleted file mode 100644 index e2fadbc2..00000000 --- a/src/concurrent/map.go +++ /dev/null @@ -1,46 +0,0 @@ -package concurrent - -import "sync" - -func NewMap() *Map { - var cm Map - return &cm -} - -type Map sync.Map - -func (cm *Map) Set(key string, value any) { - (*sync.Map)(cm).Store(key, value) -} - -func (cm *Map) Get(key string) (any, bool) { - return (*sync.Map)(cm).Load(key) -} - -func (cm *Map) Delete(key string) { - (*sync.Map)(cm).Delete(key) -} - -func (cm *Map) Contains(key string) bool { - _, ok := (*sync.Map)(cm).Load(key) - return ok -} - -func (cm *Map) ToSimpleMap() SimpleMap { - list := make(map[string]any) - (*sync.Map)(cm).Range(func(key, value any) bool { - list[key.(string)] = value - return true - }) - return list -} - -type SimpleMap map[string]any - -func (m SimpleMap) ConcurrentMap() *Map { - var cm Map - for k, v := range m { - cm.Set(k, v) - } - return &cm -} diff --git a/src/maps/concurrent.go b/src/maps/concurrent.go new file mode 100644 index 00000000..c5bb1537 --- /dev/null +++ b/src/maps/concurrent.go @@ -0,0 +1,36 @@ +package maps + +import "sync" + +func NewConcurrent() *Concurrent { + var cm Concurrent + return &cm +} + +type Concurrent sync.Map + +func (cm *Concurrent) Set(key string, value any) { + (*sync.Map)(cm).Store(key, value) +} + +func (cm *Concurrent) Get(key string) (any, bool) { + return (*sync.Map)(cm).Load(key) +} + +func (cm *Concurrent) Delete(key string) { + (*sync.Map)(cm).Delete(key) +} + +func (cm *Concurrent) Contains(key string) bool { + _, ok := (*sync.Map)(cm).Load(key) + return ok +} + +func (cm *Concurrent) ToSimple() Simple { + list := make(map[string]any) + (*sync.Map)(cm).Range(func(key, value any) bool { + list[key.(string)] = value + return true + }) + return list +} diff --git a/src/maps/simple.go b/src/maps/simple.go new file mode 100644 index 00000000..696a4550 --- /dev/null +++ b/src/maps/simple.go @@ -0,0 +1,11 @@ +package maps + +type Simple map[string]any + +func (m Simple) ToConcurrent() *Concurrent { + var cm Concurrent + for k, v := range m { + cm.Set(k, v) + } + return &cm +} diff --git a/src/runtime/os.go b/src/runtime/os.go index 75df9bb7..9e043ce9 100644 --- a/src/runtime/os.go +++ b/src/runtime/os.go @@ -18,8 +18,8 @@ import ( "time" "github.com/jandedobbeleer/oh-my-posh/src/cache" - "github.com/jandedobbeleer/oh-my-posh/src/concurrent" "github.com/jandedobbeleer/oh-my-posh/src/log" + "github.com/jandedobbeleer/oh-my-posh/src/maps" "github.com/jandedobbeleer/oh-my-posh/src/regex" "github.com/jandedobbeleer/oh-my-posh/src/runtime/battery" "github.com/jandedobbeleer/oh-my-posh/src/runtime/cmd" @@ -196,7 +196,7 @@ type Environment interface { type Terminal struct { CmdFlags *Flags - Var concurrent.SimpleMap + Var maps.Simple cwd string host string @@ -207,7 +207,7 @@ type Terminal struct { sync.RWMutex - lsDirMap concurrent.Map + lsDirMap maps.Concurrent } func (term *Terminal) Init() { @@ -228,7 +228,7 @@ func (term *Terminal) Init() { term.fileCache.Init(term.CachePath()) term.resolveConfigPath() term.cmdCache = &cache.Command{ - Commands: concurrent.NewMap(), + Commands: maps.NewConcurrent(), } term.tmplCache = &cache.Template{} @@ -710,7 +710,7 @@ func (term *Terminal) saveTemplateCache() { } tmplCache := term.TemplateCache() - tmplCache.SegmentsCache = tmplCache.Segments.ToSimpleMap() + tmplCache.SegmentsCache = tmplCache.Segments.ToSimple() templateCache, err := json.Marshal(tmplCache) if err == nil { @@ -740,7 +740,7 @@ func (term *Terminal) LoadTemplateCache() { return } - tmplCache.Segments = tmplCache.SegmentsCache.ConcurrentMap() + tmplCache.Segments = tmplCache.SegmentsCache.ToConcurrent() tmplCache.Initialized = true term.tmplCache = &tmplCache @@ -765,7 +765,7 @@ func (term *Terminal) TemplateCache() *cache.Template { tmplCache.ShellVersion = term.CmdFlags.ShellVersion tmplCache.Code, _ = term.StatusCodes() tmplCache.WSL = term.IsWsl() - tmplCache.Segments = concurrent.NewMap() + tmplCache.Segments = maps.NewConcurrent() tmplCache.PromptCount = term.CmdFlags.PromptCount tmplCache.Env = make(map[string]string) tmplCache.Var = make(map[string]any) diff --git a/src/template/text.go b/src/template/text.go index b64027b9..2671c758 100644 --- a/src/template/text.go +++ b/src/template/text.go @@ -179,7 +179,7 @@ func (t *Text) cleanTemplate() { // as we can't provide a clean way to access the list // of segments, we need to replace the property with // the list of segments so they can be accessed directly - property = strings.Replace(property, ".Segments", ".Segments.SimpleMap", 1) + property = strings.Replace(property, ".Segments", ".Segments.ToSimple", 1) result += property } else { // check if we have the same property in Data diff --git a/src/template/text_test.go b/src/template/text_test.go index 43e7ded5..0b0cd304 100644 --- a/src/template/text_test.go +++ b/src/template/text_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/cache" - "github.com/jandedobbeleer/oh-my-posh/src/concurrent" + "github.com/jandedobbeleer/oh-my-posh/src/maps" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -330,7 +330,7 @@ func TestCleanTemplate(t *testing.T) { }, { Case: "Replace a direct call to .Segments with .Segments.List", - Expected: `{{.Segments.SimpleMap.Git.Repo}}`, + Expected: `{{.Segments.ToSimple.Git.Repo}}`, Template: `{{.Segments.Git.Repo}}`, }, } @@ -355,7 +355,7 @@ func TestSegmentContains(t *testing.T) { } env := &mock.Environment{} - segments := concurrent.NewMap() + segments := maps.NewConcurrent() segments.Set("Git", "foo") env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) env.On("TemplateCache").Return(&cache.Template{