From dbef8b1a92f789f4109435a3b7278f899e328767 Mon Sep 17 00:00:00 2001 From: mapc Date: Sat, 5 May 2012 10:39:05 +0200 Subject: [PATCH 1/2] Add helper to get the value of an alias only --- lib/functions.zsh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/functions.zsh b/lib/functions.zsh index d2dcadd0..b4d89a64 100644 --- a/lib/functions.zsh +++ b/lib/functions.zsh @@ -15,3 +15,33 @@ 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" +} \ No newline at end of file From 8f71efc09b42d69cc053649fade92485d282a561 Mon Sep 17 00:00:00 2001 From: mapc Date: Sat, 5 May 2012 10:39:31 +0200 Subject: [PATCH 2/2] Add helper to easily define default values for variables and env variables. --- lib/functions.zsh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/functions.zsh b/lib/functions.zsh index b4d89a64..3770d4e8 100644 --- a/lib/functions.zsh +++ b/lib/functions.zsh @@ -44,4 +44,32 @@ function alias_value() { # 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 } \ No newline at end of file