From fde4a18eae9c7810280e52ac08dbf8a2f7b5afe3 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Wed, 22 Nov 2023 22:16:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20use=20`BASH=5FSOURCE`=20array=20to?= =?UTF-8?q?=20detect=20whether=20this=20script=20is=20sourced=20or=20not?= =?UTF-8?q?=20=F0=9F=95=B5=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/console-text-color-themes.sh | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/console-text-color-themes.sh b/lib/console-text-color-themes.sh index c7b3d287..4f9b4fb9 100755 --- a/lib/console-text-color-themes.sh +++ b/lib/console-text-color-themes.sh @@ -26,10 +26,6 @@ if command -v greadlink >/dev/null; then _ctct_READLINK_CMD=greadlink fi -# NOTE: DO NOT declare var _ctct_PROG as readonly in ONE line! -_ctct_PROG="$(basename -- "$($_ctct_READLINK_CMD -f -- "${BASH_SOURCE[0]}")")" -[ "$_ctct_PROG" == 'console-text-color-themes.sh' ] && readonly _ctct_is_direct_run=true - readonly _ctct_ec=$'\033' # escape char readonly _ctct_eend=$'\033[0m' # escape end @@ -55,9 +51,10 @@ colorEchoWithoutNewLine() { fi } -# if not directly run this script(use as lib), just export 2 helper functions, -# and do NOT print anything. -[ "$_ctct_is_direct_run" == true ] && { +# if not directly run 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 @@ -97,4 +94,4 @@ colorEchoWithoutNewLine() { echo 'Output of above code:' echo -n ' ' colorEcho '1;36;41' 'Sample Text' -} +fi