Skip to content

Instantly share code, notes, and snippets.

@s4parke
Created February 14, 2018 17:16
Show Gist options
  • Save s4parke/85eb54addf6ce7d326257e5a52fbb06d to your computer and use it in GitHub Desktop.
Save s4parke/85eb54addf6ce7d326257e5a52fbb06d to your computer and use it in GitHub Desktop.
Multicolor text formatting utility for bash scripts
#!/bin/bash
# message.sh
# Multicolored text formatting utility for bash
#
# Usage:
# message --help
#
# message -red "string1" -s -blue "string2" -t "string3" ...
# msg -error "ERROR:" -s -info "File not found"
#
alias msg=message
function message() {
echo -e " "
while (($#!=0))
do
case $1 in
-b|-s)
echo -ne " ";
;;
-t)
echo -ne "\t";
;;
-n)
echo -ne "\n";
;;
-black)
echo -ne "$EBLACK";
;;
-red)
echo -ne "$ERED";
;;
-green)
# echo -ne "\033[32m";
echo -ne "$EGREEN";
;;
-yellow)
echo -ne "$EYELLOW";
;;
-blue)
echo -ne "$EBLUE";
;;
-magenta)
echo -ne "$EMAGENTA";
;;
-cyan)
echo -ne "$ECYAN";
;;
-white|-gray) echo -ne "$EWHITE";
;;
-error)
echo -ne "$BRED$EWHITE";
;;
-success)
echo -ne "$BGREEN$EWHITE";
;;
-info)
echo -ne "$UWHITE";
;;
-reset|-r)
echo -ne "$RESET";
;;
-h|-help|--help)
echo 'Usage: message -color1 "string1" -reset "string2" -n -t -color2 "string3" ...';
echo 'eg: message -error "Error" -info "File not found"';
;;
*)
echo -ne "${1}${RESET}";
;;
esac
shift
done
echo -e " "
}
# Colors from http://wiki.archlinux.org/index.php/Color_Bash_Prompt
#
NO_COLOR='\e[0m' #disable any colors
RESET=$NO_COLOR
# regular colors
BLACK='\e[0;30m'
RED='\e[0;31m'
GREEN='\e[0;32m'
YELLOW='\e[0;33m'
BLUE='\e[0;34m'
MAGENTA='\e[0;35m'
CYAN='\e[0;36m'
WHITE='\e[0;37m'
# emphasized (bolded) colors
EBLACK='\e[1;30m'
ERED='\e[1;31m'
EGREEN='\e[1;32m'
EYELLOW='\e[1;33m'
EBLUE='\e[1;34m'
EMAGENTA='\e[1;35m'
ECYAN='\e[1;36m'
EWHITE='\e[1;37m'
# underlined colors
UBLACK='\e[4;30m'
URED='\e[4;31m'
UGREEN='\e[4;32m'
UYELLOW='\e[4;33m'
UBLUE='\e[4;34m'
UMAGENTA='\e[4;35m'
UCYAN='\e[4;36m'
UWHITE='\e[4;37m'
# background colors
BBLACK='\e[40m'
BRED='\e[41m'
BGREEN='\e[42m'
BYELLOW='\e[43m'
BBLUE='\e[44m'
BMAGENTA='\e[45m'
BCYAN='\e[46m'
BWHITE='\e[47m'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment