Skip to content

Commit

Permalink
refactor: rename concurrent to maps
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jul 4, 2024
1 parent 822e2b5 commit df4a81e
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 67 deletions.
6 changes: 4 additions & 2 deletions src/cache/command.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/cache/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import (
"path/filepath"
"time"

"github.com/jandedobbeleer/oh-my-posh/src/concurrent"
"github.com/jandedobbeleer/oh-my-posh/src/maps"
)

const (
CacheFile = "/omp.cache"
)

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)
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/cache/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
46 changes: 0 additions & 46 deletions src/concurrent/map.go

This file was deleted.

36 changes: 36 additions & 0 deletions src/maps/concurrent.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions src/maps/simple.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 7 additions & 7 deletions src/runtime/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -196,7 +196,7 @@ type Environment interface {

type Terminal struct {
CmdFlags *Flags
Var concurrent.SimpleMap
Var maps.Simple

cwd string
host string
Expand All @@ -207,7 +207,7 @@ type Terminal struct {

sync.RWMutex

lsDirMap concurrent.Map
lsDirMap maps.Concurrent
}

func (term *Terminal) Init() {
Expand All @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/template/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/template/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}}`,
},
}
Expand All @@ -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{
Expand Down

0 comments on commit df4a81e

Please sign in to comment.