[ 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

@ -1,104 +1,108 @@
#!/bin/bash
echo_rgb() {
# Echo a colored string to the terminal based on rgb values
#
# Positional Arguments:
#
# message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout
# red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true>
# - The blue value from 0 to 255
#
# Usage:
# echo_rgb "Yep" 10 8 30
#
# POSIX Compliant:
# N/A
#
# Echo a colored string to the terminal based on rgb values
#
# Positional Arguments:
#
# message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout
# red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true>
# - The blue value from 0 to 255
#
# Usage:
# echo_rgb "Yep" 10 8 30
#
# POSIX Compliant:
# N/A
#
local red
local green
local blue
local input
local red
local green
local blue
local input
input="${1}"
red="${2}"
green="${3}"
blue="${4}"
input="${1}"
red="${2}"
green="${3}"
blue="${4}"
printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}"
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() {
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
# line_number <type: int> <position: 3> <required: false>
# - The line number of the calling function (${LINNO})
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
# line_number <type: int> <position: 3> <required: false>
# - The line number of the calling function (${LINNO})
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Set debug status depending if a global debug variable has been set to either 1 or 0
local debug
if [ ${DEBUG} ]; then
debug=${DEBUG}
else
debug=0
fi
# Set debug status depending if a global debug variable has been set to either 1 or 0
local debug
if [ ${DEBUG} ]; then
debug=${DEBUG}
else
debug=0
fi
local FORMAT
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
local FORMAT
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
# Convert the level to uppercase
local level
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
# Convert the level to uppercase
local level
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
local message
message="${2}"
local message
message="${2}"
case "${level}" in
case "${level}" in
INFO)
# Output all info log levels to stdout
printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1
return 0
;;
# Output all info log levels to stdout
printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1
return 0
;;
WARN | WARNING)
# Output all warning log levels to stdout
printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1
return 0
;;
# Output all warning log levels to stdout
printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1
return 0
;;
DEBUG)
# Output all debug log levels to stdout
if [ "${DEBUG}" ]; then
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
fi
return 0
;;
# Output all debug log levels to stdout
if [ "${DEBUG}" ]; then
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
fi
return 0
;;
ERROR)
# Output all error log levels to stderr
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
return 0
;;
# Output all error log levels to stderr
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
return 0
;;
# Further log levels can be added by extending this switch statement with more comparisons
*) # Default case, no matches
# Returns non-zero code as an improper log option was passed, this helps with using `set -e`
printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2
return 1
;;
esac
# Returns non-zero code as an improper log option was passed, this helps with using `set -e`
printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2
return 1
;;
esac
}

View File

@ -1,115 +1,115 @@
#!/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
#
# Positional Arguments:
# red <type: int> <position: 1> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 2> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 2> <required: true>
# - The blue value from 0 to 255
#
# Usage:
# echo_rgb 10 80 3 && echo "hello" && reset
#
# POSIX Compliant:
# N/A
#
red="${1}"
green="${2}"
blue="${3}"
rgb () {
# Echo a color printf to the terminal awaiting an actual echo to be colored
# should be used with reset after coloring a string
#
# Positional Arguments:
# red <type: int> <position: 1> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 2> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 2> <required: true>
# - The blue value from 0 to 255
#
# Usage:
# echo_rgb 10 80 3 && echo "hello" && reset
#
# POSIX Compliant:
# N/A
#
red="${1}"
green="${2}"
blue="${3}"
printf "\e[0;38;2;%s;%s;%sm" "${red}" "${green}" "${blue}"
printf "\e[0;38;2;%s;%s;%sm" "${red}" "${green}" "${blue}"
}
echo_rgb() {
# Echo a colored string to the terminal based on rgb values
#
# NOTE: This function will only truly work with terminals that support TRUE COLOR, see:
# https://gist.github.com/XVilka/8346728
#
# Positional Arguments:
#
# message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout
# red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true>
# - The blue value from 0 to 255
# bg_red <type: int> <position: 5> <required: false>
# - The background red value from 0 to 255
# bg_green <type: int> <position: 6> <required: false>
# - The background green value from 0 to 255
# bg_blue <type: int> <position: 7> <required: false>
# - The background blue value from 0 to 255
#
# Usage:
# echo_rgb "Yep" 10 80 30
# echo_rgb "DESTROY MY EYES" 255 0 255 0 255 0
#
# POSIX Compliant:
# N/A
#
# Echo a colored string to the terminal based on rgb values
#
# NOTE: This function will only truly work with terminals that support TRUE COLOR, see:
# https://gist.github.com/XVilka/8346728
#
# Positional Arguments:
#
# message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout
# red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true>
# - The blue value from 0 to 255
# bg_red <type: int> <position: 5> <required: false>
# - The background red value from 0 to 255
# bg_green <type: int> <position: 6> <required: false>
# - The background green value from 0 to 255
# bg_blue <type: int> <position: 7> <required: false>
# - The background blue value from 0 to 255
#
# Usage:
# echo_rgb "Yep" 10 80 30
# echo_rgb "DESTROY MY EYES" 255 0 255 0 255 0
#
# POSIX Compliant:
# N/A
#
local red
local green
local blue
local input
local red
local green
local blue
local input
local bg_red
local bg_green
local bg_blue
local bg_red
local bg_green
local bg_blue
input="${1}"
red="${2}"
green="${3}"
blue="${4}"
bg_red="${5}"
bg_green="${6}"
bg_blue="${7}"
input="${1}"
red="${2}"
green="${3}"
blue="${4}"
bg_red="${5}"
bg_green="${6}"
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
done
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
done
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
printf "\033[38;2;%s;%s;%s;48;2;%s;%s;%sm%s\033[m\n" \
"${red}" "${green}" "${blue}" "${bg_red}" "${bg_green}" "${bg_blue}" "${input}"
else
printf "\033[0;38;2;%s;%s;%sm%s\033[m\n" "${red}" "${green}" "${blue}" "${input}"
fi
return 0
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
printf "\033[38;2;%s;%s;%s;48;2;%s;%s;%sm%s\033[m\n" \
"${red}" "${green}" "${blue}" "${bg_red}" "${bg_green}" "${bg_blue}" "${input}"
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
}
reset() {
# Reset colors in the terminal to default
#
# Usage:
# reset
#
# POSIX Compliant:
# N/A
#
# Reset colors in the terminal to default
#
# Usage:
# reset
#
# POSIX Compliant:
# N/A
#
printf "\e[m\n"
printf "\e[m\n"
}