Skip to content

Commit d45defb

Browse files
ss-opskarma
andauthored
perf(maintenance): ⚡️ Improve code logic. (#6)
* perf(maintenance): ⚡️ Improve code logic. * Update zsh-zoxide.plugin.zsh Fix: SH_GLOB Co-authored-by: pskarma <49123165+pskarma@users.noreply.github.com> Signed-off-by: Salvydas Lukosius <sall@w-ss.io> * refactor: ⚡️ Updated exit codes * Update functions/.zi-prepare-zoxide Corrects indentation. Signed-off-by: Salvydas Lukosius <sall@w-ss.io> * Update zsh-zoxide.plugin.zsh Signed-off-by: Salvydas Lukosius <sall@w-ss.io> Signed-off-by: Salvydas Lukosius <sall@w-ss.io> Co-authored-by: pskarma <49123165+pskarma@users.noreply.github.com>
1 parent 65f48c2 commit d45defb

File tree

6 files changed

+140
-42
lines changed

6 files changed

+140
-42
lines changed

.trunk/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*out
22
*logs
3+
*plugins
34
external

.trunk/trunk.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
version: 0.1
22
cli:
3-
version: 0.15.1-beta
3+
version: 0.16.1-beta
44
lint:
55
enabled:
66
- actionlint@1.6.15
77
- git-diff-check@SYSTEM
8-
- gitleaks@8.9.0
8+
- gitleaks@8.10.3
99
- markdownlint@0.32.1
1010
- prettier@2.7.1

docs/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ zi light ajeetdsouza/zoxide
2626
2727
### Install zsh-zoxide
2828

29-
#### Standart syntax
29+
#### Standard syntax
3030

3131
```zsh
3232
zi ice has'zoxide'
@@ -67,7 +67,7 @@ x foo / # cd into a subdirectory starting with foo
6767
x ~/foo # x also works like a regular cd command
6868
x foo/ # cd into relative path
6969
x .. # cd one level up
70-
x - # cd into previous directory
70+
x - # cd into the previous directory
7171
```
7272

7373
```sh
@@ -80,4 +80,4 @@ x foo<SPACE><TAB> # show interactive completions
8080

8181
### Environment variables and usage with other plugin managers
8282

83-
The plugin will call `zoxide init` with prefixed commands `z`, `zi`.
83+
The plugin will call `zoxide init` with prefixed commands `z`, and `zi`.

functions/.zi-prepare-zoxide

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
#autoload
22
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
33
# vim: ft=zsh sw=2 ts=2 et
4-
4+
#
5+
# Return 101 if directory failed to be created.
6+
# Return 201 failed to copy files.
7+
#
8+
# Zsh Plugin Standard
9+
# https://wiki.zshell.dev/community/zsh_plugin_standard#standard-recommended-options
510
builtin emulate -L zsh ${=${options[xtrace]:#off}:+-o xtrace}
611
builtin setopt extended_glob warn_create_global \
712
typeset_silent no_short_loops rc_quotes no_auto_pushd
813

9-
# - The directory in which the database is stored.
10-
# - Manpages copied to set / default localtion.
11-
if (( ${ZI[SOURCED]} )) && [[ -d ${ZPFX} ]]; then
12-
if [[ ! -d ${ZPFX}/share ]]; then
13-
command mkdir -p ${ZPFX}/share || \
14-
+zi-message "{error}Failed to create directory for database.{rst}"
15-
fi
16-
typeset -g _ZO_DATA_DIR=${ZPFX}/share
17-
if [[ -d ${ZI[MAN_DIR]} ]] && [[ -d ${Plugins[ZSH_ZOXIDE]}/man/man1 ]]; then
18-
cp ${Plugins[ZSH_ZOXIDE]}/man/man1/* ${ZI[MAN_DIR]}/man1/ || \
14+
integer ret=0
15+
16+
# The directory in which the database is stored.
17+
if [[ ! -d ${ZPFX}/share ]]; then
18+
# Create directory if it doesn't exist, else show error.
19+
command mkdir -p "${ZPFX}"/share || {
20+
print "Failed to create directory ${ZPFX}/share"
21+
ret=101
22+
}
23+
fi
24+
25+
# Manpages are copied to set / default location.
26+
if [[ -d ${ZI[MAN_DIR]} ]] && [[ -d ${Plugins[ZSH_ZOXIDE]:?}/man/man1 ]]; then
27+
# Copy manpages if existing, else show error
28+
command cp "${Plugins[ZSH_ZOXIDE]}"/man/man1/* "${ZI[MAN_DIR]}"/man1/ || {
1929
+zi-message "{error}Failed to install manpages.{rst}"
20-
fi
30+
ret=201
31+
}
2132
fi
2233

2334
# Set zoxide commands x, xi when using with Zi.
24-
if (( ${ZI[SOURCED]} )) && \
25-
(( ${+functions[zi]} )) && [[ -z ${_ZO_CMD_PREFIX} ]]; then
26-
typeset -g _ZO_CMD_PREFIX=x
27-
fi
35+
[[ -z $_ZO_CMD_PREFIX ]] && typeset -g _ZO_CMD_PREFIX=x
36+
37+
return $ret

functions/.zsh-prepare-zoxide

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#autoload
2+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
3+
# vim: ft=zsh sw=2 ts=2 et
4+
#
5+
# Return 100 if missing dependencies.
6+
# Return 110 for incorrect dependencies version.
7+
# Return 102 if git submodule failed to be initialized.
8+
#
9+
# Zsh Plugin Standard
10+
# https://wiki.zshell.dev/community/zsh_plugin_standard#standard-recommended-options
11+
builtin emulate -L zsh ${=${options[xtrace]:#off}:+-o xtrace}
12+
builtin setopt extended_glob warn_create_global typeset_silent \
13+
no_short_loops rc_quotes no_auto_pushd
14+
15+
integer ret=0
16+
17+
# Check dependencies
18+
typeset -a dependencies
19+
typeset dependency
20+
21+
dependencies=( "git" "zoxide" )
22+
for dependency in "${dependencies[@]}"; do
23+
(( ${+commands[$dependency]} )) || {
24+
print "Missing dependency: $dependency"
25+
ret=100
26+
}
27+
done
28+
29+
# Check if fzf has minimal required version.
30+
if (( ${+commands[fzf]} )); then
31+
typeset fzf_version
32+
fzf_version=$(fzf --version | awk -F '.' '{print $2}')
33+
if [[ $fzf_version -lt 21 ]]; then
34+
print "The fzf version is too old. Please update to version 0.21.0 or higher."
35+
print "Note: zoxide only supports fzf v0.21.0 and above."
36+
ret=110
37+
fi
38+
fi
39+
40+
# Initialize git submodule if required.
41+
if [[ ! -f ${Plugins[ZSH_ZOXIDE]:?}/man/man1/zoxide-add.1 ]]; then
42+
command git submodule --quiet init
43+
command git submodule --quiet update
44+
if [[ ! -f ${Plugins[ZSH_ZOXIDE]}/man/man1/zoxide.1 ]]; then
45+
print "Failed to initiate git submodules"
46+
ret=102
47+
fi
48+
fi
49+
50+
return $ret

zsh-zoxide.plugin.zsh

100644100755
Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#
44
# Zsh Plugin Standard
55
# https://wiki.zshell.dev/community/zsh_plugin_standard#zero-handling
6-
0="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}"
7-
0="${${(M)0:#/*}:-$PWD/$0}"
6+
0="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}"
7+
0="${${(M)0:#/*}:-$PWD/$0}"
88

99
# https://wiki.zshell.dev/community/zsh_plugin_standard#standard-plugins-hash
1010
typeset -gA Plugins
@@ -15,23 +15,47 @@ if [[ $PMSPEC != *f* ]]; then
1515
fpath+=( "${0:h}/functions" )
1616
fi
1717

18+
# https://wiki.zshell.dev/community/zsh_plugin_standard#global-parameter-with-capabilities
19+
if [[ $PMSPEC == *P* ]]; then
20+
_ZO_DATA_DIR=${ZPFX}/share
21+
fi
22+
1823
# Autoload functions
19-
autoload -Uz "${0:h}/functions"/.*(.:t)
20-
21-
if [[ ! -f ${Plugins[ZSH_ZOXIDE]}/man/man1/zoxide.1 ]]; then
22-
command git submodule --quiet init
23-
command git submodule --quiet update
24-
if [[ ! -f ${Plugins[ZSH_ZOXIDE]}/man/man1/zoxide-add.1 ]]; then
25-
print "Failed to initiate git submodules"
26-
return 1
24+
autoload -Uz .{zi,zsh}-prepare-zoxide
25+
26+
# TODO: Investigate variables and functions.
27+
# Unset variables and functions which is not required after initialization.
28+
29+
# Check and prepare zsh-zoxide.
30+
# Used only once when zsh-zoxide is installed, or then
31+
# Plugins[ZSH_ZOXIDE_READY] reset to 0 to prevent full re-initialization
32+
if (( ! Plugins[ZSH_ZOXIDE_READY] )); then
33+
# Set zoxide as ready to initiate.
34+
Plugins[ZSH_ZOXIDE_READY]=1
35+
# Returns 100 if missing dependencies.
36+
# Returns 110 for incorrect dependencies version.
37+
# Returns 102 if git submodule failed to be initialized.
38+
.zsh-prepare-zoxide
39+
exit_code=$?
40+
if (( exit_code )); then
41+
print "Failed to prepare zoxide, exit code: $exit_code"
42+
return $exit_code
43+
fi
44+
# Prepare for Zi.
45+
if (( ZI[SOURCED] )) && [[ -d $ZPFX ]]; then
46+
# Returns 101 if directory failed to be created.
47+
# Returns 201 failed to copy files.
48+
.zi-prepare-zoxide
49+
exit_code=$?
50+
if (( exit_code )); then
51+
print "Failed to prepare Zi, exit code: $exit_code"
52+
return $exit_code
53+
fi
2754
fi
2855
fi
2956

30-
# When using Zi:
31-
.zi-prepare-zoxide
32-
33-
# TODO: Env variables
34-
# If not set for Zi use default or user prefered.
57+
# TODO: Investigate variables for potential use.
58+
# Set variable to preferred prefix.
3559
: ${_ZO_CMD_PREFIX:=$_ZO_CMD_PREFIX}
3660
# Directory in which the database is stored.
3761
: ${_ZO_DATA_DIR:=$_ZO_DATA_DIR}
@@ -46,14 +70,27 @@ fi
4670
# When set to 1, x will resolve symlinks before adding directories to the database.
4771
# _ZO_RESOLVE_SYMLINKS
4872

49-
# Expand any tilde in the path.
50-
export _ZO_DATA_DIR=${~_ZO_DATA_DIR}
51-
52-
# TODO: Output failures
53-
if (( $+commands[zoxide] )); then
73+
if (( ${+commands[zoxide]} )); then
74+
if [[ -n $_ZO_DATA_DIR ]]; then
75+
# If a parameter specified does not already exist, it is created in the global scope,
76+
# The variable is set to the absolute path of the directory.
77+
typeset -gx _ZO_DATA_DIR=${~_ZO_DATA_DIR}
78+
fi
79+
# TODO: Check zoxide exit codes for possible improvements.
5480
if [[ $_ZO_CMD_PREFIX =~ ^[a-zA-Z]*$ ]]; then
81+
# Set zoxide commands x, xi when using with Zi.
5582
eval "$(zoxide init zsh --cmd $_ZO_CMD_PREFIX)"
56-
else
83+
exit_code=$?
84+
elif (( ! _ZO_CMD_PREFIX )); then
5785
eval "$(zoxide init zsh)"
86+
exit_code=$?
87+
fi
88+
if (( exit_code )); then
89+
print "Failed to initialize zoxide, exit code: $exit_code"
90+
return $exit_code
5891
fi
92+
else
93+
print "Please install zoxide or make sure it is in your PATH"
94+
print "More info: https://github.com/ajeetdsouza/zoxide#installation"
95+
exit 1
5996
fi

0 commit comments

Comments
 (0)