Created
April 15, 2016 19:46
-
-
Save tjpeden/3d96f4fa04dace3d30b2ebd3b29bf1ff to your computer and use it in GitHub Desktop.
Playing around with some ZSH scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
colorize() { | |
case $1 in | |
none) | |
str="\e[0m";; | |
boldred) | |
str="\e[1;31m";; | |
boldgreen) | |
str="\e[1;32m";; | |
yellow) | |
str="\e[1;33m";; | |
boldblue) | |
str="\e[1;34m";; | |
boldmagenta) | |
str="\e[1;35m";; | |
boldcyan) | |
str="\e[1;36m";; | |
red) | |
str="\e[0;31m";; | |
green) | |
str="\e[0;32m";; | |
orange) | |
str="\e[0;33m";; | |
blue) | |
str="\e[0;34m";; | |
magenta) | |
str="\e[0;35m";; | |
cyan) | |
str="\e[0;36m";; | |
white) | |
str="\e[0;37m";; | |
esac | |
echo -ne $str; | |
} | |
message() { | |
if [ $# -gt 1 ] | |
then | |
color=$1 | |
shift | |
colorize $color | |
fi | |
echo "$@" | |
if [ $color ] | |
then | |
colorize none | |
fi | |
} | |
ask() { | |
read -d '' action | |
while true | |
do | |
if [ $# -gt 1 ] | |
then | |
color=$1 | |
shift | |
colorize $color | |
fi | |
read -p "$@ " answer | |
if [ $color ] | |
then | |
colorize none | |
fi | |
eval "() { | |
$action | |
}" | |
done | |
} | |
ask boldblue "Are you okay?" <<'EOS' | |
case "$answer" in | |
Y*|y*) | |
message boldgreen "Okay.";; | |
N*|n*) | |
message green "Not okay.";; | |
*) | |
message red "Answer unrecognized: ${answer}" | |
continue | |
;; | |
esac | |
break | |
EOS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment