Skip to content

Commit

Permalink
refactor(awl): merge function colorPrint into rotateColorPrint 🛠️
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jan 24, 2024
1 parent 5b25e67 commit 176443e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
23 changes: 6 additions & 17 deletions bin/a2l
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ readonly PROG_VERSION='2.6.0-dev'
# util functions
################################################################################

colorPrint() {
local color=$1
shift
# 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
}

usage() {
cat <<EOF
Usage: $PROG [OPTION]... ARG...
Expand Down Expand Up @@ -95,13 +82,15 @@ readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
rotateColorPrint() {
local content=$*

# skip color for white space
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
# - if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
# check isatty in bash https://stackoverflow.com/questions/10022323
# - skip color for white space
if [[ ! -t 1 || $content =~ ^[[:space:]]*$ ]]; then
printf '%s\n' "$content"
else
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
colorPrint "$color" "$content"
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
fi
}

Expand Down
2 changes: 1 addition & 1 deletion bin/coat
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COUNT=0
rotateColorPrint() {
local content=$*
# skip color for white space
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
if [[ $content =~ ^[[:space:]]*$ ]]; then
printf %s "$content"
else
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
Expand Down
2 changes: 1 addition & 1 deletion bin/taoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COUNT=0
rotateColorPrint() {
local content=$*
# skip color for white space
if [[ "$content" =~ ^[[:space:]]*$ ]]; then
if [[ $content =~ ^[[:space:]]*$ ]]; then
printf %s "$content"
else
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
Expand Down

0 comments on commit 176443e

Please sign in to comment.