From 25977b59c3ce43167be9e64728441fff67cabb5d Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sun, 7 Jan 2024 18:08:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20improve=20readability=20?= =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - remove var interpolation in `printf format` if possible - rename vars, e.g. `message` -> `content`, `normal` -> `color_reset` - use `\e` instead of `\033` - improve comments for `-t` check --- bin/a2l | 13 +++++++------ bin/ap | 7 ++++--- bin/c | 7 ++++--- bin/coat | 11 ++++++----- bin/cp-into-docker-run | 6 ++++-- bin/echo-args | 10 +++++----- bin/find-in-jars | 24 ++++++++++++------------ bin/rp | 7 ++++--- bin/show-busy-java-threads | 6 ++++-- bin/taoc | 11 ++++++----- bin/uq | 9 +++++---- lib/console-text-color-themes.sh | 16 +++++++++------- lib/parseOpts.sh | 4 ++-- test-cases/bump-scripts-version.sh | 7 ++++--- test-cases/integration-test.sh | 15 +++++++++------ test-cases/my_unit_test_lib.sh | 14 ++++++++------ 16 files changed, 93 insertions(+), 74 deletions(-) diff --git a/bin/a2l b/bin/a2l index d751aeda..a244c646 100755 --- a/bin/a2l +++ b/bin/a2l @@ -22,10 +22,11 @@ readonly PROG_VERSION='2.6.0-dev' colorPrint() { local color="$1" shift - # check isatty in bash https://stackoverflow.com/questions/10022323 - # if stdout is console, turn on color output. + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\033[1;${color}m%s\033[0m\n" "$*" + printf '\e[1;%sm%s\e[0m\n' "$color" "$*" else printf '%s\n' "$*" fi @@ -93,11 +94,11 @@ readonly args readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34) COUNT=0 rotateColorPrint() { - local message="$*" + local content="$*" # skip color for white space - if [[ "$message" =~ ^[[:space:]]*$ ]]; then - printf '%s\n' "$message" + if [[ "$content" =~ ^[[:space:]]*$ ]]; then + printf '%s\n' "$content" else local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}" colorPrint "$color" "$*" diff --git a/bin/ap b/bin/ap index 51c587a5..3b270014 100755 --- a/bin/ap +++ b/bin/ap @@ -24,10 +24,11 @@ readonly PROG_VERSION='2.6.0-dev' colorPrint() { local color="$1" shift - # check isatty in bash https://stackoverflow.com/questions/10022323 - # if stdout is console, turn on color output. + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\033[1;${color}m%s\033[0m\n" "$*" + printf '\e[1;%sm%s\e[0m\n' "$color" "$*" else printf '%s\n' "$*" fi diff --git a/bin/c b/bin/c index 9ebd0e75..f7348ada 100755 --- a/bin/c +++ b/bin/c @@ -36,10 +36,11 @@ readonly PROG_VERSION='2.6.0-dev' ################################################################################ printErrorMsg() { - # check isatty in bash https://stackoverflow.com/questions/10022323 - # if stdout is console, print with red color. + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\033[1;31m%s\033[0m\n\n" "Error: $*" + printf '\e[1;31m%s\e[0m\n\n' "Error: $*" else printf '%s\n\n' "Error: $*" fi diff --git a/bin/coat b/bin/coat index 496bf7ef..af1b3705 100755 --- a/bin/coat +++ b/bin/coat @@ -11,21 +11,22 @@ # @author Jerry Lee (oldratlee at gmail dot com) set -eEuo pipefail -# if not in console, use cat directly +# if stdout is a terminal, use cat directly. +# '-t' check: is a terminal? # check isatty in bash https://stackoverflow.com/questions/10022323 [ ! -t 1 ] && exec cat "$@" readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34) COUNT=0 rotateColorPrint() { - local message="$*" + local content="$*" # skip color for white space - if [[ "$message" =~ ^[[:space:]]*$ ]]; then - printf '%s\n' "$message" + if [[ "$content" =~ ^[[:space:]]*$ ]]; then + printf '%s\n' "$content" else local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}" - printf "\033[1;${color}m%s\033[0m\n" "$message" + printf '\e[1;%sm%s\e[0m\n' "$color" "$content" fi } diff --git a/bin/cp-into-docker-run b/bin/cp-into-docker-run index bf1dd578..00b0d941 100755 --- a/bin/cp-into-docker-run +++ b/bin/cp-into-docker-run @@ -18,9 +18,11 @@ readonly PROG_VERSION='2.6.0-dev' ################################################################################ redPrint() { - # -t check: is a terminal device? + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\033[1;31m%s\033[0m\n" "$*" + printf '\e[1;31m%s\e[0m\n' "$*" else printf '%s\n' "$*" fi diff --git a/bin/echo-args b/bin/echo-args index a2dd472a..b7f2ada9 100755 --- a/bin/echo-args +++ b/bin/echo-args @@ -20,16 +20,16 @@ digitCount() { digit_count=$(digitCount $#) readonly arg_count=$# digit_count -readonly red='\033[1;31m' -readonly blue='\033[1;36m' -readonly normal='\033[0m' +readonly red='\e[1;31m' blue='\e[1;36m' color_reset='\e[0m' printArg() { local idx="$1" value="$2" - # if stdout is console, turn on color output. + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "%${digit_count}s/%s: ${red}[${blue}%s${red}]${normal}\n" "$idx" "$arg_count" "$value" + printf "%${digit_count}s/%s: ${red}[${blue}%s${red}]${color_reset}\n" "$idx" "$arg_count" "$value" else printf "%${digit_count}s/%s: [%s]\n" "$idx" "$arg_count" "$value" fi diff --git a/bin/find-in-jars b/bin/find-in-jars index 9805e714..79f7a9e5 100755 --- a/bin/find-in-jars +++ b/bin/find-in-jars @@ -40,7 +40,7 @@ readonly PROG_VERSION='2.6.0-dev' # util functions ################################################################################ -readonly red='\033[1;31m' normal='\033[0m' +readonly color_reset='\e[0m' # How to delete line with echo? # https://unix.stackexchange.com/questions/26576 @@ -50,12 +50,14 @@ readonly red='\033[1;31m' normal='\033[0m' # echo -e "\033[1K" # Or everything on the line, regardless of cursor position: # echo -e "\033[2K" -readonly clear_line='\033[2K\r' +readonly clear_line='\e[2K\r' redPrint() { - # -t check: is a terminal device? + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "${red}%s${normal}\n" "$*" + printf "\e[1;31m%s${color_reset}\n" "$*" else printf '%s\n' "$*" fi @@ -72,9 +74,9 @@ printResponsiveMessage() { return fi - local message="$*" + local content="$*" # http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html - printf "${clear_line}%s" "${message:0:columns}" >&2 + printf %b%s "${clear_line}" "${content:0:columns}" >&2 } clearResponsiveMessage() { @@ -82,7 +84,7 @@ clearResponsiveMessage() { return fi - printf "%b" "$clear_line" >&2 + printf %b "$clear_line" >&2 } die() { @@ -353,10 +355,8 @@ searchJarFiles() { printf '%s\n' "$jar_files" } -readonly jar_color='\033[1;35m' sep_color='\033[1;32m' - __outputResultOfJarFile() { - local jar_file="$1" file + local jar_file="$1" file jar_color='\e[1;35m' sep_color='\e[1;32m' # shellcheck disable=SC2206 local grep_opt_args=("$regex_mode" ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern") @@ -380,7 +380,7 @@ __outputResultOfJarFile() { clearResponsiveMessage if [ -t 1 ]; then - printf "${jar_color}%s${normal}\n" "${jar_file}" + printf "${jar_color}%s${color_reset}\n" "${jar_file}" else printf '%s\n' "${jar_file}" fi @@ -392,7 +392,7 @@ __outputResultOfJarFile() { } | while read -r file; do clearResponsiveMessage if [ -t 1 ]; then - printf "${jar_color}%s${sep_color}%s${normal}%s\n" "$jar_file" "$separator" "$file" + printf "${jar_color}%s${sep_color}%s${color_reset}%s\n" "$jar_file" "$separator" "$file" else printf '%s\n' "${jar_file}${separator}${file}" fi diff --git a/bin/rp b/bin/rp index 960f2bda..5e5d578e 100755 --- a/bin/rp +++ b/bin/rp @@ -24,10 +24,11 @@ readonly PROG_VERSION='2.6.0-dev' colorPrint() { local color="$1" shift - # check isatty in bash https://stackoverflow.com/questions/10022323 - # if stdout is console, turn on color output. + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\033[1;${color}m%s\033[0m\n" "$*" + printf '\e[1;%sm%s\e[0m\n' "$color" "$*" else printf '%s\n' "$*" fi diff --git a/bin/show-busy-java-threads b/bin/show-busy-java-threads index a7f2e30c..bb392a27 100755 --- a/bin/show-busy-java-threads +++ b/bin/show-busy-java-threads @@ -55,9 +55,11 @@ colorPrint() { local color=$1 shift - # if stdout is console, turn on color output. + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\033[1;${color}m%s\033[0m\n" "$*" + printf '\e[1;%sm%s\e[0m\n' "$color" "$*" else printf '%s\n' "$*" fi diff --git a/bin/taoc b/bin/taoc index de5f600f..c51ada19 100755 --- a/bin/taoc +++ b/bin/taoc @@ -11,21 +11,22 @@ # @author Jerry Lee (oldratlee at gmail dot com) set -eEuo pipefail -# if not in console, use tac directly +# if stdout is a terminal, use cat directly. +# '-t' check: is a terminal? # check isatty in bash https://stackoverflow.com/questions/10022323 [ ! -t 1 ] && exec tac "$@" readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34) COUNT=0 rotateColorPrint() { - local message="$*" + local content="$*" # skip color for white space - if [[ "$message" =~ ^[[:space:]]*$ ]]; then - printf '%s\n' "$message" + if [[ "$content" =~ ^[[:space:]]*$ ]]; then + printf '%s\n' "$content" else local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}" - printf "\033[1;${color}m%s\033[0m\n" "$message" + printf '\e[1;%sm%s\e[0m\n' "$color" "$content" fi } diff --git a/bin/uq b/bin/uq index 51447e1f..2d958fdf 100755 --- a/bin/uq +++ b/bin/uq @@ -41,18 +41,19 @@ readonly PROG_VERSION='2.6.0-dev' readonly nl=$'\n' # new line redPrint() { - # -t check: is a terminal device? + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\033[1;31m%s\033[0m\n" "$*" + printf '\e[1;31m%s\e[0m\n' "$*" else printf '%s\n' "$*" fi } yellowPrint() { - # -t check: is a terminal device? if [ -t 1 ]; then - printf "\033[1;33m%s\033[0m\n" "$*" + printf '\e[1;33m%s\e[0m\n' "$*" else printf '%s\n' "$*" fi diff --git a/lib/console-text-color-themes.sh b/lib/console-text-color-themes.sh index 94614c14..e416388c 100755 --- a/lib/console-text-color-themes.sh +++ b/lib/console-text-color-themes.sh @@ -24,9 +24,11 @@ colorEcho() { local combination="$1" shift 1 - + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\e[${combination}m%s\e[0m\n" "$*" + printf '\e[%sm%s\e[0m\n' "$combination" "$*" else print '%s\n' "$*" fi @@ -37,7 +39,7 @@ colorEchoWithoutNewLine() { shift 1 if [ -t 1 ]; then - printf "\e[${combination}m%s\e[0m" "$*" + printf '\e[%sm%s\e[0m' "$combination" "$*" else printf %s "$*" fi @@ -63,17 +65,17 @@ done echo 'Code sample to print color text:' -printf %s ' echo -e "\033[' +printf %s ' echo -e "\e[' colorEchoWithoutNewLine '3;35;40' '1;36;41' printf %s m colorEchoWithoutNewLine '0;32;40' 'Sample Text' -printf '%s\n' '\033[0m"' +printf '%s\n' '\e[0m"' -printf %s " echo \$'\033[" +printf %s " echo \$'\e[" colorEchoWithoutNewLine '3;35;40' '1;36;41' printf %s "m'\"" colorEchoWithoutNewLine '0;32;40' 'Sample Text' -printf '%s\n' "\"$'\033[0m'" +printf '%s\n' "\"$'\e[0m'" printf '%s\n' " # NOTE: $'foo' is the escape sequence syntax of bash, safer escape" printf '%s\n' 'Output of above code:' diff --git a/lib/parseOpts.sh b/lib/parseOpts.sh index 341d7e4e..31c506bc 100755 --- a/lib/parseOpts.sh +++ b/lib/parseOpts.sh @@ -23,8 +23,8 @@ ##################################################################### # NOTE: $'foo' is the escape sequence syntax of bash -readonly _opts_ec=$'\033' # escape char -readonly _opts_eend=$'\033[0m' # escape end +readonly _opts_ec=$'\e' # escape char +readonly _opts_eend=$'\e[0m' # escape end # shellcheck disable=SC2209 diff --git a/test-cases/bump-scripts-version.sh b/test-cases/bump-scripts-version.sh index 159cb64f..7cd2f70d 100755 --- a/test-cases/bump-scripts-version.sh +++ b/test-cases/bump-scripts-version.sh @@ -10,10 +10,11 @@ readonly nl=$'\n' # new line colorPrint() { local color="$1" shift - # check isatty in bash https://stackoverflow.com/questions/10022323 - # if stdout is console, turn on color output. + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 if [ -t 1 ]; then - printf "\033[1;${color}m%s\033[0m\n" "$*" + printf '\e[1;%sm%s\e[0m\n' "$color" "$*" else printf '%s\n' "$*" fi diff --git a/test-cases/integration-test.sh b/test-cases/integration-test.sh index e7e5e8e7..0242edf7 100755 --- a/test-cases/integration-test.sh +++ b/test-cases/integration-test.sh @@ -13,9 +13,7 @@ cd "$(dirname -- "$($READLINK_CMD -f -- "${BASH_SOURCE[0]}")")" ################################################################################ # NOTE: $'foo' is the escape sequence syntax of bash -readonly ec=$'\033' # escape char -readonly eend=$'\033[0m' # escape end -readonly nl=$'\n' # new line +readonly nl=$'\n' # new line ################################################################################ # common util functions @@ -24,9 +22,14 @@ readonly nl=$'\n' # new line colorEcho() { local color=$1 shift - - # if stdout is the console, turn on color output. - [ -t 1 ] && echo "${ec}[1;${color}m$*${eend}" || echo "$*" + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 + if [[ -t 1 || "${GITHUB_ACTIONS:-}" = true ]]; then + printf '\e[1;%sm%s\e[0m\n' "$color" "$*" + else + printf '%s\n' "$*" + fi } redEcho() { diff --git a/test-cases/my_unit_test_lib.sh b/test-cases/my_unit_test_lib.sh index ab349d06..ebd779f9 100644 --- a/test-cases/my_unit_test_lib.sh +++ b/test-cases/my_unit_test_lib.sh @@ -5,15 +5,17 @@ # commons functions ################################################# -# NOTE: $'foo' is the escape sequence syntax of bash -readonly __ut_ec=$'\033' # escape char -readonly __ut_eend=$'\033[0m' # escape end - __ut_colorEcho() { local color=$1 shift - # if stdout is console, turn on color output. - [ -t 1 ] && echo "${__ut_ec}[1;${color}m$*$__ut_eend" || echo "$*" + # if stdout is a terminal, turn on color output. + # '-t' check: is a terminal? + # check isatty in bash https://stackoverflow.com/questions/10022323 + if [[ -t 1 || "${GITHUB_ACTIONS:-}" = true ]]; then + printf '\e[1;%sm%s\e[0m\n' "$color" "$*" + else + printf '%s\n' "$*" + fi } redEcho() {