From 4f50517784b1ce283debe1393abc0b26b2460eb8 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 19 Mar 2022 19:32:40 +0100 Subject: [PATCH] feat(cli): add cache --- docs/docs/install-prompt.mdx | 2 +- src/cli/cache.go | 53 +++++++++++++++++++++++++++++++ src/cli/{config_get.go => get.go} | 12 +++---- src/engine/init/omp.bash | 4 +-- src/engine/init/omp.lua | 2 +- src/engine/init/omp.zsh | 4 +-- src/environment/cache.go | 9 ++++-- 7 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 src/cli/cache.go rename src/cli/{config_get.go => get.go} (77%) diff --git a/docs/docs/install-prompt.mdx b/docs/docs/install-prompt.mdx index aed3eb9e..d327ac3c 100644 --- a/docs/docs/install-prompt.mdx +++ b/docs/docs/install-prompt.mdx @@ -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 ``` + +*/ +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) +} diff --git a/src/cli/config_get.go b/src/cli/get.go similarity index 77% rename from src/cli/config_get.go rename to src/cli/get.go index c7608265..d30dde7c 100644 --- a/src/cli/config_get.go +++ b/src/cli/get.go @@ -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) } diff --git a/src/engine/init/omp.bash b/src/engine/init/omp.bash index 99452759..05c1098c 100644 --- a/src/engine/init/omp.bash +++ b/src/engine/init/omp.bash @@ -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" diff --git a/src/engine/init/omp.lua b/src/engine/init/omp.lua index 56810ac4..580d4fbd 100644 --- a/src/engine/init/omp.lua +++ b/src/engine/init/omp.lua @@ -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 diff --git a/src/engine/init/omp.zsh b/src/engine/init/omp.zsh index 1f2d468b..57d15e0d 100644 --- a/src/engine/init/omp.zsh +++ b/src/engine/init/omp.zsh @@ -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)" diff --git a/src/environment/cache.go b/src/environment/cache.go index e953e750..ec862568 100644 --- a/src/environment/cache.go +++ b/src/environment/cache.go @@ -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) } }