Skip to content

Commit

Permalink
refactor/robust(coat/a2l): use printf 💪 instead of echo; the `e…
Browse files Browse the repository at this point in the history
…cho` option(e.g. -e -n) may effect correctness 🐞 , `printf` is more robust 💪
  • Loading branch information
oldratlee committed Aug 28, 2023
1 parent 59044e2 commit 1dcead5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
34 changes: 15 additions & 19 deletions bin/a2l
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
# @Function
# echo each arguments on one line colorfully.
# print each arguments on one line colorfully.
#
# @Usage
# $ ./a2l arg1 arg2
Expand All @@ -19,26 +19,22 @@ 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

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 "$*"
}

redEcho() {
colorEcho 31 "$@"
if [ -t 1 ]; then
printf "\033[1;${color}m%s\033[0m\n" "$*"
else
printf '%s\n' "$*"
fi
}

usage() {
cat <<EOF
Usage: ${PROG} [OPTION]... ARG...
echo each arguments on one line colorfully.
print each arguments on one line colorfully.
Example:
${PROG} arg1 arg2
Expand All @@ -53,7 +49,7 @@ EOF
}

progVersion() {
echo "$PROG $PROG_VERSION"
printf '%s\n' "$PROG $PROG_VERSION"
exit
}

Expand Down Expand Up @@ -94,20 +90,20 @@ readonly args
# biz logic
################################################################################

readonly -a ECHO_COLORS=(33 35 36 31 32 37 34)
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
rotateColorEcho() {
rotateColorPrint() {
local message="$*"

# skip color for white space
if [[ "$message" =~ ^[[:space:]]*$ ]]; then
echo "$message"
printf '%s\n' "$message"
else
local color="${ECHO_COLORS[COUNT++ % ${#ECHO_COLORS[@]}]}"
colorEcho "$color" "$*"
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
colorPrint "$color" "$*"
fi
}

for a in ${args[@]:+"${args[@]}"}; do
rotateColorEcho "$a"
rotateColorPrint "$a"
done
16 changes: 6 additions & 10 deletions bin/coat
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@ set -eEuo pipefail
# check isatty in bash https://stackoverflow.com/questions/10022323
[ ! -t 1 ] && exec cat "$@"

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

readonly -a ECHO_COLORS=(33 35 36 31 32 37 34)
readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
rotateColorEcho() {
rotateColorPrint() {
local message="$*"

# skip color for white space
if [[ "$message" =~ ^[[:space:]]*$ ]]; then
echo "$message"
printf '%s\n' "$message"
else
local color="${ECHO_COLORS[COUNT++ % ${#ECHO_COLORS[@]}]}"
echo "${ec}[1;${color}m$message$eend"
local color="${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}"
printf "\033[1;${color}m%s\033[0m\n" "$message"
fi
}

# Bash read line does not read leading spaces
# https://stackoverflow.com/questions/29689172
cat "$@" | while IFS= read -r line; do
rotateColorEcho "$line"
rotateColorPrint "$line"
done

0 comments on commit 1dcead5

Please sign in to comment.