Fixed faulty sanity check in _color()

This commit is contained in:
ayakael 2018-01-06 00:26:35 -09:00
parent 79bfb447ea
commit 5f446594ee
No known key found for this signature in database
GPG key ID: 575626A4AE5F4026
2 changed files with 25 additions and 26 deletions

View file

@ -9,22 +9,22 @@
# Sets color variables for easy color change # Sets color variables for easy color change
# #
# USAGE # USAGE
# _color <command> [<color>] # _color <command> [<arg>]
# #
# COMMAND # COMMAND
# fg # fg <color>
# Sets foreground to defined color # Sets foreground to defined color
# #
# bfg # bfg <color>
# Set foreground to bold and defined color # Set foreground to bold and defined color
# #
# bg # bg <color>
# Sets background to defined color # Sets background to defined color
# #
# rs # rs
# Resets text to terminal default # Resets text to terminal default
# #
# COLOR # COLORS
# blk # blk
# Color black # Color black
# #
@ -48,57 +48,55 @@
# #
# gry # gry
# Color grey # Color grey
#
# } # }
cmdoptArray=(fg bfg bg rs) cmdoptArray=(fg bfg bg rs)
coloptArray=(blk red grn yel blu mag cyn gry) coloptArray=(blk red grn yel blu mag cyn gry)
_color_to_ansi() { _color_to_ansi() {
COL=${1} local COL=${1}
for no in {0..7}; do for no in {0..7}; do
if [ "${COL}" == "${coloptArray[${no}]}" ]; then if [ "${COL}" == "${coloptArray[${no}]}" ]; then
echo $(( ${no} + 30 )) echo $(( ${no} + 30 ))
fi fi
done done
} }
_color() {
CMD=${1}
COL=${2}
_color() {
local CMD=${1}
local COL=${2}
# Sanity check for COMMAND argument # Sanity check for COMMAND argument
for cmdopt in ${cmdoptArray[@]}; do if [ -z ${1+x} ]; then
if [ "${cmdopt}" == "${CMD}" ]; then _msg ECHO "_color(): Command not defined"
CMD_EXIST=true elif ! _if_array_contains "${CMD}" "${cmdoptArray[@]}"; then
fi
done
if ! ${CMD_EXIST}; then
_msg ECHO "_color(): ${CMD} not a command" _msg ECHO "_color(): ${CMD} not a command"
fi fi
# Sanity check for COLOR argument # Sanity check for COLOR argument
for colopt in ${coloptArray[@]}; do [ ! "${CMD}" == "rs" ] && {
if [ "${colopt}" == "${COL}" ]; then if [ -z ${2+x} ]; then
OPT_EXIT=true _msg ECHO "_color(): Color not defined"
elif ! _if_array_contains "${COL}" "${coloptArray[@]}"; then
_msg ECHO "_color(): ${COL} not a color"
fi fi
done }
if ! ${OPT_EXIT}; then
_msg ECHO "_color(): ${COL} not a color"
fi
# Converts color to associated ANSI value # Converts color to associated ANSI value
ANSI="$(_color_to_ansi ${COL})" ANSI="$(_color_to_ansi ${COL})"
case ${CMD} in case ${CMD} in
bfg) bfg)
BOLD="1;" local BOLD="1;"
;; ;;
bg) bg)
ANSI=$(( ${ANSI} + 10 )) local ANSI=$(( ${ANSI} + 10 ))
;; ;;
rs) rs)
ANSI=0 local ANSI=0
;; ;;
esac esac

View file

@ -2,10 +2,11 @@
source ../lib/msg.sh source ../lib/msg.sh
source ../lib/color.sh source ../lib/color.sh
source ../lib/if.sh
_msg ECHO "Begin color.sh test" _msg ECHO "Begin color.sh test"
for cmd in fg bfg bg; do for cmd in fg bfg bg; do
for col in ${coloptArray[@]}; do for col in blk red grn yel blu mag cyn gry; do
echo "Testing _color $(_color ${cmd} ${col})${cmd} ${col}$(_color rs)" echo "Testing _color $(_color ${cmd} ${col})${cmd} ${col}$(_color rs)"
done done
done done