[ RGB Out ] - Detect if in tty

This commit is contained in:
Price Hiller 2022-01-17 10:25:33 -06:00
parent 0cbcda179f
commit 9e9e73400b
2 changed files with 184 additions and 180 deletions

View File

@ -31,7 +31,11 @@ echo_rgb() {
green="${3}"
blue="${4}"
if [ -t 1 ]; then
printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}"
else
printf "%s\n" "${input}"
fi
}
log() {

View File

@ -1,7 +1,5 @@
#!/bin/bash
rgb() {
# Echo a color printf to the terminal awaiting an actual echo to be colored
# should be used with reset after coloring a string
@ -28,7 +26,6 @@ rgb () {
printf "\e[0;38;2;%s;%s;%sm" "${red}" "${green}" "${blue}"
}
echo_rgb() {
# Echo a colored string to the terminal based on rgb values
#
@ -69,7 +66,6 @@ echo_rgb() {
local bg_green
local bg_blue
input="${1}"
red="${2}"
green="${3}"
@ -79,17 +75,18 @@ echo_rgb() {
bg_blue="${7}"
for num in "${@:2}"; do
[[ ! "${num}" =~ [0-9] ]] \
&& echo "Given RGB value was not a number, received ${num}" \
&& return 1
[[ "${num}" -gt 255 ]] \
&& echo "Given RGB value must be less than 255, received ${num}" \
&& return 1
[[ "${num}" -lt 0 ]] \
&& echo "Given RGB value must be more than 0, received ${num}" \
&& return 1
[[ ! "${num}" =~ [0-9] ]] &&
echo "Given RGB value was not a number, received ${num}" &&
return 1
[[ "${num}" -gt 255 ]] &&
echo "Given RGB value must be less than 255, received ${num}" &&
return 1
[[ "${num}" -lt 0 ]] &&
echo "Given RGB value must be more than 0, received ${num}" &&
return 1
done
if [ -t 1 ]; then
if [ -n "${5}" ]; then
[[ -z "${6}" ]] && echo "A value must be passed for bg_green" && return 1
[[ -z "${7}" ]] && echo "A value must be passed for bg_blue" && return 1
@ -98,6 +95,9 @@ echo_rgb() {
else
printf "\033[0;38;2;%s;%s;%sm%s\033[m\n" "${red}" "${green}" "${blue}" "${input}"
fi
else
printf "%s\n" "${input}"
fi
return 0
}