mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: cache capabilities
This commit is contained in:
parent
8d35689170
commit
7d2001c936
30
src/concurrent_map.go
Normal file
30
src/concurrent_map.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import "sync"
|
||||
|
||||
type concurrentMap struct {
|
||||
values map[string]string
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func newConcurrentMap() *concurrentMap {
|
||||
return &concurrentMap{
|
||||
values: make(map[string]string),
|
||||
lock: sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *concurrentMap) set(key, value string) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.values[key] = value
|
||||
}
|
||||
|
||||
func (c *concurrentMap) get(key string) (string, bool) {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
if val, ok := c.values[key]; ok {
|
||||
return val, true
|
||||
}
|
||||
return "", false
|
||||
}
|
|
@ -48,6 +48,13 @@ type fileInfo struct {
|
|||
isDir bool
|
||||
}
|
||||
|
||||
type cache interface {
|
||||
init(home string)
|
||||
close()
|
||||
get(key string) (string, bool)
|
||||
set(key, value string)
|
||||
}
|
||||
|
||||
type environmentInfo interface {
|
||||
getenv(key string) string
|
||||
getcwd() string
|
||||
|
@ -77,6 +84,8 @@ type environmentInfo interface {
|
|||
isWsl() bool
|
||||
stackCount() int
|
||||
getTerminalWidth() (int, error)
|
||||
cache() cache
|
||||
close()
|
||||
}
|
||||
|
||||
type commandCache struct {
|
||||
|
@ -131,6 +140,7 @@ type environment struct {
|
|||
args *args
|
||||
cwd string
|
||||
cmdCache *commandCache
|
||||
fileCache *fileCache
|
||||
tracer *tracer
|
||||
}
|
||||
|
||||
|
@ -139,6 +149,8 @@ func (env *environment) init(args *args) {
|
|||
env.cmdCache = &commandCache{
|
||||
commands: newConcurrentMap(),
|
||||
}
|
||||
env.fileCache = &fileCache{}
|
||||
env.fileCache.init(env.homeDir())
|
||||
tracer := &tracer{
|
||||
debug: *args.Debug,
|
||||
}
|
||||
|
@ -461,6 +473,15 @@ func (env *environment) stackCount() int {
|
|||
return *env.args.StackCount
|
||||
}
|
||||
|
||||
func (env *environment) cache() cache {
|
||||
return env.fileCache
|
||||
}
|
||||
|
||||
func (env *environment) close() {
|
||||
env.fileCache.close()
|
||||
env.tracer.close()
|
||||
}
|
||||
|
||||
func cleanHostName(hostName string) string {
|
||||
garbage := []string{
|
||||
".lan",
|
||||
|
|
49
src/environment_cache.go
Normal file
49
src/environment_cache.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
cachePath = "/.omp.cache"
|
||||
)
|
||||
|
||||
type fileCache struct {
|
||||
cache *concurrentMap
|
||||
home string
|
||||
}
|
||||
|
||||
func (fc *fileCache) init(home string) {
|
||||
fc.cache = newConcurrentMap()
|
||||
fc.home = home
|
||||
content, err := ioutil.ReadFile(home + cachePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, line := range strings.Split(string(content), "\n") {
|
||||
if len(line) == 0 || !strings.Contains(line, "=") {
|
||||
continue
|
||||
}
|
||||
kv := strings.SplitN(line, "=", 2)
|
||||
fc.set(kv[0], kv[1])
|
||||
}
|
||||
}
|
||||
|
||||
func (fc *fileCache) close() {
|
||||
var sb strings.Builder
|
||||
for key, value := range fc.cache.values {
|
||||
cacheEntry := fmt.Sprintf("%s=%s\n", key, value)
|
||||
sb.WriteString(cacheEntry)
|
||||
}
|
||||
_ = ioutil.WriteFile(fc.home+cachePath, []byte(sb.String()), 0644)
|
||||
}
|
||||
|
||||
func (fc *fileCache) get(key string) (string, bool) {
|
||||
return fc.cache.get(key)
|
||||
}
|
||||
|
||||
func (fc *fileCache) set(key, value string) {
|
||||
fc.cache.set(key, value)
|
||||
}
|
|
@ -160,7 +160,7 @@ func main() {
|
|||
flag.Parse()
|
||||
env := &environment{}
|
||||
env.init(args)
|
||||
defer env.tracer.close()
|
||||
defer env.close()
|
||||
if *args.Millis {
|
||||
fmt.Print(time.Now().UnixNano() / 1000000)
|
||||
return
|
||||
|
|
|
@ -153,6 +153,15 @@ func (env *MockedEnvironment) getTerminalWidth() (int, error) {
|
|||
return args.Int(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) cache() cache {
|
||||
args := env.Called(nil)
|
||||
return args.Get(0).(cache)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) close() {
|
||||
_ = env.Called(nil)
|
||||
}
|
||||
|
||||
const (
|
||||
homeBill = "/home/bill"
|
||||
homeJan = "/usr/home/jan"
|
||||
|
|
Loading…
Reference in a new issue