diff --git a/bin/ap b/bin/ap index 901d8055..0905e658 100755 --- a/bin/ap +++ b/bin/ap @@ -21,25 +21,24 @@ readonly PROG_VERSION='2.5.0-dev' # util functions ################################################################################ -# 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 - -colorEcho() { +colorPrint() { local color="$1" shift # check isatty in bash https://stackoverflow.com/questions/10022323 # if stdout is console, turn on color output. - [ -t 1 ] && echo "${ec}[1;${color}m$*${eend}" || echo "$*" + if [ -t 1 ]; then + printf "\033[1;${color}m%s\033[0m\n" "$*" + else + printf '%s\n' "$*" + fi } -redEcho() { - colorEcho 31 "$@" +redPrint() { + colorPrint 31 "$*" } die() { - redEcho "Error: $*" 1>&2 + redPrint "Error: $*" >&2 exit 1 } @@ -54,10 +53,15 @@ portableReadLink() { readlink -f "$file" ;; Darwin*) + local py_args=(-c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file") if command -v greadlink >/dev/null; then greadlink -f "$file" + elif command -v python3 >/dev/null; then + python3 "${py_args[@]}" + elif command -v python >/dev/null; then + python "${py_args[@]}" else - python -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "$file" + die "fail to find command(greadlink/python3/python) to get absolute path!" fi ;; *) @@ -72,10 +76,12 @@ usage() { # shellcheck disable=SC2015 [ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout - (($# > 0)) && redEcho "$*$nl" >$out + # NOTE: $'foo' is the escape sequence syntax of bash + local nl=$'\n' # new line + (($# > 0)) && redPrint "$*$nl" >$out cat >$out <&2 + has_error=true + fi done + +if $has_error; then + exit 1 +fi diff --git a/bin/rp b/bin/rp index 01ad6c47..3ab3a3c4 100755 --- a/bin/rp +++ b/bin/rp @@ -21,21 +21,51 @@ readonly PROG_VERSION='2.5.0-dev' # util functions ################################################################################ -# 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 - -colorEcho() { +colorPrint() { local color="$1" shift # check isatty in bash https://stackoverflow.com/questions/10022323 # if stdout is console, turn on color output. - [ -t 1 ] && echo "${ec}[1;${color}m$*${eend}" || echo "$*" + if [ -t 1 ]; then + printf "\033[1;${color}m%s\033[0m\n" "$*" + else + printf '%s\n' "$*" + fi +} + +redPrint() { + colorPrint 31 "$@" +} + +die() { + redPrint "Error: $*" >&2 + exit 1 } -redEcho() { - colorEcho 31 "$@" +portableRelPath() { + local file="$1" relTo="$2" uname + + uname="$(uname)" + case "$uname" in + Linux* | CYGWIN* | MINGW*) + realpath "$f" --relative-to="$relTo" + ;; + Darwin*) + local py_args=(-c 'import os, sys; print(os.path.relpath(sys.argv[1], sys.argv[2]))' "$file" "$relTo") + if command -v grealpath >/dev/null; then + grealpath "$f" --relative-to="$relTo" + elif command -v python3 >/dev/null; then + python3 "${py_args[@]}" + elif command -v python >/dev/null; then + python "${py_args[@]}" + else + die "fail to find command(grealpath/python3/python) to get relative path!" + fi + ;; + *) + die "NOT support uname($uname)!" + ;; + esac } usage() { @@ -44,15 +74,18 @@ usage() { # shellcheck disable=SC2015 [ "$exit_code" != 0 ] && local -r out=/dev/stderr || local -r out=/dev/stdout - (($# > 0)) && redEcho "$*$nl" >$out + # NOTE: $'foo' is the escape sequence syntax of bash + local nl=$'\n' # new line + (($# > 0)) && redPrint "$*$nl" >$out cat >$out <&2 - exit 1 -} +[ "${#files[@]}" -eq 0 ] && die "NO argument!" if [ "${#files[@]}" -eq 1 ]; then relativeTo=. @@ -113,12 +142,25 @@ else fi [ -f "$relativeTo" ] && relativeTo="$(dirname "$relativeTo")" +[ -e "$relativeTo" ] || die "relativeTo dir($relativeTo) does NOT exists!" + readonly files relativeTo +################################################################################ +# biz logic +################################################################################ + +has_error=false + for f in "${files[@]}"; do - ! [ -e "$f" ] && { - echo "$f does not exists!" - continue - } - realpath "$f" --relative-to="$relativeTo" + if [ -e "$f" ]; then + portableRelPath "$f" "$relativeTo" + else + redPrint "error: $f does not exists!" >&2 + has_error=true + fi done + +if $has_error; then + exit 1 +fi