Skip to content

Commit

Permalink
refactor: improve readability 🛠️
Browse files Browse the repository at this point in the history
- remove var interpolation in `printf format` if possible
- rename vars, e.g. `message` -> `content`
- use `\e` instead of `\033`
- improve comments for `-t` check
  • Loading branch information
oldratlee committed Jan 7, 2024
1 parent 1dae473 commit 5f2bab6
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 68 deletions.
13 changes: 7 additions & 6 deletions bin/a2l
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" "$*"
Expand Down
7 changes: 4 additions & 3 deletions bin/ap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions bin/c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions bin/coat
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 4 additions & 2 deletions bin/cp-into-docker-run
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions bin/echo-args
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +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'
readonly blue='\e[1;36m'
readonly normal='\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"
else
Expand Down
16 changes: 9 additions & 7 deletions bin/find-in-jars
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ readonly PROG_VERSION='2.6.0-dev'
# util functions
################################################################################

readonly red='\033[1;31m' normal='\033[0m'
readonly red='\e[1;31m' normal='\e[0m'

# How to delete line with echo?
# https://unix.stackexchange.com/questions/26576
Expand All @@ -50,10 +50,12 @@ 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" "$*"
else
Expand All @@ -72,17 +74,17 @@ 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() {
if ! $show_responsive || [ ! -t 2 ]; then
return
fi

printf "%b" "$clear_line" >&2
printf %b "$clear_line" >&2
}

die() {
Expand Down Expand Up @@ -353,7 +355,7 @@ searchJarFiles() {
printf '%s\n' "$jar_files"
}

readonly jar_color='\033[1;35m' sep_color='\033[1;32m'
readonly jar_color='\e[1;35m' sep_color='\e[1;32m'

__outputResultOfJarFile() {
local jar_file="$1" file
Expand Down
7 changes: 4 additions & 3 deletions bin/rp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions bin/show-busy-java-threads
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions bin/taoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 5 additions & 4 deletions bin/uq
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions lib/console-text-color-themes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:'
Expand Down
4 changes: 2 additions & 2 deletions lib/parseOpts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions test-cases/bump-scripts-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions test-cases/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
14 changes: 8 additions & 6 deletions test-cases/my_unit_test_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5f2bab6

Please sign in to comment.