Add `grml_theme_has_token' guard function
authorFrank Terbeck <ft@grml.org>
Wed, 28 Aug 2013 08:46:34 +0000 (10:46 +0200)
committerFrank Terbeck <ft@grml.org>
Wed, 28 Aug 2013 08:46:34 +0000 (10:46 +0200)
Signed-off-by: Frank Terbeck <ft@grml.org>
etc/zsh/zshrc

index 415ec43..6a6f1e6 100644 (file)
@@ -1906,6 +1906,14 @@ grml_prompt_token_default=(
     vcs               '0'
 )
 
+function grml_theme_has_token () {
+    if (( ARGC != 1 )); then
+        printf 'usage: grml_theme_has_token <name>\n'
+        return 1
+    fi
+    (( ${+grml_prompt_token_default[$1]} ))
+}
+
 function GRML_theme_add_token_usage () {
     cat <<__EOF__
   Usage: grml_theme_add_token <name> [-f|-i] <token/function> [<pre> <post>]
@@ -1927,6 +1935,11 @@ function GRML_theme_add_token_usage () {
     return value is expected in the \$REPLY parameter. The use of these
     options is mutually exclusive.
 
+    There is a utility function \`grml_theme_has_token', which you can use
+    to test if a token exists before trying to add it. This can be a guard
+    for situations in which a \`grml_theme_add_token' call may happen more
+    than once.
+
   Example:
 
     To add a new token \`day' that expands to the current weekday in the
@@ -1990,7 +2003,7 @@ grml_theme_add_token: <pre> and <post> need to by specified _both_!\n\n'
         shift 2
     fi
 
-    if (( ${+grml_prompt_token_default[$name]} )); then
+    if grml_theme_has_token $name; then
         printf '
 grml_theme_add_token: Token `%s'\'' exists! Giving up!\n\n' $name
         GRML_theme_add_token_usage