Merge pull request #1134 from koraa/pull_req_helpers
Helpers for default variables and alias value access
Diff
1diff --git a/lib/functions.zsh b/lib/functions.zsh
2index 63ab755cf72b1094975ef017cd71727c756e1943..aaf8a03e31e838d117ee99b076c6b5ee4fd75bb1 100644
3--- a/lib/functions.zsh
4+++ b/lib/functions.zsh
5@@ -15,3 +15,61 @@ function take() {
6 cd $1
7 }
8 9+#
10+# Get the value of an alias.
11+#
12+# Arguments:
13+# 1. alias - The alias to get its value from
14+# STDOUT:
15+# The value of alias $1 (if it has one).
16+# Return value:
17+# 0 if the alias was found,
18+# 1 if it does not exist
19+#
20+function alias_value() {
21+ alias "$1" | sed "s/^$1='\(.*\)'$/\1/"
22+ test $(alias "$1")
23+}
24+
25+#
26+# Try to get the value of an alias,
27+# otherwise return the input.
28+#
29+# Arguments:
30+# 1. alias - The alias to get its value from
31+# STDOUT:
32+# The value of alias $1, or $1 if there is no alias $1.
33+# Return value:
34+# Always 0
35+#
36+function try_alias_value() {
37+ alias_value "$1" || echo "$1"
38+}
39+
40+#
41+# Set variable "$1" to default value "$2" if "$1" is not yet defined.
42+#
43+# Arguments:
44+# 1. name - The variable to set
45+# 2. val - The default value
46+# Return value:
47+# 0 if the variable exists, 3 if it was set
48+#
49+function default() {
50+ test `typeset +m "$1"` && return 0
51+ typeset -g "$1"="$2" && return 3
52+}
53+
54+#
55+# Set enviroment variable "$1" to default value "$2" if "$1" is not yet defined.
56+#
57+# Arguments:
58+# 1. name - The env variable to set
59+# 2. val - The default value
60+# Return value:
61+# 0 if the env variable exists, 3 if it was set
62+#
63+function env_default() {
64+ env | grep -q "^$1=" && return 0
65+ export "$1=$2" && return 3
66+}