Skip to content

Commit

Permalink
refactor: simplify and uniform the codes
Browse files Browse the repository at this point in the history
- remove declare for global vars, more consistent
- use upper-case var name for global readonly vars
- unset temp global vars after use
  • Loading branch information
oldratlee committed Feb 18, 2024
1 parent d3224b5 commit 7e03b4f
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion bin/a2l
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ progVersion() {
# parse options
################################################################################

declare -a args=()
args=()
while (($# > 0)); do
case "$1" in
-h | --help)
Expand Down
5 changes: 2 additions & 3 deletions bin/ap
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ progVersion() {
# parse options
################################################################################

declare -a files=()
files=()
while (($# > 0)); do
case "$1" in
-h | --help)
Expand All @@ -129,8 +129,7 @@ while (($# > 0)); do
done

# if files is empty, use "."
files=("${files[@]:-.}")
readonly files
readonly files=("${files[@]:-.}")

################################################################################
# biz logic
Expand Down
2 changes: 1 addition & 1 deletion bin/c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ progVersion() {

quiet=false
keep_eol=false
declare -a target_command=()
target_command=()
while (($# > 0)); do
case "$1" in
-k | --keep-eol)
Expand Down
2 changes: 1 addition & 1 deletion bin/coat
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ EOF
exit
}

declare -a args=("$@")
args=("$@")
# check arguments in reverse, so last option wins.
for ((idx = $# - 1; idx >= 0; --idx)); do
[ "${args[idx]}" = --help ] && usage
Expand Down
2 changes: 1 addition & 1 deletion bin/cp-into-docker-run
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ docker_workdir=
docker_tmpdir=/tmp
docker_command_cp_path=
verbose=false
declare -a args=()
args=()

while (($# > 0)); do
case "$1" in
Expand Down
5 changes: 2 additions & 3 deletions bin/echo-args
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ digitCount() {
digit_count=$(digitCount $#)
readonly arg_count=$# digit_count

readonly red='\e[1;31m' blue='\e[1;36m' color_reset='\e[0m'

readonly RED='\e[1;31m' BLUE='\e[1;36m' COLOR_RESET='\e[0m'
printArg() {
local idx=$1 value=$2

# 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}]${color_reset}\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
Expand Down
47 changes: 25 additions & 22 deletions bin/find-in-jars
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,34 @@ readonly PROG_VERSION='2.x-dev'
# util functions
################################################################################

readonly color_reset='\e[0m'

# How to delete line with echo?
# https://unix.stackexchange.com/questions/26576
#
# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php
# In particular, to clear from the cursor position to the beginning of the line:
# echo -e "\033[1K"
# Or everything on the line, regardless of cursor position:
# echo -e "\033[2K"
readonly clear_line='\e[2K\r'
readonly COLOR_RESET='\e[0m'

redPrint() {
# 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[1;31m%s$color_reset\n" "$*"
printf "\e[1;31m%s$COLOR_RESET\n" "$*"
else
printf '%s\n' "$*"
fi
}

# How to delete line with echo?
# https://unix.stackexchange.com/questions/26576
#
# terminal escapes: http://ascii-table.com/ansi-escape-sequences.php
# In particular, to clear from the cursor position to the beginning of the line:
# echo -e "\033[1K"
# Or everything on the line, regardless of cursor position:
# echo -e "\033[2K"
readonly LINE_CLEAR='\e[2K\r'

# Getting console width using a bash script
# https://unix.stackexchange.com/questions/299067
#
# NOTE: DO NOT declare columns var as readonly in ONE line!
[ -t 2 ] && columns=$(stty size | awk '{print $2}')
[ -t 2 ] && COLUMNS=$(stty size | awk '{print $2}')

printResponsiveMessage() {
if ! $show_responsive || [ ! -t 2 ]; then
Expand All @@ -74,15 +74,15 @@ printResponsiveMessage() {

local content=$*
# http://www.linuxforums.org/forum/red-hat-fedora-linux/142825-how-truncate-string-bash-script.html
printf %b%s "$clear_line" "${content:0:columns}" >&2
printf %b%s "$LINE_CLEAR" "${content:0:COLUMNS}" >&2
}

clearResponsiveMessage() {
if ! $show_responsive || [ ! -t 2 ]; then
return
fi

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

die() {
Expand Down Expand Up @@ -155,9 +155,9 @@ progVersion() {
# parse options
################################################################################

declare -a dirs=()
declare -a extensions=()
declare -a args=()
dirs=()
extensions=()
args=()

separator='!'
regex_mode=-E
Expand Down Expand Up @@ -250,7 +250,7 @@ readonly extensions=${extensions:-jar}
((${#args[@]} > 1)) && usage 1 "More than 1 file pattern: ${args[*]}"
readonly pattern=${args[0]}

declare -a tmp_dirs=()
tmp_dirs=()
for d in "${dirs[@]}"; do
[ -e "$d" ] || die "file $d(specified by option -d) does not exist!"
[ -d "$d" ] || die "file $d(specified by option -d) exists but is not a directory!"
Expand All @@ -262,13 +262,15 @@ done
# set dirs to Absolute Path
$use_absolute_path && dirs=("${tmp_dirs[@]}")
readonly dirs
unset d tmp_dirs

# convert extensions to find -iname options
find_iname_options=()
for e in "${extensions[@]}"; do
find_iname_options=(${find_iname_options[@]:+"${find_iname_options[@]}" -o} -iname "*.$e")
done
readonly find_iname_options
unset e

################################################################################
# Check the existence of command for listing zip entry!
Expand Down Expand Up @@ -353,8 +355,9 @@ searchJarFiles() {
printf '%s\n' "$jar_files"
}

readonly JAR_COLOR='\e[1;35m' SEP_COLOR='\e[1;32m'
__outputResultOfJarFile() {
local jar_file=$1 file jar_color='\e[1;35m' sep_color='\e[1;32m'
local jar_file=$1 file
# shellcheck disable=SC2206
local grep_opt_args=("$regex_mode" ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")

Expand All @@ -374,7 +377,7 @@ __outputResultOfJarFile() {

clearResponsiveMessage
if [ -t 1 ]; then
printf "$jar_color%s$color_reset\n" "$jar_file"
printf "$JAR_COLOR%s$COLOR_RESET\n" "$jar_file"
else
printf '%s\n' "$jar_file"
fi
Expand All @@ -386,7 +389,7 @@ __outputResultOfJarFile() {
} | while read -r file; do
clearResponsiveMessage
if [ -t 1 ]; then
printf "$jar_color%s$sep_color%s$color_reset%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
Expand Down
2 changes: 1 addition & 1 deletion bin/rp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ progVersion() {
# parse options
################################################################################

declare -a files=()
files=()
while (($# > 0)); do
case "$1" in
-h | --help)
Expand Down
12 changes: 6 additions & 6 deletions bin/show-busy-java-threads
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ readonly USER
################################################################################

# NOTE: $'foo' is the escape sequence syntax of bash
readonly nl=$'\n' # new line
readonly NL=$'\n' # new line

colorPrint() {
local color=$1
Expand Down Expand Up @@ -151,7 +151,7 @@ usage() {
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))

(($# > 0)) && colorPrint 31 "$*$nl" >&"$out"
(($# > 0)) && colorPrint 31 "$*$NL" >&"$out"

cat >&"$out" <<EOF
Usage: ${PROG} [OPTION]... [delay [count]]
Expand Down Expand Up @@ -352,18 +352,18 @@ if [ -n "$jstack_path" ]; then
elif [ -n "$JAVA_HOME" ]; then
# 2. search jstack under JAVA_HOME
if [ -f "$JAVA_HOME/bin/jstack" ]; then
[ -x "$JAVA_HOME/bin/jstack" ] || die "found \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executable!${nl}Use -s option set jstack path manually."
[ -x "$JAVA_HOME/bin/jstack" ] || die "found \$JAVA_HOME/bin/jstack($JAVA_HOME/bin/jstack) is NOT executable!${NL}Use -s option set jstack path manually."
jstack_path="$JAVA_HOME/bin/jstack"
elif [ -f "$JAVA_HOME/../bin/jstack" ]; then
[ -x "$JAVA_HOME/../bin/jstack" ] || die "found \$JAVA_HOME/../bin/jstack($JAVA_HOME/../bin/jstack) is NOT executable!${nl}Use -s option set jstack path manually."
[ -x "$JAVA_HOME/../bin/jstack" ] || die "found \$JAVA_HOME/../bin/jstack($JAVA_HOME/../bin/jstack) is NOT executable!${NL}Use -s option set jstack path manually."
jstack_path="$JAVA_HOME/../bin/jstack"
fi
elif command -v jstack &>/dev/null; then
# 3. search jstack under PATH
jstack_path=$(command -v jstack)
[ -x "$jstack_path" ] || die "found $jstack_path from PATH is NOT executable!${nl}Use -s option set jstack path manually."
[ -x "$jstack_path" ] || die "found $jstack_path from PATH is NOT executable!${NL}Use -s option set jstack path manually."
else
die "jstack NOT found by JAVA_HOME(${JAVA_HOME:-not set}) setting and PATH!${nl}Use -s option set jstack path manually."
die "jstack NOT found by JAVA_HOME(${JAVA_HOME:-not set}) setting and PATH!${NL}Use -s option set jstack path manually."
fi
readonly jstack_path

Expand Down
2 changes: 1 addition & 1 deletion bin/taoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ EOF
exit
}

declare -a args=("$@")
args=("$@")
# check arguments in reverse, so last option wins.
for ((idx = $# - 1; idx >= 0; --idx)); do
[ "${args[idx]}" = --help ] && usage
Expand Down
6 changes: 4 additions & 2 deletions bin/uq
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ uq_opt_only_unique=0
uq_opt_ignore_case=0
uq_opt_zero_terminated=0
uq_max_input_human_readable_size=256m
declare -a argv=()
argv=()

while (($# > 0)); do
case "$1" in
Expand Down Expand Up @@ -221,7 +221,7 @@ done
uq_max_input_size=$(convertHumanReadableSizeToSize "$uq_max_input_human_readable_size") ||
usage 2 "[$PROG] ERROR: illegal value of option -XM/--max-input: $uq_max_input_human_readable_size"

readonly argc=${#argv[@]}
readonly argc=${#argv[@]} argv

if ((argc == 0)); then
input_files=()
Expand All @@ -236,6 +236,7 @@ else
output_file=/dev/stdout
fi
fi
readonly output_file

# Check input file
for f in ${input_files[@]:+"${input_files[@]}"}; do
Expand All @@ -247,6 +248,7 @@ for f in ${input_files[@]:+"${input_files[@]}"}; do
[ -f "$f" ] || die "input file $f exists, but is not a file!"
[ -r "$f" ] || die "input file $f exists, but is not readable!"
done
unset f

################################################################################
# biz logic
Expand Down
2 changes: 1 addition & 1 deletion bin/xpl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ progVersion() {
# parse options
################################################################################

declare -a files=()
files=()
selected=false
while (($# > 0)); do
case "$1" in
Expand Down
14 changes: 8 additions & 6 deletions legacy-bin/swtrunk
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
# @online-doc https://github.com/oldratlee/useful-scripts/blob/dev-2.x/docs/vcs.md#-swtrunk
# @author Jerry Lee (oldratlee at gmail dot com)

# NOTE: $'foo' is the escape sequence syntax of bash
readonly ec=$'\033' # escape char
readonly eend=$'\033[0m' # escape end

colorEcho() {
local color=$1
shift
# if stdout is 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 ]; then
printf '\e[1;%sm%s\e[0m\n' "$color" "$*"
else
printf '%s\n' "$*"
fi
}

redEcho() {
Expand Down

0 comments on commit 7e03b4f

Please sign in to comment.