Snippets
Created by
Troy Weber
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #!/usr/bin/env bash
# you can source or run this file
# run as a script, it will print the string using your arguments with the format function
# Usage: log.sh FORMAT_NAME FORE_COLOR BACK_COLOR [TEXT_ARG]...
# Example: log.sh BOLD GREEN WHITE hello world!
# bash codes from http://misc.flogisoft.com/bash/tip_colors_and_formatting
# FORMAT_CODE
format_DEFAULT=
format_BOLD=1
format_DIM=2
format_UNDERLINE=4
format_BLINK=5
format_INVERT=7
format_HIDDEN=8
# COLOR_CODE (these are foreground, for background we add 10)
format_color_DEFAULT=39
format_color_BLACK=30
format_color_RED=31
format_color_GREEN=32
format_color_YELLOW=33
format_color_BLUE=34
format_color_MAGENTA=35
format_color_CYAN=36
format_color_LIGHT_GRAY=37
format_color_DARK_GRAY=90
format_color_LIGHT_RED=91
format_color_LIGHT_GREEN=92
format_color_LIGHT_YELLOW=93
format_color_LIGHT_BLUE=94
format_color_LIGHT_MAGENTA=95
format_color_LIGHT_CYAN=96
format_color_WHITE=97
# Usage: format_accent FORMAT_NAME [TEXT_ARG]...
# Example: echo -e `format_accent BOLD hello world!`
function format_accent()
{
eval "S=\${format_$1}"
if [[ "$1" == "DEFAULT" ]]
then
E=$S
else
E="$(($S+20))"
fi
echo "\e[${S}m${@:2}\e[${E}m"
}
# Usage: format_fore COLOR_NAME [TEXT_ARG]...
# Example: echo -e `format_fore GREEN hello world!`
function format_fore()
{
eval "S=\${format_color_$1}"
E="$format_color_DEFAULT"
echo "\e[${S}m${@:2}\e[${E}m"
}
# Usage: format_back COLOR_NAME [TEXT_ARG]...
# Example: echo -e `format_back GREEN hello world!`
function format_back()
{
eval "S=\$((\${format_color_$1}+10))"
E="$(($format_color_DEFAULT+10))"
echo "\e[${S}m${@:2}\e[${E}m"
}
# Usage: format_colors FORE_COLOR BACK_COLOR [TEXT_ARG]...
# Example: echo -e `format_colors GREEN WHITE hello world!`
function format_colors()
{
echo $(format_fore $1 $(format_back $2 ${@:3}))
}
# Usage: format FORMAT_NAME FORE_COLOR BACK_COLOR [TEXT_ARG]...
# Example: echo -e `format BOLD GREEN WHITE hello world!`
function format()
{
echo $(format_accent $1 $(format_colors $2 $3 ${@:4}))
}
if [[ "$0" == "$BASH_SOURCE" ]]
then
case $1 in
-h)
SCRIPT=`basename $0`
echo -e "Usage: $(format_accent BOLD ${SCRIPT} FORMAT_NAME FORE_COLOR BACK_COLOR [TEXT_ARG]...)"
echo
echo -e "Example: $(format_accent BOLD ${SCRIPT} BOLD GREEN DARK_GRAY hello world!)"
echo -e "Result: $(format BOLD GREEN DARK_GRAY hello world!)"
;;
*)
echo -e `format $1 $2 $3 ${@:4}`
;;
esac
else
if [[ "$1" == "-v" ]]
then
echo "formatting sourced!"
fi
fi
|
Comments (1)
You can clone a snippet to your computer for local editing. Learn more.
To easily include these any of these bash functions into your script, you can source the script with: