mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-09 20:44:03 -08:00
refactor: rename concurrent to maps
This commit is contained in:
parent
822e2b5b48
commit
df4a81e2f6
6
src/cache/command.go
vendored
6
src/cache/command.go
vendored
|
@ -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) {
|
||||
|
|
8
src/cache/file.go
vendored
8
src/cache/file.go
vendored
|
@ -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)
|
||||
|
|
8
src/cache/template.go
vendored
8
src/cache/template.go
vendored
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
36
src/maps/concurrent.go
Normal file
36
src/maps/concurrent.go
Normal file
|
@ -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
src/maps/simple.go
Normal file
11
src/maps/simple.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{
|
||||
|
|
Loading…
Reference in a new issue