Skip to content

Commit

Permalink
add defensive logic for lib mode
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Nov 20, 2016
1 parent 145f236 commit 2254367
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions console-text-color-themes.sh
Expand Up @@ -2,6 +2,9 @@
# @Function
# show all console text color themes.

readonly _ctct_PROG="$(basename "$(readlink -f "$0")")"
[ "$_ctct_PROG" == 'console-text-color-themes.sh' ] && readonly _ctct_is_direct_run=true

colorEcho() {
local combination="$1"
shift 1
Expand All @@ -22,32 +25,36 @@ colorEchoWithoutNewLine() {
} || echo -n "$@"
}

for style in 0 1 2 3 4 5 6 7; do
for fg in 30 31 32 33 34 35 36 37; do
for bg in 40 41 42 43 44 45 46 47; do
combination="${style};${fg};${bg}"
colorEchoWithoutNewLine "$combination" "$combination"
echo -n " "
# if not directly run this script(use as lib), just export 2 helper functions,
# and do NOT print anything.
[ "$_ctct_is_direct_run" == "true" ] && {
for style in 0 1 2 3 4 5 6 7; do
for fg in 30 31 32 33 34 35 36 37; do
for bg in 40 41 42 43 44 45 46 47; do
combination="${style};${fg};${bg}"
colorEchoWithoutNewLine "$combination" "$combination"
echo -n " "
done
echo
done
echo
done
echo
done

echo "Code sample to print color text:"
echo -n ' echo -e "\033['
colorEchoWithoutNewLine "3;35;40" "1;36;41"
echo -n "m"
colorEchoWithoutNewLine "0;32;40" "Sample Text"
echo "\033[0m\""
echo "Output of above code:"
echo -e " \033[1;36;41mSample Text\033[0m"
echo
echo "If you are going crazy to write text in escapes string like me,"
echo "you can use colorEcho and colorEchoWithoutNewLine function in this script."
echo
echo "Code sample to print color text:"
echo ' colorEcho "1;36;41" "Sample Text"'
echo "Output of above code:"
echo -n " "
colorEcho "1;36;41" "Sample Text"
echo "Code sample to print color text:"
echo -n ' echo -e "\033['
colorEchoWithoutNewLine "3;35;40" "1;36;41"
echo -n "m"
colorEchoWithoutNewLine "0;32;40" "Sample Text"
echo "\033[0m\""
echo "Output of above code:"
echo -e " \033[1;36;41mSample Text\033[0m"
echo
echo "If you are going crazy to write text in escapes string like me,"
echo "you can use colorEcho and colorEchoWithoutNewLine function in this script."
echo
echo "Code sample to print color text:"
echo ' colorEcho "1;36;41" "Sample Text"'
echo "Output of above code:"
echo -n " "
colorEcho "1;36;41" "Sample Text"
}

0 comments on commit 2254367

Please sign in to comment.