Skip to content

Commit 2254367

Browse files
committed
add defensive logic for lib mode
1 parent 145f236 commit 2254367

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

console-text-color-themes.sh

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# @Function
33
# show all console text color themes.
44

5+
readonly _ctct_PROG="$(basename "$(readlink -f "$0")")"
6+
[ "$_ctct_PROG" == 'console-text-color-themes.sh' ] && readonly _ctct_is_direct_run=true
7+
58
colorEcho() {
69
local combination="$1"
710
shift 1
@@ -22,32 +25,36 @@ colorEchoWithoutNewLine() {
2225
} || echo -n "$@"
2326
}
2427

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

37-
echo "Code sample to print color text:"
38-
echo -n ' echo -e "\033['
39-
colorEchoWithoutNewLine "3;35;40" "1;36;41"
40-
echo -n "m"
41-
colorEchoWithoutNewLine "0;32;40" "Sample Text"
42-
echo "\033[0m\""
43-
echo "Output of above code:"
44-
echo -e " \033[1;36;41mSample Text\033[0m"
45-
echo
46-
echo "If you are going crazy to write text in escapes string like me,"
47-
echo "you can use colorEcho and colorEchoWithoutNewLine function in this script."
48-
echo
49-
echo "Code sample to print color text:"
50-
echo ' colorEcho "1;36;41" "Sample Text"'
51-
echo "Output of above code:"
52-
echo -n " "
53-
colorEcho "1;36;41" "Sample Text"
43+
echo "Code sample to print color text:"
44+
echo -n ' echo -e "\033['
45+
colorEchoWithoutNewLine "3;35;40" "1;36;41"
46+
echo -n "m"
47+
colorEchoWithoutNewLine "0;32;40" "Sample Text"
48+
echo "\033[0m\""
49+
echo "Output of above code:"
50+
echo -e " \033[1;36;41mSample Text\033[0m"
51+
echo
52+
echo "If you are going crazy to write text in escapes string like me,"
53+
echo "you can use colorEcho and colorEchoWithoutNewLine function in this script."
54+
echo
55+
echo "Code sample to print color text:"
56+
echo ' colorEcho "1;36;41" "Sample Text"'
57+
echo "Output of above code:"
58+
echo -n " "
59+
colorEcho "1;36;41" "Sample Text"
60+
}

0 commit comments

Comments
 (0)