agkozak / zsh-z

Jump quickly to directories that you have visited "frecently." A native Zsh port of z.sh with added features.
MIT License
2.02k stars 76 forks source link

Declare ZSHZ_EXCLUDE_DIRS so that it can be appended to #44

Closed zachriggle closed 3 years ago

zachriggle commented 3 years ago

I have conditionals and multiple scripts (in multiple places) where I want to add entries to ZSHZ_EXCLUDE_DIRS.

The main script should:

declare -ga ZSHZ_EXCLUDE_DIRS=( )

So that these scripts can all just append to the array, rather than having to rely on one of them to create the array, and the others to append to it, e.g.:

ZSHZ_EXCLUDE_DIRS+=( foo bar )

So that other scripts don't need to check for its existence.

if [ -z "${ZSHZ_EXCLUDE_DIRS[*]}" ]; then
  declare -ga ZSHZ_EXCLUDE_DIRS
fi

ZSHZ_EXCLUDE_DIRS+=( fizz buzz )
agkozak commented 3 years ago

This is a great idea. Many thanks. I'll add the code in just a few days - I'm finishing up a huge project today and tomorrow.

agkozak commented 3 years ago

I've made the addition on the exclude-dirs branch; make sure it does what you want, and I'll merge it to master. Thanks again.

zachriggle commented 3 years ago

The only thing that I might add is to add -U to the typeset in order to keep the entries unique. Multiple scripts might add the same directory and this just keeps them unique.

The Zsh docs say:

-U For arrays (but not for associative arrays), keep only the first occurrence of each duplicated value. This may also be set for tied parameters (see -T) or colon-separated special parameters like PATH or FIGNORE, etc. Note the flag takes effect on assignment, and the type of the variable being assigned to is determinative; for variables with shared values it is therefore recommended to set the flag for all interfaces, e.g. ‘typeset -U PATH path’.

But otherwise this looks fine to me!

diff --git a/zsh-z.plugin.zsh b/zsh-z.plugin.zsh
index 77c10d4..57ebec4 100644
--- a/zsh-z.plugin.zsh
+++ b/zsh-z.plugin.zsh
@@ -108,6 +108,10 @@ With no ARGUMENT, list the directory history in ascending rank.
 # Global associative array for internal use
 typeset -gA ZSHZ

+# Make sure ZSHZ_EXCLUDE_DIRS has been declared so that other scripts can
+# simply append to it
+(( ${+ZSHZ_EXCLUDE_DIRS} )) || typeset -ga ZSHZ_EXCLUDE_DIRS
+
 # Determine if zsystem flock is available
 zsystem supports flock &> /dev/null && ZSHZ[USE_FLOCK]=1
agkozak commented 3 years ago

@zachriggle Thanks for the great suggestion! I'm merged this improvement to the master branch.