agkozak/zsh-z

Declare ZSHZ_EXCLUDE_DIRS so that it can be appended to

zachriggle opened this issue · 4 comments

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 )

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.

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.

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

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