Skip to content

Instantly share code, notes, and snippets.

@0x4C4A
Forked from eahrold/cecho
Last active May 20, 2020 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x4C4A/f9345022088ac2dc925d8883f7be6ebe to your computer and use it in GitHub Desktop.
Save 0x4C4A/f9345022088ac2dc925d8883f7be6ebe to your computer and use it in GitHub Desktop.
Color Read / Color Echo in bash
#!/bin/bash
colorread(){
case "$1" in
red|alert) local COLOR=$(printf "\\e[1;31m");;
green|attention) local COLOR=$(printf "\\e[1;32m");;
yellow|warn) local COLOR=$(printf "\\e[1;33m");;
blue|question) local COLOR=$(printf "\\e[1;34m");;
purple|info) local COLOR=$(printf "\\e[1;35m");;
cyan|notice) local COLOR=$(printf "\\e[1;36m");;
bold|prompt) local COLOR=$(printf "\\e[1;30m");;
*) local COLOR=$(printf "\\e[0;30m");;
esac
local MESSAGE="${2}"
local RESET=$(printf "\\e[0m")
if [ -z ${3} ];then
read -e -p "${COLOR}${MESSAGE}${RESET} "
else
read -e -p "${COLOR}${MESSAGE}${RESET} " VAR
eval $3="'$VAR'"
fi
}
colorecho(){
case "$1" in
red|alert) local COLOR=$(printf "\\e[1;31m");;
green|attention) local COLOR=$(printf "\\e[1;32m");;
yellow|warn) local COLOR=$(printf "\\e[1;33m");;
blue|question) local COLOR=$(printf "\\e[1;34m");;
purple|info) local COLOR=$(printf "\\e[1;35m");;
cyan|notice) local COLOR=$(printf "\\e[1;36m");;
bold|prompt) local COLOR=$(printf "\\e[1;30m");;
*) local COLOR=$(printf "\\e[0;30m");;
esac
if [ -z "${2}" ];then
local MESSAGE="${1}"
else
local MESSAGE="${2}"
fi
local RESET=$(printf "\\e[0m")
echo "${COLOR}${MESSAGE}${RESET} ${3}"
}
cecho alert "hello my alert:" "what's new"
cecho prompt "hello my prompt:" "what's new"
cecho question "what's up"
cecho warn "hello my warn"
cecho attention "hello my attention"
cecho notice "hello my notice"
cecho "hello my none"
cread alert "are you ready (y/n/m)?"
if [[ $REPLY =~ ^[Yy]$ ]];then
cecho green yes
elif [[ $REPLY =~ ^[Nn]$ ]];then
cecho red no
elif [[ $REPLY =~ ^[Mm]$ ]];then
cecho yellow maybe
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment