Last active
October 4, 2021 18:02
-
-
Save alanEG/6acc947bfe2a903399ab8dd526fa940c to your computer and use it in GitHub Desktop.
color bash, source color https://github.com/s0md3v/Arjun/blob/master/arjun/core/colors.py
This file contains hidden or 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
#!/bin/bash | |
#usage | |
# bash color.sh "{{red}}hello {{blue}}marcos" # singel color | |
# bash color.sh "{{red}}hello [start]{{red}}how are you[end]" | |
text="" | |
args="$@" | |
declare -A colors=(\ | |
["{{reset}}"]="\\\033[0m" \ | |
["{{black}}"]="\\\033[0;30m" \ | |
["{{red}}"]="\\\033[0;31m" \ | |
["{{green}}"]="\\\033[0;32m" \ | |
["{{yellow}}"]="\\\033[0;33m" \ | |
["{{blue}}"]="\\\033[0;34m" \ | |
["{{purple}}"]="\\\033[0;35m" \ | |
["{{cyan}}"]="\\\033[0;36m" \ | |
["{{white}}"]="\\\033[0;37m" \ | |
["{{ok}}"]="\\\033[1;92m[✓]" \ | |
["{{good}}"]="\\\033[1;32m[+]" \ | |
["{{bad}}"]="\\\033[1;91m[-]" \ | |
["{{unknow}}"]="\\\033[1;94m[?]" \ | |
["{{info}}"]="\\\033[1;93m[!]" \ | |
) | |
parseColor(){ | |
for i in ${args} | |
do | |
color="$(echo "${i}" | grep -o -E '\{\{[a-zA-Z]+\}\}')" | |
if [ -z "${color}" ] | |
then | |
text+=$(echo "${i}") | |
else | |
text+=$(echo "${i}" | sed "s/$color/${colors[${color}]}/g" | sed 's/$/\\033[0m /') | |
fi | |
done | |
} | |
manyWords="$(echo ${args} | grep -o -E '\[start\](.*)\[end\]')" # get tag start | |
getColorManyWords="$(echo ${manyWords} | grep -o -E '\{\{[a-zA-Z]+\}\}')" # get color from tag start | |
if [ ! -z "${manyWords}" ] | |
then | |
args="$(echo ${args} | sed -E 's/\[start\](.*)\[end\]/{start_end_here}/g')" # replace start tag to {start_end_here} for replace in future | |
parseColor ${args} # replace tag color | |
finalManyWords=$(echo ${manyWords} | sed -E 's/\[start\]|\[end\]//g' | sed "s/${getColorManyWords}/\\\\${colors[${getColorManyWords}]}/g"| sed 's/$/\\\\033[0m /') # parse tag start | |
text=$(echo "${text}" | sed "s/{start_end_here}/${finalManyWords}/g") # replace tag ${start_end_here} to tag start after parse | |
else | |
parseColor ${args} | |
fi | |
echo -e ${text} 1>&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment