Skip to content

Commit

Permalink
refactor/robust(`tcp-connection-state-counter/console-text-color-them…
Browse files Browse the repository at this point in the history
…es.sh`): use `printf` 💪 instead of `echo`; use `if-else` instead of `&&-||`

NOTE:

- the `echo` option(e.g. -e -n) may effect correctness, `printf` is more robust 💪
- about `&&-||` see shell check:
  https://www.shellcheck.net/wiki/SC2015
  • Loading branch information
oldratlee committed Aug 30, 2023
1 parent 3bec3ab commit 6463f9d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
3 changes: 2 additions & 1 deletion bin/tcp-connection-state-counter
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ EOF
}

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

Expand All @@ -58,6 +58,7 @@ done
# On MacOS, netstat need to using -p tcp to get only tcp output.
uname | grep Darwin -q && option_for_mac="-ptcp"

# shellcheck disable=SC2086
netstat -tna ${option_for_mac:-} | awk 'NR > 2 {
++s[$NF]
}
Expand Down
47 changes: 28 additions & 19 deletions lib/console-text-color-themes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,55 +37,64 @@ colorEcho() {
local combination="$1"
shift 1

[ -t 1 ] && echo "${_ctct_ec}[${combination}m$*$_ctct_eend" || echo "$*"
if [ -t 1 ]; then
echo "${_ctct_ec}[${combination}m$*$_ctct_eend"
else
echo "$*"
fi
}

colorEchoWithoutNewLine() {
local combination="$1"
shift 1

[ -t 1 ] && echo -n "${_ctct_ec}[${combination}m$*$_ctct_eend" || echo -n "$*"
if [ -t 1 ]; then
echo -n "${_ctct_ec}[${combination}m$*$_ctct_eend"
else
echo -n "$*"
fi
}

# if not directly run this script(use as lib), just export 2 helper functions,
# and do NOT print anything.
[ "$_ctct_is_direct_run" == "true" ] && {
[ "$_ctct_is_direct_run" == true ] && {
for style in 0 1 2 3 4 5 6 7; do
for fg in 30 31 32 33 34 35 36 37; do
for bg in 40 41 42 43 44 45 46 47; do
combination="${style};${fg};${bg}"
colorEchoWithoutNewLine "$combination" "$combination"
echo -n " "
printf ' '
done
echo
done
echo
done

echo "Code sample to print color text:"
echo 'Code sample to print color text:'

echo -n ' echo -e "\033['
colorEchoWithoutNewLine "3;35;40" "1;36;41"
echo -n "m"
colorEchoWithoutNewLine "0;32;40" "Sample Text"
echo "\033[0m\""
colorEchoWithoutNewLine '3;35;40' '1;36;41'
echo -n m
colorEchoWithoutNewLine '0;32;40' 'Sample Text'
echo '\033[0m"'

echo -n " echo \$'\033["
colorEchoWithoutNewLine "3;35;40" "1;36;41"
colorEchoWithoutNewLine '3;35;40' '1;36;41'
echo -n "m'\""
colorEchoWithoutNewLine "0;32;40" "Sample Text"
colorEchoWithoutNewLine '0;32;40' 'Sample Text'
echo "\"$'\033[0m'"
echo " # NOTE: $'foo' is the escape sequence syntax of bash, safer escape"

echo "Output of above code:"
echo " ${_ctct_ec}[1;36;41mSample Text${_ctct_eend}"
echo 'Output of above code:'
echo -n ' '
colorEcho '1;36;41' 'Sample Text'
echo
echo "If you are going crazy to write text in escapes string like me,"
echo "you can use colorEcho and colorEchoWithoutNewLine function in this script."
echo 'If you are going crazy to write text in escapes string like me,'
echo 'you can use colorEcho and colorEchoWithoutNewLine function in this script.'
echo
echo "Code sample to print color text:"
echo 'Code sample to print color text:'
echo ' colorEcho "1;36;41" "Sample Text"'
echo "Output of above code:"
echo -n " "
colorEcho "1;36;41" "Sample Text"
echo 'Output of above code:'
echo -n ' '
colorEcho '1;36;41' 'Sample Text'
}

0 comments on commit 6463f9d

Please sign in to comment.