Skip to content

Commit

Permalink
refactor: use file descriptor number instead of /dev/std*; move var…
Browse files Browse the repository at this point in the history
… location
  • Loading branch information
oldratlee committed Nov 20, 2023
1 parent d97e22d commit af7b2ca
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 34 deletions.
7 changes: 3 additions & 4 deletions bin/ap
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ portableReadLink() {
usage() {
local -r exit_code="${1:-0}"
(($# > 0)) && shift
# shellcheck disable=SC2015
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
local -r out=$(((exit_code != 0) + 1))

# NOTE: $'foo' is the escape sequence syntax of bash
local nl=$'\n' # new line
(($# > 0)) && redPrint "$*$nl" >"$out"
(($# > 0)) && redPrint "$*$nl" >&"$out"

cat >"$out" <<EOF
cat >&"$out" <<EOF
Usage: ${PROG} [OPTION]... [FILE]...
convert to Absolute Path.
Expand Down
7 changes: 3 additions & 4 deletions bin/c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ printErrorMsg() {
usage() {
local -r exit_code="${1:-0}"
(($# > 0)) && shift
# shellcheck disable=SC2015
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
local -r out=$(((exit_code != 0) + 1))

(($# > 0)) && printErrorMsg "$*" >"$out"
(($# > 0)) && printErrorMsg "$*" >&"$out"

cat >"$out" <<EOF
cat >&"$out" <<EOF
Usage: ${PROG} [OPTION]... [command [command_args ...]]
Run command and put output to system clipper.
If no command is specified, read from stdin(pipe).
Expand Down
7 changes: 3 additions & 4 deletions bin/cp-into-docker-run
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ portableReadLink() {
usage() {
local -r exit_code="${1:-0}"
(($# > 0)) && shift
# shellcheck disable=SC2015
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
local -r out=$(((exit_code != 0) + 1))

# NOTE: $'foo' is the escape sequence syntax of bash
local nl=$'\n' # new line
(($# > 0)) && redPrint "$*$nl" >"$out"
(($# > 0)) && redPrint "$*$nl" >&"$out"

cat >"$out" <<EOF
cat >&"$out" <<EOF
Usage: ${PROG} [OPTION]... command [command-args]...
Copy the command into docker container
Expand Down
12 changes: 6 additions & 6 deletions bin/find-in-jars
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ readonly PROG_VERSION='2.6.0-dev'
################################################################################

readonly red='\033[1;31m' normal='\033[0m'
readonly jar_color='\033[1;35m' sep_color='\033[1;32m'

# How to delete line with echo?
# https://unix.stackexchange.com/questions/26576
Expand Down Expand Up @@ -95,14 +94,13 @@ die() {
usage() {
local -r exit_code="${1:-0}"
(($# > 0)) && shift
# shellcheck disable=SC2015
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
local -r out=$(((exit_code != 0) + 1))

# NOTE: $'foo' is the escape sequence syntax of bash
local -r nl=$'\n' # new line
(($# > 0)) && redPrint "$*$nl" >"$out"
(($# > 0)) && redPrint "$*$nl" >&"$out"

cat >"$out" <<EOF
cat >&"$out" <<EOF
Usage: ${PROG} [OPTION]... PATTERN
Find files in the jar files under specified directory,
Expand Down Expand Up @@ -355,10 +353,12 @@ searchJarFiles() {
printf '%s\n' "$jar_files"
}

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

__outputResultOfJarFile() {
local jar_file="$1" file
# shellcheck disable=SC2206
local grep_opt_args=($regex_mode ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")
local grep_opt_args=("$regex_mode" ${ignore_case_option:-} ${grep_color_option:-} -- "$pattern")

if $only_print_file_name; then
local matched=false
Expand Down
7 changes: 3 additions & 4 deletions bin/rp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ portableRelPath() {
usage() {
local -r exit_code="${1:-0}"
(($# > 0)) && shift
# shellcheck disable=SC2015
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
local -r out=$(((exit_code != 0) + 1))

# NOTE: $'foo' is the escape sequence syntax of bash
local nl=$'\n' # new line
(($# > 0)) && redPrint "$*$nl" >"$out"
(($# > 0)) && redPrint "$*$nl" >&"$out"

cat >"$out" <<EOF
cat >&"$out" <<EOF
Usage: ${PROG} [OPTION]... [FILE]...
convert to Relative Path.
Expand Down
7 changes: 3 additions & 4 deletions bin/show-busy-java-threads
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,11 @@ printCallingCommandLine() {
usage() {
local -r exit_code="${1:-0}"
(($# > 0)) && shift
# shellcheck disable=SC2015
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
local -r out=$(((exit_code != 0) + 1))

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

cat >"$out" <<EOF
cat >&"$out" <<EOF
Usage: ${PROG} [OPTION]... [delay [count]]
Find out the highest cpu consumed threads of java processes,
and print the stack of these threads.
Expand Down
7 changes: 3 additions & 4 deletions bin/uq
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ convertHumanReadableSizeToSize() {
usage() {
local -r exit_code="${1:-0}"
(($# > 0)) && shift
# shellcheck disable=SC2015
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
local -r out=$(((exit_code != 0) + 1))

(($# > 0)) && redPrint "$*$nl" >"$out"
(($# > 0)) && redPrint "$*$nl" >&"$out"

cat >"$out" <<EOF
cat >&"$out" <<EOF
Usage: ${PROG} [OPTION]... [INPUT [OUTPUT]]
Filter lines from INPUT (or standard input), writing to OUTPUT (or standard output).
Same as \`uniq\` command in core utils,
Expand Down
7 changes: 3 additions & 4 deletions bin/xpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ readonly PROG_VERSION='2.6.0-dev'
usage() {
local -r exit_code="${1:-0}"
(($# > 0)) && shift
# shellcheck disable=SC2015
[ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout
local -r out=$(((exit_code != 0) + 1))

(($# > 0)) && printf '%s\n\n' "$*" >"$out"
(($# > 0)) && printf '%s\n\n' "$*" >&"$out"

cat <<EOF
cat >&"$out" <<EOF
Usage: ${PROG} [OPTION] [FILE]...
Open file in file explorer.
Example: ${PROG} file.txt
Expand Down

0 comments on commit af7b2ca

Please sign in to comment.