Skip to content

Commit 7a779ef

Browse files
committed
refactor: improve readability 🛠️
- 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
1 parent 9f24158 commit 7a779ef

File tree

16 files changed

+100
-81
lines changed

16 files changed

+100
-81
lines changed

bin/a2l

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ readonly PROG_VERSION='2.6.0-dev'
2222
colorPrint() {
2323
local color="$1"
2424
shift
25-
# check isatty in bash https://stackoverflow.com/questions/10022323
26-
# if stdout is console, turn on color output.
25+
# if stdout is a terminal, turn on color output.
26+
# '-t' check: is a terminal?
27+
# check isatty in bash https://stackoverflow.com/questions/10022323
2728
if [ -t 1 ]; then
28-
printf "\033[1;${color}m%s\033[0m\n" "$*"
29+
printf '\e[1;%sm%s\e[0m\n' "$color" "$*"
2930
else
3031
printf '%s\n' "$*"
3132
fi
@@ -93,14 +94,14 @@ readonly args
9394
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
9495
COUNT=0
9596
rotateColorPrint() {
96-
local message="$*"
97+
local content="$*"
9798

9899
# skip color for white space
99-
if [[ "$message" =~ ^[[:space:]]*$ ]]; then
100-
printf '%s\n' "$message"
100+
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
101+
printf '%s\n' "$content"
101102
else
102103
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
103-
colorPrint "$color" "$*"
104+
colorPrint "$color" "$content"
104105
fi
105106
}
106107

bin/ap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ readonly PROG_VERSION='2.6.0-dev'
2424
colorPrint() {
2525
local color="$1"
2626
shift
27-
# check isatty in bash https://stackoverflow.com/questions/10022323
28-
# if stdout is console, turn on color output.
27+
# if stdout is a terminal, turn on color output.
28+
# '-t' check: is a terminal?
29+
# check isatty in bash https://stackoverflow.com/questions/10022323
2930
if [ -t 1 ]; then
30-
printf "\033[1;${color}m%s\033[0m\n" "$*"
31+
printf '\e[1;%sm%s\e[0m\n' "$color" "$*"
3132
else
3233
printf '%s\n' "$*"
3334
fi

bin/c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ readonly PROG_VERSION='2.6.0-dev'
3636
################################################################################
3737

3838
printErrorMsg() {
39-
# check isatty in bash https://stackoverflow.com/questions/10022323
40-
# if stdout is console, print with red color.
39+
# if stdout is a terminal, turn on color output.
40+
# '-t' check: is a terminal?
41+
# check isatty in bash https://stackoverflow.com/questions/10022323
4142
if [ -t 1 ]; then
42-
printf "\033[1;31m%s\033[0m\n\n" "Error: $*"
43+
printf '\e[1;31m%s\e[0m\n\n' "Error: $*"
4344
else
4445
printf '%s\n\n' "Error: $*"
4546
fi

bin/coat

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
# @author Jerry Lee (oldratlee at gmail dot com)
1212
set -eEuo pipefail
1313

14-
# if not in console, use cat directly
14+
# if stdout is a terminal, use `cat` directly.
15+
# '-t' check: is a terminal?
1516
# check isatty in bash https://stackoverflow.com/questions/10022323
1617
[ ! -t 1 ] && exec cat "$@"
1718

1819
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
1920
COUNT=0
2021
rotateColorPrint() {
21-
local message="$*"
22+
local content="$*"
2223

2324
# skip color for white space
24-
if [[ "$message" =~ ^[[:space:]]*$ ]]; then
25-
printf '%s\n' "$message"
25+
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
26+
printf '%s\n' "$content"
2627
else
2728
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
28-
printf "\033[1;${color}m%s\033[0m\n" "$message"
29+
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
2930
fi
3031
}
3132

bin/cp-into-docker-run

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ readonly PROG_VERSION='2.6.0-dev'
1818
################################################################################
1919

2020
redPrint() {
21-
# -t check: is a terminal device?
21+
# if stdout is a terminal, turn on color output.
22+
# '-t' check: is a terminal?
23+
# check isatty in bash https://stackoverflow.com/questions/10022323
2224
if [ -t 1 ]; then
23-
printf "\033[1;31m%s\033[0m\n" "$*"
25+
printf '\e[1;31m%s\e[0m\n' "$*"
2426
else
2527
printf '%s\n' "$*"
2628
fi

bin/echo-args

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ digitCount() {
2020
digit_count=$(digitCount $#)
2121
readonly arg_count=$# digit_count
2222

23-
readonly red='\033[1;31m'
24-
readonly blue='\033[1;36m'
25-
readonly normal='\033[0m'
23+
readonly red='\e[1;31m' blue='\e[1;36m' color_reset='\e[0m'
2624

2725
printArg() {
2826
local idx="$1" value="$2"
2927

30-
# if stdout is console, turn on color output.
28+
# if stdout is a terminal, turn on color output.
29+
# '-t' check: is a terminal?
30+
# check isatty in bash https://stackoverflow.com/questions/10022323
3131
if [ -t 1 ]; then
32-
printf "%${digit_count}s/%s: ${red}[${blue}%s${red}]${normal}\n" "$idx" "$arg_count" "$value"
32+
printf "%${digit_count}s/%s: ${red}[${blue}%s${red}]${color_reset}\n" "$idx" "$arg_count" "$value"
3333
else
3434
printf "%${digit_count}s/%s: [%s]\n" "$idx" "$arg_count" "$value"
3535
fi

bin/find-in-jars

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ readonly PROG_VERSION='2.6.0-dev'
4040
# util functions
4141
################################################################################
4242

43-
readonly red='\033[1;31m' normal='\033[0m'
43+
readonly color_reset='\e[0m'
4444

4545
# How to delete line with echo?
4646
# https://unix.stackexchange.com/questions/26576
@@ -50,12 +50,14 @@ readonly red='\033[1;31m' normal='\033[0m'
5050
# echo -e "\033[1K"
5151
# Or everything on the line, regardless of cursor position:
5252
# echo -e "\033[2K"
53-
readonly clear_line='\033[2K\r'
53+
readonly clear_line='\e[2K\r'
5454

5555
redPrint() {
56-
# -t check: is a terminal device?
56+
# if stdout is a terminal, turn on color output.
57+
# '-t' check: is a terminal?
58+
# check isatty in bash https://stackoverflow.com/questions/10022323
5759
if [ -t 1 ]; then
58-
printf "${red}%s${normal}\n" "$*"
60+
printf "\e[1;31m%s${color_reset}\n" "$*"
5961
else
6062
printf '%s\n' "$*"
6163
fi
@@ -72,17 +74,17 @@ printResponsiveMessage() {
7274
return
7375
fi
7476

75-
local message="$*"
77+
local content="$*"
7678
# http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html
77-
printf "${clear_line}%s" "${message:0:columns}" >&2
79+
printf %b%s "${clear_line}" "${content:0:columns}" >&2
7880
}
7981

8082
clearResponsiveMessage() {
8183
if ! $show_responsive || [ ! -t 2 ]; then
8284
return
8385
fi
8486

85-
printf "%b" "$clear_line" >&2
87+
printf %b "$clear_line" >&2
8688
}
8789

8890
die() {
@@ -322,14 +324,14 @@ listZipEntries() {
322324
clearResponsiveMessage
323325
redPrint "fail to list zip entries of $zip_file, ignored: $msg" >&2
324326
fi
325-
return 0
327+
return
326328
}
327329
fi
328330

329331
"${command_to_list_zip_entries[@]}" "$zip_file" || {
330332
clearResponsiveMessage
331333
redPrint "fail to list zip entries of $zip_file, ignored!" >&2
332-
return 0
334+
return
333335
}
334336
}
335337

@@ -347,16 +349,14 @@ searchJarFiles() {
347349

348350
total_jar_count="$(printf '%s\n' "$jar_files" | wc -l)"
349351
# remove white space, because the `wc -l` output on mac contains white space!
350-
total_jar_count="${total_jar_count//[[:space:]]/}"
352+
total_jar_count=${total_jar_count//[[:space:]]}
351353

352354
echo "$total_jar_count"
353355
printf '%s\n' "$jar_files"
354356
}
355357

356-
readonly jar_color='\033[1;35m' sep_color='\033[1;32m'
357-
358358
__outputResultOfJarFile() {
359-
local jar_file="$1" file
359+
local jar_file="$1" file jar_color='\e[1;35m' sep_color='\e[1;32m'
360360
# shellcheck disable=SC2206
361361
local grep_opt_args=("$regex_mode" ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")
362362

@@ -380,7 +380,7 @@ __outputResultOfJarFile() {
380380

381381
clearResponsiveMessage
382382
if [ -t 1 ]; then
383-
printf "${jar_color}%s${normal}\n" "${jar_file}"
383+
printf "${jar_color}%s${color_reset}\n" "${jar_file}"
384384
else
385385
printf '%s\n' "${jar_file}"
386386
fi
@@ -392,7 +392,7 @@ __outputResultOfJarFile() {
392392
} | while read -r file; do
393393
clearResponsiveMessage
394394
if [ -t 1 ]; then
395-
printf "${jar_color}%s${sep_color}%s${normal}%s\n" "$jar_file" "$separator" "$file"
395+
printf "${jar_color}%s${sep_color}%s${color_reset}%s\n" "$jar_file" "$separator" "$file"
396396
else
397397
printf '%s\n' "${jar_file}${separator}${file}"
398398
fi

bin/rp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ readonly PROG_VERSION='2.6.0-dev'
2424
colorPrint() {
2525
local color="$1"
2626
shift
27-
# check isatty in bash https://stackoverflow.com/questions/10022323
28-
# if stdout is console, turn on color output.
27+
# if stdout is a terminal, turn on color output.
28+
# '-t' check: is a terminal?
29+
# check isatty in bash https://stackoverflow.com/questions/10022323
2930
if [ -t 1 ]; then
30-
printf "\033[1;${color}m%s\033[0m\n" "$*"
31+
printf '\e[1;%sm%s\e[0m\n' "$color" "$*"
3132
else
3233
printf '%s\n' "$*"
3334
fi

bin/show-busy-java-threads

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ colorPrint() {
5555
local color=$1
5656
shift
5757

58-
# if stdout is console, turn on color output.
58+
# if stdout is a terminal, turn on color output.
59+
# '-t' check: is a terminal?
60+
# check isatty in bash https://stackoverflow.com/questions/10022323
5961
if [ -t 1 ]; then
60-
printf "\033[1;${color}m%s\033[0m\n" "$*"
62+
printf '\e[1;%sm%s\e[0m\n' "$color" "$*"
6163
else
6264
printf '%s\n' "$*"
6365
fi
@@ -301,7 +303,7 @@ isNaturalNumber "$update_count" || die "update count($update_count) is not a nat
301303
readonly update_count
302304

303305
if [ -n "$pid_list" ]; then
304-
pid_list="${pid_list//[[:space:]]/}" # delete white space
306+
pid_list=${pid_list//[[:space:]]} # delete white space
305307
isNaturalNumberList "$pid_list" || die "pid(s)($pid_list) is illegal! example: 42 or 42,99,67"
306308
fi
307309
readonly pid_list
@@ -532,7 +534,7 @@ findBusyJavaThreadsByTop() {
532534
printStackOfThreads() {
533535
local idx=0 pid threadId pcpu user threadId0x
534536
while read -r pid threadId pcpu user; do
535-
threadId0x="0x$(printf %x "${threadId}")"
537+
printf -v threadId0x '%#x' "$threadId"
536538

537539
((idx++))
538540
local jstackFile="${store_file_prefix}$((update_round_num + 1))_jstack_${pid}"

bin/taoc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# @Function
3-
# tac lines colorfully. taoc means *CO*lorful c*AT* in reverse(last line first).
3+
# tac lines colorfully. taoc means coat(*CO*lorful c*AT*) in reverse(last line first).
44
#
55
# @Usage
66
# $ echo -e 'Hello\nWorld' | taoc
@@ -11,21 +11,22 @@
1111
# @author Jerry Lee (oldratlee at gmail dot com)
1212
set -eEuo pipefail
1313

14-
# if not in console, use tac directly
14+
# if stdout is a terminal, use `tac` directly.
15+
# '-t' check: is a terminal?
1516
# check isatty in bash https://stackoverflow.com/questions/10022323
1617
[ ! -t 1 ] && exec tac "$@"
1718

1819
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
1920
COUNT=0
2021
rotateColorPrint() {
21-
local message="$*"
22+
local content="$*"
2223

2324
# skip color for white space
24-
if [[ "$message" =~ ^[[:space:]]*$ ]]; then
25-
printf '%s\n' "$message"
25+
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
26+
printf '%s\n' "$content"
2627
else
2728
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
28-
printf "\033[1;${color}m%s\033[0m\n" "$message"
29+
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
2930
fi
3031
}
3132

0 commit comments

Comments
 (0)