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
|
isDir bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type cache interface {
|
||||||
|
init(home string)
|
||||||
|
close()
|
||||||
|
get(key string) (string, bool)
|
||||||
|
set(key, value string)
|
||||||
|
}
|
||||||
|
|
||||||
type environmentInfo interface {
|
type environmentInfo interface {
|
||||||
getenv(key string) string
|
getenv(key string) string
|
||||||
getcwd() string
|
getcwd() string
|
||||||
|
@ -77,6 +84,8 @@ type environmentInfo interface {
|
||||||
isWsl() bool
|
isWsl() bool
|
||||||
stackCount() int
|
stackCount() int
|
||||||
getTerminalWidth() (int, error)
|
getTerminalWidth() (int, error)
|
||||||
|
cache() cache
|
||||||
|
close()
|
||||||
}
|
}
|
||||||
|
|
||||||
type commandCache struct {
|
type commandCache struct {
|
||||||
|
@ -128,10 +137,11 @@ func (t *tracer) trace(start time.Time, function string, args ...string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type environment struct {
|
type environment struct {
|
||||||
args *args
|
args *args
|
||||||
cwd string
|
cwd string
|
||||||
cmdCache *commandCache
|
cmdCache *commandCache
|
||||||
tracer *tracer
|
fileCache *fileCache
|
||||||
|
tracer *tracer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *environment) init(args *args) {
|
func (env *environment) init(args *args) {
|
||||||
|
@ -139,6 +149,8 @@ func (env *environment) init(args *args) {
|
||||||
env.cmdCache = &commandCache{
|
env.cmdCache = &commandCache{
|
||||||
commands: newConcurrentMap(),
|
commands: newConcurrentMap(),
|
||||||
}
|
}
|
||||||
|
env.fileCache = &fileCache{}
|
||||||
|
env.fileCache.init(env.homeDir())
|
||||||
tracer := &tracer{
|
tracer := &tracer{
|
||||||
debug: *args.Debug,
|
debug: *args.Debug,
|
||||||
}
|
}
|
||||||
|
@ -461,6 +473,15 @@ func (env *environment) stackCount() int {
|
||||||
return *env.args.StackCount
|
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 {
|
func cleanHostName(hostName string) string {
|
||||||
garbage := []string{
|
garbage := []string{
|
||||||
".lan",
|
".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()
|
flag.Parse()
|
||||||
env := &environment{}
|
env := &environment{}
|
||||||
env.init(args)
|
env.init(args)
|
||||||
defer env.tracer.close()
|
defer env.close()
|
||||||
if *args.Millis {
|
if *args.Millis {
|
||||||
fmt.Print(time.Now().UnixNano() / 1000000)
|
fmt.Print(time.Now().UnixNano() / 1000000)
|
||||||
return
|
return
|
||||||
|
|
|
@ -153,6 +153,15 @@ func (env *MockedEnvironment) getTerminalWidth() (int, error) {
|
||||||
return args.Int(0), args.Error(1)
|
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 (
|
const (
|
||||||
homeBill = "/home/bill"
|
homeBill = "/home/bill"
|
||||||
homeJan = "/usr/home/jan"
|
homeJan = "/usr/home/jan"
|
||||||
|
|
Loading…
Reference in a new issue