feat(cli): add cache

This commit is contained in:
Jan De Dobbeleer 2022-03-19 19:32:40 +01:00 committed by Jan De Dobbeleer
parent 255c9baebf
commit 4f50517784
7 changed files with 70 additions and 16 deletions

View file

@ -12,7 +12,7 @@ If you have no idea which shell you're currently using, Oh My Posh has a utility
:::
```bash
oh-my-posh config get shell
oh-my-posh get shell
```
<Tabs

53
src/cli/cache.go Normal file
View file

@ -0,0 +1,53 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cli
import (
"fmt"
"oh-my-posh/environment"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
// getCmd represents the get command
var getCache = &cobra.Command{
Use: "cache [path|clear]",
Short: "Interact with the oh-my-posh cache",
Long: `Interact with the oh-my-posh cache.
You can do the following:
- path: list the cache path
- clear: remove all cache values`,
ValidArgs: []string{
"path",
"clear",
},
Args: cobra.OnlyValidArgs,
Run: func(cmd *cobra.Command, args []string) {
env := &environment.ShellEnvironment{
Version: cliVersion,
}
env.Init(false)
defer env.Close()
switch args[0] {
case "path":
fmt.Print(env.CachePath())
case "clear":
cacheFilePath := filepath.Join(env.CachePath(), environment.CacheFile)
err := os.Remove(cacheFilePath)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("removed cache file at %s\n", cacheFilePath)
}
},
}
func init() { // nolint:gochecknoinits
rootCmd.AddCommand(getCache)
}

View file

@ -15,17 +15,15 @@ import (
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get [shell|cache-path|millies]",
Short: "Get a value from the oh-my-posh configuration",
Long: `Get a value from the oh-my-posh configuration.
Short: "Get a value from oh-my-posh",
Long: `Get a value from oh-my-posh.
This command is used to get the value of the following variables:
- shell
- cache-path
- millis`,
ValidArgs: []string{
"millis",
"shell",
"cache-path",
},
Args: cobra.OnlyValidArgs,
Run: func(cmd *cobra.Command, args []string) {
@ -39,12 +37,12 @@ This command is used to get the value of the following variables:
fmt.Print(time.Now().UnixNano() / 1000000)
case "shell":
fmt.Println(env.Shell())
case "cache-path":
fmt.Print(env.CachePath())
// case "cache-path":
// fmt.Print(env.CachePath())
}
},
}
func init() { // nolint:gochecknoinits
configCmd.AddCommand(getCmd)
rootCmd.AddCommand(getCmd)
}

View file

@ -10,7 +10,7 @@ if [[ ! -d "/tmp" ]]; then
fi
# start timer on command start
PS0='$(::OMP:: config get millis > "$TIMER_START")'
PS0='$(::OMP:: get millis > "$TIMER_START")'
# set secondary prompt
PS2="$(::OMP:: prompt print secondary --config="$POSH_THEME" --shell=bash)"
@ -20,7 +20,7 @@ function _omp_hook() {
omp_stack_count=$((${#DIRSTACK[@]} - 1))
omp_elapsed=-1
if [[ -f "$TIMER_START" ]]; then
omp_now=$(::OMP:: config get millis)
omp_now=$(::OMP:: get millis)
omp_start_time=$(cat "$TIMER_START")
omp_elapsed=$((omp_now-omp_start_time))
rm -f "$TIMER_START"

View file

@ -37,7 +37,7 @@ local function os_clock_millis()
if (clink.version_encoded or 0) >= 10020030 then
return math.floor(os.clock() * 1000)
else
local prompt_exe = string.format('%s config get millis', omp_exe())
local prompt_exe = string.format('%s get millis', omp_exe())
return run_posh_command(prompt_exe)
end
end

View file

@ -6,7 +6,7 @@ export CONDA_PROMPT_MODIFIER=false
PS2="$(::OMP:: prompt print secondary --config="$POSH_THEME" --shell=zsh)"
function prompt_ohmyposh_preexec() {
omp_start_time=$(::OMP:: config get millis)
omp_start_time=$(::OMP:: get millis)
}
function prompt_ohmyposh_precmd() {
@ -14,7 +14,7 @@ function prompt_ohmyposh_precmd() {
omp_stack_count=${#dirstack[@]}
omp_elapsed=-1
if [ $omp_start_time ]; then
omp_now=$(::OMP:: config get millis)
omp_now=$(::OMP:: get millis)
omp_elapsed=$(($omp_now-$omp_start_time))
fi
eval "$(::OMP:: prompt print primary --config="$POSH_THEME" --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --eval --shell=zsh)"

View file

@ -3,11 +3,12 @@ package environment
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"time"
)
const (
fileName = "/omp.cache"
CacheFile = "/omp.cache"
)
type cacheObject struct {
@ -25,7 +26,8 @@ type fileCache struct {
func (fc *fileCache) Init(cachePath string) {
fc.cache = newConcurrentMap()
fc.cachePath = cachePath
content, err := ioutil.ReadFile(fc.cachePath + fileName)
cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
content, err := ioutil.ReadFile(cacheFilePath)
if err != nil {
return
}
@ -46,7 +48,8 @@ func (fc *fileCache) Close() {
}
cache := fc.cache.list()
if dump, err := json.MarshalIndent(cache, "", " "); err == nil {
_ = ioutil.WriteFile(fc.cachePath+fileName, dump, 0644)
cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
_ = ioutil.WriteFile(cacheFilePath, dump, 0644)
}
}