summaryrefslogtreecommitdiff
path: root/lib/functions.zsh
diff options
context:
space:
mode:
authorRobby Russell <robby@planetargon.com>2013-12-02 23:47:21 -0800
committerRobby Russell <robby@planetargon.com>2013-12-02 23:47:21 -0800
commit1dd9c43e12403a00af9a3ac3c3602870d42cc8f3 (patch)
treed35394a1104daf351ef713f047c73d6f09bede91 /lib/functions.zsh
parentc9dd778ec07a890b71f652f92202b6a4dc9b9c27 (diff)
parent8f71efc09b42d69cc053649fade92485d282a561 (diff)
downloadzsh-1dd9c43e12403a00af9a3ac3c3602870d42cc8f3.tar.gz
zsh-1dd9c43e12403a00af9a3ac3c3602870d42cc8f3.tar.bz2
zsh-1dd9c43e12403a00af9a3ac3c3602870d42cc8f3.zip
Merge pull request #1134 from koraa/pull_req_helpers
Helpers for default variables and alias value access
Diffstat (limited to 'lib/functions.zsh')
-rw-r--r--lib/functions.zsh58
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/functions.zsh b/lib/functions.zsh
index 63ab755cf..aaf8a03e3 100644
--- a/lib/functions.zsh
+++ b/lib/functions.zsh
@@ -15,3 +15,61 @@ function take() {
cd $1
}
+#
+# Get the value of an alias.
+#
+# Arguments:
+# 1. alias - The alias to get its value from
+# STDOUT:
+# The value of alias $1 (if it has one).
+# Return value:
+# 0 if the alias was found,
+# 1 if it does not exist
+#
+function alias_value() {
+ alias "$1" | sed "s/^$1='\(.*\)'$/\1/"
+ test $(alias "$1")
+}
+
+#
+# Try to get the value of an alias,
+# otherwise return the input.
+#
+# Arguments:
+# 1. alias - The alias to get its value from
+# STDOUT:
+# The value of alias $1, or $1 if there is no alias $1.
+# Return value:
+# Always 0
+#
+function try_alias_value() {
+ alias_value "$1" || echo "$1"
+}
+
+#
+# Set variable "$1" to default value "$2" if "$1" is not yet defined.
+#
+# Arguments:
+# 1. name - The variable to set
+# 2. val - The default value
+# Return value:
+# 0 if the variable exists, 3 if it was set
+#
+function default() {
+ test `typeset +m "$1"` && return 0
+ typeset -g "$1"="$2" && return 3
+}
+
+#
+# Set enviroment variable "$1" to default value "$2" if "$1" is not yet defined.
+#
+# Arguments:
+# 1. name - The env variable to set
+# 2. val - The default value
+# Return value:
+# 0 if the env variable exists, 3 if it was set
+#
+function env_default() {
+ env | grep -q "^$1=" && return 0
+ export "$1=$2" && return 3
+}