mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-28 20:39:40 -08:00
feat(cli): add cache
This commit is contained in:
parent
255c9baebf
commit
4f50517784
|
@ -12,7 +12,7 @@ If you have no idea which shell you're currently using, Oh My Posh has a utility
|
||||||
:::
|
:::
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
oh-my-posh config get shell
|
oh-my-posh get shell
|
||||||
```
|
```
|
||||||
|
|
||||||
<Tabs
|
<Tabs
|
||||||
|
|
53
src/cli/cache.go
Normal file
53
src/cli/cache.go
Normal 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)
|
||||||
|
}
|
|
@ -15,17 +15,15 @@ import (
|
||||||
// getCmd represents the get command
|
// getCmd represents the get command
|
||||||
var getCmd = &cobra.Command{
|
var getCmd = &cobra.Command{
|
||||||
Use: "get [shell|cache-path|millies]",
|
Use: "get [shell|cache-path|millies]",
|
||||||
Short: "Get a value from the oh-my-posh configuration",
|
Short: "Get a value from oh-my-posh",
|
||||||
Long: `Get a value from the oh-my-posh configuration.
|
Long: `Get a value from oh-my-posh.
|
||||||
This command is used to get the value of the following variables:
|
This command is used to get the value of the following variables:
|
||||||
|
|
||||||
- shell
|
- shell
|
||||||
- cache-path
|
|
||||||
- millis`,
|
- millis`,
|
||||||
ValidArgs: []string{
|
ValidArgs: []string{
|
||||||
"millis",
|
"millis",
|
||||||
"shell",
|
"shell",
|
||||||
"cache-path",
|
|
||||||
},
|
},
|
||||||
Args: cobra.OnlyValidArgs,
|
Args: cobra.OnlyValidArgs,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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)
|
fmt.Print(time.Now().UnixNano() / 1000000)
|
||||||
case "shell":
|
case "shell":
|
||||||
fmt.Println(env.Shell())
|
fmt.Println(env.Shell())
|
||||||
case "cache-path":
|
// case "cache-path":
|
||||||
fmt.Print(env.CachePath())
|
// fmt.Print(env.CachePath())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { // nolint:gochecknoinits
|
func init() { // nolint:gochecknoinits
|
||||||
configCmd.AddCommand(getCmd)
|
rootCmd.AddCommand(getCmd)
|
||||||
}
|
}
|
|
@ -10,7 +10,7 @@ if [[ ! -d "/tmp" ]]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# start timer on command start
|
# start timer on command start
|
||||||
PS0='$(::OMP:: config get millis > "$TIMER_START")'
|
PS0='$(::OMP:: get millis > "$TIMER_START")'
|
||||||
# set secondary prompt
|
# set secondary prompt
|
||||||
PS2="$(::OMP:: prompt print secondary --config="$POSH_THEME" --shell=bash)"
|
PS2="$(::OMP:: prompt print secondary --config="$POSH_THEME" --shell=bash)"
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ function _omp_hook() {
|
||||||
omp_stack_count=$((${#DIRSTACK[@]} - 1))
|
omp_stack_count=$((${#DIRSTACK[@]} - 1))
|
||||||
omp_elapsed=-1
|
omp_elapsed=-1
|
||||||
if [[ -f "$TIMER_START" ]]; then
|
if [[ -f "$TIMER_START" ]]; then
|
||||||
omp_now=$(::OMP:: config get millis)
|
omp_now=$(::OMP:: get millis)
|
||||||
omp_start_time=$(cat "$TIMER_START")
|
omp_start_time=$(cat "$TIMER_START")
|
||||||
omp_elapsed=$((omp_now-omp_start_time))
|
omp_elapsed=$((omp_now-omp_start_time))
|
||||||
rm -f "$TIMER_START"
|
rm -f "$TIMER_START"
|
||||||
|
|
|
@ -37,7 +37,7 @@ local function os_clock_millis()
|
||||||
if (clink.version_encoded or 0) >= 10020030 then
|
if (clink.version_encoded or 0) >= 10020030 then
|
||||||
return math.floor(os.clock() * 1000)
|
return math.floor(os.clock() * 1000)
|
||||||
else
|
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)
|
return run_posh_command(prompt_exe)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,7 @@ export CONDA_PROMPT_MODIFIER=false
|
||||||
PS2="$(::OMP:: prompt print secondary --config="$POSH_THEME" --shell=zsh)"
|
PS2="$(::OMP:: prompt print secondary --config="$POSH_THEME" --shell=zsh)"
|
||||||
|
|
||||||
function prompt_ohmyposh_preexec() {
|
function prompt_ohmyposh_preexec() {
|
||||||
omp_start_time=$(::OMP:: config get millis)
|
omp_start_time=$(::OMP:: get millis)
|
||||||
}
|
}
|
||||||
|
|
||||||
function prompt_ohmyposh_precmd() {
|
function prompt_ohmyposh_precmd() {
|
||||||
|
@ -14,7 +14,7 @@ function prompt_ohmyposh_precmd() {
|
||||||
omp_stack_count=${#dirstack[@]}
|
omp_stack_count=${#dirstack[@]}
|
||||||
omp_elapsed=-1
|
omp_elapsed=-1
|
||||||
if [ $omp_start_time ]; then
|
if [ $omp_start_time ]; then
|
||||||
omp_now=$(::OMP:: config get millis)
|
omp_now=$(::OMP:: get millis)
|
||||||
omp_elapsed=$(($omp_now-$omp_start_time))
|
omp_elapsed=$(($omp_now-$omp_start_time))
|
||||||
fi
|
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)"
|
eval "$(::OMP:: prompt print primary --config="$POSH_THEME" --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --eval --shell=zsh)"
|
||||||
|
|
|
@ -3,11 +3,12 @@ package environment
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
fileName = "/omp.cache"
|
CacheFile = "/omp.cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cacheObject struct {
|
type cacheObject struct {
|
||||||
|
@ -25,7 +26,8 @@ type fileCache struct {
|
||||||
func (fc *fileCache) Init(cachePath string) {
|
func (fc *fileCache) Init(cachePath string) {
|
||||||
fc.cache = newConcurrentMap()
|
fc.cache = newConcurrentMap()
|
||||||
fc.cachePath = cachePath
|
fc.cachePath = cachePath
|
||||||
content, err := ioutil.ReadFile(fc.cachePath + fileName)
|
cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
|
||||||
|
content, err := ioutil.ReadFile(cacheFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -46,7 +48,8 @@ func (fc *fileCache) Close() {
|
||||||
}
|
}
|
||||||
cache := fc.cache.list()
|
cache := fc.cache.list()
|
||||||
if dump, err := json.MarshalIndent(cache, "", " "); err == nil {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue