Skip to content

Commit

Permalink
fix/issue/#37 (#38)
Browse files Browse the repository at this point in the history
correctly assign parameters

Signed-off-by: Sall <[email protected]>
  • Loading branch information
ss-o authored Mar 18, 2023
1 parent 94fbe40 commit 627e420
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
10 changes: 10 additions & 0 deletions .trunk/configs/.yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules:
quoted-strings:
required: only-when-needed
extra-allowed: ["{|}"]
empty-values:
forbid-in-block-mappings: true
forbid-in-flow-mappings: true
key-duplicates: {}
octal-values:
forbid-implicit-octal: true
11 changes: 7 additions & 4 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
version: 0.1
cli:
version: 1.5.1
version: 1.6.1
plugins:
sources:
- id: trunk
ref: v0.0.12
ref: v0.0.13
uri: https://github.com/trunk-io/plugins
lint:
disabled:
- yamllint
enabled:
- [email protected]
- git-diff-check
- [email protected]
- [email protected]
- gitleaks@8.15.3
- gitleaks@8.16.1
runtimes:
enabled:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
actions:
enabled:
Expand Down
29 changes: 22 additions & 7 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@

## The <samp>[`ajeetdsouza/zoxide`](https://github.com/ajeetdsouza/zoxide)</samp>

### Options
### Environment variables

Export the following environment variables to change the default behavior:
| Variable | Description | Default |
| -------------------------- | ----------------------------------------- | --------------------------------------- |
| <kbd>\_ZO_CMD_PREFIX</kbd> | Set variable to preferred prefix | Zi: <kbd>x</kbd>, other: <kbd>z</kbd> |
| <kbd>\_ZO_DATA_DIR</kbd> | Directory in which the database is stored | Zi: <kbd>$ZPFX/share</kbd>, other: none |

| Variable | Description | Default |
| ----------------------- | ------------------------------------------------------- | --------------- |
| <kbd>ZEC_DISABLED</kbd> | Disable eval-caching | <kbd>0</kbd> |
| <kbd>ZEC_DEBUG</kbd> | Enable debug mode for eval-caching | <kbd>0</kbd> |
| <kbd>ZEC_MAX</kbd> | Maximum number to load from cache (until force refresh) | <kbd>1000</kbd> |
All environment variables: [ajeetdsouza/zoxide#environment-variables](https://github.com/ajeetdsouza/zoxide#environment-variables)

#### Eval-cache options

Before setting the environment variables, you must declare the associative array:

```shell
typeset -A ZEC
```

Set the following environment variables to change the default behavior:

| Variable | Description | Default |
| ------------------------ | ------------------------------------------------------- | --------------- |
| <kbd>ZEC[DISABLED]</kbd> | Disable eval-caching | <kbd>0</kbd> |
| <kbd>ZEC[DEBUG]</kbd> | Enable debug mode for eval-caching | <kbd>0</kbd> |
| <kbd>ZEC[MAX]</kbd> | Maximum number to load from cache (until force refresh) | <kbd>1000</kbd> |

> Eval-cache files are stored in <kbd><samp>${Plugins[ZSH_ZOXIDE]}/.\_zoxide/\*</samp></kbd> directory.
Expand Down
59 changes: 34 additions & 25 deletions functions/@zsh-eval-cache
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,58 @@
# vim: ft=zsh sw=2 ts=2 et

builtin emulate -L zsh ${=${options[xtrace]:#off}:+-o xtrace}
builtin setopt extended_glob warn_create_global typeset_silent no_short_loops rc_quotes no_auto_pushd
builtin setopt extended_glob warn_create_global typeset_silent \
no_short_loops rc_quotes no_auto_pushd local_options

local -a input=( $@ ) chpwd_functions; integer count ret=0
local md5_hash zoxide_cache zoxide_cache_dir __zoxide_z_prefix
local counter_file max=${ZEC_MAX:-1000}
typeset -gA ZEC
typeset -i count=0 ret=0
typeset -a input_array=( "$@" )
typeset -ga chpwd_functions __zoxide_z_prefix

md5_hash="no_hash"
zoxide_cache_dir="${Plugins[ZSH_ZOXIDE]}/._zoxide"
: ${ZEC[CACHE_DIR]:=${ZEC[CACHE_DIR]:-${Plugins[ZSH_ZOXIDE]}/._zoxide}}
: ${ZEC[MD5_HASH]:=${ZEC[MD5_HASH]:-no_hash}}
: ${ZEC[DISABLED]:=${ZEC[DISABLED]:-0}}
: ${ZEC[DEBUG]:=${ZEC[DEBUG]:-0}}
: ${ZEC[MAX]:=${ZEC[MAX]:-1000}}

[[ -d $zoxide_cache_dir ]] || command mkdir -p $zoxide_cache_dir &> /dev/null
ZEC[CACHE_DIR]=${~ZEC[CACHE_DIR]}

: ${ZEC[CACHE]:=${ZEC[CACHE]:-${ZEC[CACHE_DIR]}/${ZEC[MD5_HASH]}.zsh}}
: ${ZEC[COUNT]:=${ZEC[COUNT]:-${ZEC[CACHE_DIR]}/${ZEC[MD5_HASH]}.txt}}

[[ -d $ZEC[CACHE_DIR] ]] || command mkdir -p $ZEC[CACHE_DIR] &> /dev/null

if (( $+commands[md5] )); then
md5_hash=$(md5 <<< $input)
ZEC[MD5_HASH]=$(md5 <<< $input_array)
elif (( $+commands[md5sum] )); then
md5_hash=$(md5sum <<< $input | awk '{ print $1 }')
ZEC[MD5_HASH]=$(md5sum <<< $input_array | awk '{ print $1 }')
else
print "@zsh-eval-cache: md5 program not found"; return 1
fi

zoxide_cache="${zoxide_cache_dir}/${md5_hash}.zsh"
counter_file="${zoxide_cache_dir}/${md5_hash}.txt"

if (( ZEC_DISABLED )); then
(( ZEC_DEBUG )) && builtin print -Pr "@zsh-eval-cache: disabled"
eval "$( $input )"; ret=$?
elif [[ -s $zoxide_cache ]]; then
[[ -f $counter_file ]] || builtin print 1 > $counter_file 2> /dev/null
if (( ZEC[DISABLED] )); then
(( ZEC[DEBUG] )) && builtin print -Pr "@zsh-eval-cache: disabled"
eval "$( ${(@)input_array} )"; ret=$?
elif [[ -s $ZEC[CACHE] ]]; then
[[ -f $ZEC[COUNT] ]] || builtin print 1 > $ZEC[COUNT] 2> /dev/null

count=$(<$counter_file) && (( count++ ))
builtin print $count > $counter_file 2> /dev/null
count=$(<$ZEC[COUNT]) && (( count++ ))
builtin print $count > $ZEC[COUNT] 2> /dev/null

(( ZEC_DEBUG )) && builtin print -Pr "@zsh-eval-cache: hit $count"
(( ZEC[DEBUG] )) && builtin print -Pr "@zsh-eval-cache: hit $count"

builtin source $zoxide_cache; ret=$?
builtin source $ZEC[CACHE]; ret=$?
else
[[ -f $counter_file ]] || builtin print 1 > $counter_file 2> /dev/null
[[ -f $ZEC[COUNT] ]] || builtin print 1 > $ZEC[COUNT] 2> /dev/null

(( ZEC_DEBUG )) && builtin print -Pr "@zsh-eval-cache: miss"
(( ZEC[DEBUG] )) && builtin print -Pr "@zsh-eval-cache: miss"

$input > $zoxide_cache && zcompile -U $zoxide_cache && builtin source $zoxide_cache; ret=$?
${(@)input_array} >! $ZEC[CACHE] && zcompile -U $ZEC[CACHE] && builtin source $ZEC[CACHE]; ret=$?
fi

(( count > max )) && command rm -rf ${zoxide_cache_dir} &> /dev/null
(( count > ZEC[MAX] )) && command rm -rf $ZEC[CACHE_DIR] &> /dev/null

(( ret )) && builtin print -Pr "@zsh-eval-cache: failed to initialize zoxide" >&2

(( ret )) && builtin print -Pr "@zsh-eval-cache: failed to initialize zoxide" >&2

Expand Down

0 comments on commit 627e420

Please sign in to comment.