From 1dae473c0daa1b5a2c479cf9cae405e4fa170e72 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 15 Dec 2023 13:31:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(`console-text-color-themes.sh`):=20use?= =?UTF-8?q?=20`Guard=20Clauses`=20instead=20of=20nested=20`if`=20?= =?UTF-8?q?=F0=9F=92=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/console-text-color-themes.sh | 73 ++++++++++++++++---------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/lib/console-text-color-themes.sh b/lib/console-text-color-themes.sh index 3944458a..94614c14 100755 --- a/lib/console-text-color-themes.sh +++ b/lib/console-text-color-themes.sh @@ -43,47 +43,48 @@ colorEchoWithoutNewLine() { fi } -# if not directly run this script(use as lib), just export 2 helper functions, and do NOT print anything. +# if source this script(use as lib), just export 2 helper functions, and do NOT print anything. # -# if directly run this script, the length of array BASH_SOURCE is 1. -if ((${#BASH_SOURCE[@]} == 1)); then - 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" - printf ' ' - done - echo +# if directly run this script, the length of array BASH_SOURCE is 1; +# if source this script, the length of array BASH_SOURCE is grater than 1. +((${#BASH_SOURCE[@]} == 1)) || return 0 + +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" + printf ' ' done echo done + echo +done - echo 'Code sample to print color text:' +echo 'Code sample to print color text:' - printf %s ' echo -e "\033[' - colorEchoWithoutNewLine '3;35;40' '1;36;41' - printf %s m - colorEchoWithoutNewLine '0;32;40' 'Sample Text' - printf '%s\n' '\033[0m"' +printf %s ' echo -e "\033[' +colorEchoWithoutNewLine '3;35;40' '1;36;41' +printf %s m +colorEchoWithoutNewLine '0;32;40' 'Sample Text' +printf '%s\n' '\033[0m"' - printf %s " echo \$'\033[" - colorEchoWithoutNewLine '3;35;40' '1;36;41' - printf %s "m'\"" - colorEchoWithoutNewLine '0;32;40' 'Sample Text' - printf '%s\n' "\"$'\033[0m'" - printf '%s\n' " # NOTE: $'foo' is the escape sequence syntax of bash, safer escape" +printf %s " echo \$'\033[" +colorEchoWithoutNewLine '3;35;40' '1;36;41' +printf %s "m'\"" +colorEchoWithoutNewLine '0;32;40' 'Sample Text' +printf '%s\n' "\"$'\033[0m'" +printf '%s\n' " # NOTE: $'foo' is the escape sequence syntax of bash, safer escape" - printf '%s\n' 'Output of above code:' - printf %s ' ' - colorEcho '1;36;41' 'Sample Text' - 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' -fi +printf '%s\n' 'Output of above code:' +printf %s ' ' +colorEcho '1;36;41' 'Sample Text' +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'