Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Last active January 2, 2024 00:50
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattmc3/449430b6654aaab0ba7160e8efe8291b to your computer and use it in GitHub Desktop.
Save mattmc3/449430b6654aaab0ba7160e8efe8291b to your computer and use it in GitHub Desktop.
zsh: zstyle examples

zstyle booleans

Setup foo,bar,baz boolean vars

zstyle ':example:foo' doit yes
zstyle ':example:bar' doit no
# leave baz unset

zstyle -t succeeds if the style is defined and is 'true', 'yes', 'on', or '1' zstyle -T succeeds if the style is undefined, or is 'true', 'yes', 'on', or '1'

foo and bar aren't very interesting because we explicitly set them.

# yes = success
zstyle -t ':example:foo' doit && echo "success" || echo "fail"
zstyle -T ':example:foo' doit && echo "success" || echo "fail"

# no = fail
zstyle -t ':example:bar' doit && echo "success" || echo "fail"
zstyle -T ':example:bar' doit && echo "success" || echo "fail"

# undefined = fail
zstyle -t ':example:baz' doit && echo "success" || echo "fail"

# undefined = success
zstyle -T ':example:baz' doit && echo "success" || echo "fail"

So the short version is...

If you want to default to 'no', and have to explicitly say 'yes', then test with zstyle -t:

if zstyle -t ':example:foo' doit; then
  echo "Make it so..."
fi
if zstyle -t ':example:bar' doit; then
  echo "Never gonna happen"
fi
if zstyle -t ':example:baz' doit; then
  echo "Never gonna happen"
fi

If you want to default to 'yes', and have to explicitly say 'no', then test with zstyle -T:

if zstyle -T ':example:foo' doit; then
  echo "Make it so..."
fi
if zstyle -T ':example:bar' doit; then
  echo "Never gonna happen"
fi
if zstyle -T ':example:baz' doit; then
  echo "Make it so..."
fi
# zstyle-bool
echo "For values that can be missing or default to true, use -T"
function test_bool_zstyle_default_true {
if zstyle -T ':foo:bar' value; then
echo "bar is true or unset"
else
echo "bar is false"
fi
}
test_bool_zstyle_default_true
zstyle ':foo:bar' value yes
test_bool_zstyle_default_true
zstyle ':foo:bar' value no
test_bool_zstyle_default_true
zstyle ':foo:bar' value 1
test_bool_zstyle_default_true
zstyle ':foo:bar' value ''
test_bool_zstyle_default_true
echo "For values that can be missing or default to false, use ! -t"
function test_bool_zstyle_default_false {
if ! zstyle -t ':foo:baz' value; then
echo "baz is false or unset"
else
echo "baz is true"
fi
}
test_bool_zstyle_default_false
zstyle ':foo:baz' value yes
test_bool_zstyle_default_false
zstyle ':foo:baz' value no
test_bool_zstyle_default_false
zstyle ':foo:baz' value 1
test_bool_zstyle_default_false
zstyle ':foo:baz' value ''
test_bool_zstyle_default_false
# reference: http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fzutil-Module
# https://unix.stackexchange.com/questions/214657/what-does-zstyle-do
# list all zstyle settings
zstyle -L
# store value in zstyle
zstyle :example:favorites fruit apple
# store multiple values in zstyle
zstyle :example:list fruits banana mango pear
# retrieve from zstyle and assign new $fav variable with -g
zstyle -g fav ':example:favorites' fruit && echo $fav
# retrieve from zstyle and be explicit about the assignment data type:
# -a: array, -b: boolean, -s: string
zstyle -a :example:list fruits myfruitlist && echo $myfruitlist
# test that a zstyle value exists with -t
if zstyle -t ':example:favorites' 'fruit' 'apple'; then
echo "an apple a day keeps the dr. away"
fi
if ! zstyle -t ':example:favorites:vegtable' 'broccoli' 'no'; then
echo "Broccoli is the deadliest plant on Earth - why, it tries to warn you itself with its terrible taste"
fi
# delete a value with -d
zstyle -d ':example:favorites' 'fruit'
# list only zstyle settings for a certain pattern
zstyle -L ':example:favorites*'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment