This repository has been archived on 2024-08-13. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
bunc/lib/color.sh

105 lines
1.8 KiB
Bash
Raw Normal View History

2018-01-05 21:02:41 -09:00
#!/bin/bash
# doc color {
#
# NAME
# color
#
# DESCRIPTION
# Sets color variables for easy color change
#
# USAGE
2018-01-06 00:26:35 -09:00
# _color <command> [<arg>]
2018-01-05 21:02:41 -09:00
#
# COMMAND
2018-01-06 00:26:35 -09:00
# fg <color>
2018-01-05 21:02:41 -09:00
# Sets foreground to defined color
#
2018-01-06 00:26:35 -09:00
# bfg <color>
2018-01-05 21:02:41 -09:00
# Set foreground to bold and defined color
#
2018-01-06 00:26:35 -09:00
# bg <color>
2018-01-05 21:02:41 -09:00
# Sets background to defined color
#
# rs
# Resets text to terminal default
#
2018-01-06 00:26:35 -09:00
# COLORS
2018-01-05 21:02:41 -09:00
# blk
# Color black
#
# red
# Color red
#
# grn
# Color green
#
# yel
# Color yellow
#
# blu
# Color blue
#
# mag
# Color magenta
#
# cyn
# Color cyan
#
# gry
# Color grey
2018-01-06 00:26:35 -09:00
#
2018-01-05 21:02:41 -09:00
# }
2018-01-06 00:26:35 -09:00
2018-01-05 21:02:41 -09:00
cmdoptArray=(fg bfg bg rs)
coloptArray=(blk red grn yel blu mag cyn gry)
_color_to_ansi() {
2018-01-06 00:26:35 -09:00
local COL=${1}
2018-01-05 21:02:41 -09:00
for no in {0..7}; do
if [ "${COL}" == "${coloptArray[${no}]}" ]; then
echo $(( ${no} + 30 ))
fi
done
}
2018-01-06 00:26:35 -09:00
_color() {
local CMD=${1}
local COL=${2}
2018-01-05 21:02:41 -09:00
# Sanity check for COMMAND argument
2018-01-06 00:26:35 -09:00
if [ -z ${1+x} ]; then
_msg ECHO "_color(): Command not defined"
elif ! _if_array_contains "${CMD}" "${cmdoptArray[@]}"; then
2018-01-05 21:02:41 -09:00
_msg ECHO "_color(): ${CMD} not a command"
fi
# Sanity check for COLOR argument
2018-01-06 00:26:35 -09:00
[ ! "${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"
2018-01-05 21:02:41 -09:00
fi
2018-01-06 00:26:35 -09:00
}
2018-01-05 21:02:41 -09:00
# Converts color to associated ANSI value
ANSI="$(_color_to_ansi ${COL})"
case ${CMD} in
bfg)
2018-01-06 00:26:35 -09:00
local BOLD="1;"
2018-01-05 21:02:41 -09:00
;;
bg)
2018-01-06 00:26:35 -09:00
local ANSI=$(( ${ANSI} + 10 ))
2018-01-05 21:02:41 -09:00
;;
rs)
2018-01-06 00:26:35 -09:00
local ANSI=0
2018-01-05 21:02:41 -09:00
;;
esac
echo -e "\033[${BOLD}${ANSI}m"
}