Skip to content

Commit 1dae473

Browse files
committed
refactor(console-text-color-themes.sh): use Guard Clauses instead of nested if 💂
1 parent 90eb441 commit 1dae473

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

lib/console-text-color-themes.sh

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,47 +43,48 @@ colorEchoWithoutNewLine() {
4343
fi
4444
}
4545

46-
# if not directly run this script(use as lib), just export 2 helper functions, and do NOT print anything.
46+
# if source this script(use as lib), just export 2 helper functions, and do NOT print anything.
4747
#
48-
# if directly run this script, the length of array BASH_SOURCE is 1.
49-
if ((${#BASH_SOURCE[@]} == 1)); then
50-
for style in 0 1 2 3 4 5 6 7; do
51-
for fg in 30 31 32 33 34 35 36 37; do
52-
for bg in 40 41 42 43 44 45 46 47; do
53-
combination="${style};${fg};${bg}"
54-
colorEchoWithoutNewLine "$combination" "$combination"
55-
printf ' '
56-
done
57-
echo
48+
# if directly run this script, the length of array BASH_SOURCE is 1;
49+
# if source this script, the length of array BASH_SOURCE is grater than 1.
50+
((${#BASH_SOURCE[@]} == 1)) || return 0
51+
52+
for style in 0 1 2 3 4 5 6 7; do
53+
for fg in 30 31 32 33 34 35 36 37; do
54+
for bg in 40 41 42 43 44 45 46 47; do
55+
combination="${style};${fg};${bg}"
56+
colorEchoWithoutNewLine "$combination" "$combination"
57+
printf ' '
5858
done
5959
echo
6060
done
61+
echo
62+
done
6163

62-
echo 'Code sample to print color text:'
64+
echo 'Code sample to print color text:'
6365

64-
printf %s ' echo -e "\033['
65-
colorEchoWithoutNewLine '3;35;40' '1;36;41'
66-
printf %s m
67-
colorEchoWithoutNewLine '0;32;40' 'Sample Text'
68-
printf '%s\n' '\033[0m"'
66+
printf %s ' echo -e "\033['
67+
colorEchoWithoutNewLine '3;35;40' '1;36;41'
68+
printf %s m
69+
colorEchoWithoutNewLine '0;32;40' 'Sample Text'
70+
printf '%s\n' '\033[0m"'
6971

70-
printf %s " echo \$'\033["
71-
colorEchoWithoutNewLine '3;35;40' '1;36;41'
72-
printf %s "m'\""
73-
colorEchoWithoutNewLine '0;32;40' 'Sample Text'
74-
printf '%s\n' "\"$'\033[0m'"
75-
printf '%s\n' " # NOTE: $'foo' is the escape sequence syntax of bash, safer escape"
72+
printf %s " echo \$'\033["
73+
colorEchoWithoutNewLine '3;35;40' '1;36;41'
74+
printf %s "m'\""
75+
colorEchoWithoutNewLine '0;32;40' 'Sample Text'
76+
printf '%s\n' "\"$'\033[0m'"
77+
printf '%s\n' " # NOTE: $'foo' is the escape sequence syntax of bash, safer escape"
7678

77-
printf '%s\n' 'Output of above code:'
78-
printf %s ' '
79-
colorEcho '1;36;41' 'Sample Text'
80-
echo
81-
echo 'If you are going crazy to write text in escapes string like me,'
82-
echo 'you can use colorEcho and colorEchoWithoutNewLine function in this script.'
83-
echo
84-
echo 'Code sample to print color text:'
85-
echo ' colorEcho "1;36;41" "Sample Text"'
86-
echo 'Output of above code:'
87-
echo -n ' '
88-
colorEcho '1;36;41' 'Sample Text'
89-
fi
79+
printf '%s\n' 'Output of above code:'
80+
printf %s ' '
81+
colorEcho '1;36;41' 'Sample Text'
82+
echo
83+
echo 'If you are going crazy to write text in escapes string like me,'
84+
echo 'you can use colorEcho and colorEchoWithoutNewLine function in this script.'
85+
echo
86+
echo 'Code sample to print color text:'
87+
echo ' colorEcho "1;36;41" "Sample Text"'
88+
echo 'Output of above code:'
89+
echo -n ' '
90+
colorEcho '1;36;41' 'Sample Text'

0 commit comments

Comments
 (0)