Fixed faulty sanity check in _color()
This commit is contained in:
parent
79bfb447ea
commit
5f446594ee
2 changed files with 25 additions and 26 deletions
46
lib/color.sh
46
lib/color.sh
|
@ -9,22 +9,22 @@
|
|||
# Sets color variables for easy color change
|
||||
#
|
||||
# USAGE
|
||||
# _color <command> [<color>]
|
||||
# _color <command> [<arg>]
|
||||
#
|
||||
# COMMAND
|
||||
# fg
|
||||
# fg <color>
|
||||
# Sets foreground to defined color
|
||||
#
|
||||
# bfg
|
||||
# bfg <color>
|
||||
# Set foreground to bold and defined color
|
||||
#
|
||||
# bg
|
||||
# bg <color>
|
||||
# Sets background to defined color
|
||||
#
|
||||
# rs
|
||||
# Resets text to terminal default
|
||||
#
|
||||
# COLOR
|
||||
# COLORS
|
||||
# blk
|
||||
# Color black
|
||||
#
|
||||
|
@ -48,57 +48,55 @@
|
|||
#
|
||||
# gry
|
||||
# Color grey
|
||||
#
|
||||
# }
|
||||
|
||||
|
||||
cmdoptArray=(fg bfg bg rs)
|
||||
coloptArray=(blk red grn yel blu mag cyn gry)
|
||||
|
||||
_color_to_ansi() {
|
||||
COL=${1}
|
||||
local COL=${1}
|
||||
for no in {0..7}; do
|
||||
if [ "${COL}" == "${coloptArray[${no}]}" ]; then
|
||||
echo $(( ${no} + 30 ))
|
||||
fi
|
||||
done
|
||||
}
|
||||
_color() {
|
||||
CMD=${1}
|
||||
COL=${2}
|
||||
|
||||
_color() {
|
||||
local CMD=${1}
|
||||
local COL=${2}
|
||||
# Sanity check for COMMAND argument
|
||||
for cmdopt in ${cmdoptArray[@]}; do
|
||||
if [ "${cmdopt}" == "${CMD}" ]; then
|
||||
CMD_EXIST=true
|
||||
fi
|
||||
done
|
||||
if ! ${CMD_EXIST}; then
|
||||
if [ -z ${1+x} ]; then
|
||||
_msg ECHO "_color(): Command not defined"
|
||||
elif ! _if_array_contains "${CMD}" "${cmdoptArray[@]}"; then
|
||||
_msg ECHO "_color(): ${CMD} not a command"
|
||||
fi
|
||||
|
||||
# Sanity check for COLOR argument
|
||||
for colopt in ${coloptArray[@]}; do
|
||||
if [ "${colopt}" == "${COL}" ]; then
|
||||
OPT_EXIT=true
|
||||
fi
|
||||
done
|
||||
if ! ${OPT_EXIT}; then
|
||||
[ ! "${CMD}" == "rs" ] && {
|
||||
if [ -z ${2+x} ]; then
|
||||
_msg ECHO "_color(): Color not defined"
|
||||
elif ! _if_array_contains "${COL}" "${coloptArray[@]}"; then
|
||||
_msg ECHO "_color(): ${COL} not a color"
|
||||
fi
|
||||
}
|
||||
|
||||
# Converts color to associated ANSI value
|
||||
ANSI="$(_color_to_ansi ${COL})"
|
||||
|
||||
case ${CMD} in
|
||||
bfg)
|
||||
BOLD="1;"
|
||||
local BOLD="1;"
|
||||
;;
|
||||
|
||||
bg)
|
||||
ANSI=$(( ${ANSI} + 10 ))
|
||||
local ANSI=$(( ${ANSI} + 10 ))
|
||||
;;
|
||||
|
||||
rs)
|
||||
ANSI=0
|
||||
local ANSI=0
|
||||
;;
|
||||
esac
|
||||
|
||||
|
|
|
@ -2,10 +2,11 @@
|
|||
|
||||
source ../lib/msg.sh
|
||||
source ../lib/color.sh
|
||||
source ../lib/if.sh
|
||||
|
||||
_msg ECHO "Begin color.sh test"
|
||||
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)"
|
||||
done
|
||||
done
|
||||
|
|
Reference in a new issue