[ RGB Out ] - Detect if in tty
This commit is contained in:
parent
0cbcda179f
commit
9e9e73400b
@ -1,104 +1,108 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
echo_rgb() {
|
echo_rgb() {
|
||||||
# Echo a colored string to the terminal based on rgb values
|
# Echo a colored string to the terminal based on rgb values
|
||||||
#
|
#
|
||||||
# Positional Arguments:
|
# Positional Arguments:
|
||||||
#
|
#
|
||||||
# message <type: string> <position: 1> <required: true>
|
# message <type: string> <position: 1> <required: true>
|
||||||
# - The message to be printed to stdout
|
# - The message to be printed to stdout
|
||||||
# red <type: int> <position: 2> <required: true>
|
# red <type: int> <position: 2> <required: true>
|
||||||
# - The red value from 0 to 255
|
# - The red value from 0 to 255
|
||||||
# green <type: int> <position: 3> <required: true>
|
# green <type: int> <position: 3> <required: true>
|
||||||
# - The green value from 0 to 255
|
# - The green value from 0 to 255
|
||||||
# blue <type: int> <position: 4> <required: true>
|
# blue <type: int> <position: 4> <required: true>
|
||||||
# - The blue value from 0 to 255
|
# - The blue value from 0 to 255
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# echo_rgb "Yep" 10 8 30
|
# echo_rgb "Yep" 10 8 30
|
||||||
#
|
#
|
||||||
# POSIX Compliant:
|
# POSIX Compliant:
|
||||||
# N/A
|
# N/A
|
||||||
#
|
#
|
||||||
|
|
||||||
local red
|
local red
|
||||||
local green
|
local green
|
||||||
local blue
|
local blue
|
||||||
local input
|
local input
|
||||||
|
|
||||||
input="${1}"
|
input="${1}"
|
||||||
red="${2}"
|
red="${2}"
|
||||||
green="${3}"
|
green="${3}"
|
||||||
blue="${4}"
|
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() {
|
log() {
|
||||||
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
|
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# level <type: string> <position: 1> <required: true>
|
# level <type: string> <position: 1> <required: true>
|
||||||
# - The log level, defined within a case check in this function
|
# - The log level, defined within a case check in this function
|
||||||
# message <type: string> <position: 2> <required: true>
|
# message <type: string> <position: 2> <required: true>
|
||||||
# - The info message
|
# - The info message
|
||||||
# line_number <type: int> <position: 3> <required: false>
|
# line_number <type: int> <position: 3> <required: false>
|
||||||
# - The line number of the calling function (${LINNO})
|
# - The line number of the calling function (${LINNO})
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# log "info" "Could not find that directory"
|
# log "info" "Could not find that directory"
|
||||||
#
|
#
|
||||||
# POSIX Compliant:
|
# POSIX Compliant:
|
||||||
# Yes
|
# Yes
|
||||||
#
|
#
|
||||||
|
|
||||||
# Set debug status depending if a global debug variable has been set to either 1 or 0
|
# Set debug status depending if a global debug variable has been set to either 1 or 0
|
||||||
local debug
|
local debug
|
||||||
if [ ${DEBUG} ]; then
|
if [ ${DEBUG} ]; then
|
||||||
debug=${DEBUG}
|
debug=${DEBUG}
|
||||||
else
|
else
|
||||||
debug=0
|
debug=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local FORMAT
|
local FORMAT
|
||||||
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
|
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
|
||||||
|
|
||||||
# Convert the level to uppercase
|
# Convert the level to uppercase
|
||||||
local level
|
local level
|
||||||
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
|
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
|
||||||
|
|
||||||
local message
|
local message
|
||||||
message="${2}"
|
message="${2}"
|
||||||
|
|
||||||
case "${level}" in
|
case "${level}" in
|
||||||
INFO)
|
INFO)
|
||||||
# Output all info log levels to stdout
|
# Output all info log levels to stdout
|
||||||
printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1
|
printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
WARN | WARNING)
|
WARN | WARNING)
|
||||||
# Output all warning log levels to stdout
|
# Output all warning log levels to stdout
|
||||||
printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1
|
printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
DEBUG)
|
DEBUG)
|
||||||
# Output all debug log levels to stdout
|
# Output all debug log levels to stdout
|
||||||
if [ "${DEBUG}" ]; then
|
if [ "${DEBUG}" ]; then
|
||||||
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
|
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
|
||||||
fi
|
fi
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
ERROR)
|
ERROR)
|
||||||
# Output all error log levels to stderr
|
# Output all error log levels to stderr
|
||||||
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
|
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
|
||||||
return 0
|
return 0
|
||||||
;;
|
;;
|
||||||
# Further log levels can be added by extending this switch statement with more comparisons
|
# Further log levels can be added by extending this switch statement with more comparisons
|
||||||
|
|
||||||
*) # Default case, no matches
|
*) # Default case, no matches
|
||||||
# Returns non-zero code as an improper log option was passed, this helps with using `set -e`
|
# 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
|
printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
@ -1,115 +1,115 @@
|
|||||||
#!/bin/bash
|
#!/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 () {
|
printf "\e[0;38;2;%s;%s;%sm" "${red}" "${green}" "${blue}"
|
||||||
# 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}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
echo_rgb() {
|
echo_rgb() {
|
||||||
# Echo a colored string to the terminal based on rgb values
|
# 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:
|
# NOTE: This function will only truly work with terminals that support TRUE COLOR, see:
|
||||||
# https://gist.github.com/XVilka/8346728
|
# https://gist.github.com/XVilka/8346728
|
||||||
#
|
#
|
||||||
# Positional Arguments:
|
# Positional Arguments:
|
||||||
#
|
#
|
||||||
# message <type: string> <position: 1> <required: true>
|
# message <type: string> <position: 1> <required: true>
|
||||||
# - The message to be printed to stdout
|
# - The message to be printed to stdout
|
||||||
# red <type: int> <position: 2> <required: true>
|
# red <type: int> <position: 2> <required: true>
|
||||||
# - The red value from 0 to 255
|
# - The red value from 0 to 255
|
||||||
# green <type: int> <position: 3> <required: true>
|
# green <type: int> <position: 3> <required: true>
|
||||||
# - The green value from 0 to 255
|
# - The green value from 0 to 255
|
||||||
# blue <type: int> <position: 4> <required: true>
|
# blue <type: int> <position: 4> <required: true>
|
||||||
# - The blue value from 0 to 255
|
# - The blue value from 0 to 255
|
||||||
# bg_red <type: int> <position: 5> <required: false>
|
# bg_red <type: int> <position: 5> <required: false>
|
||||||
# - The background red value from 0 to 255
|
# - The background red value from 0 to 255
|
||||||
# bg_green <type: int> <position: 6> <required: false>
|
# bg_green <type: int> <position: 6> <required: false>
|
||||||
# - The background green value from 0 to 255
|
# - The background green value from 0 to 255
|
||||||
# bg_blue <type: int> <position: 7> <required: false>
|
# bg_blue <type: int> <position: 7> <required: false>
|
||||||
# - The background blue value from 0 to 255
|
# - The background blue value from 0 to 255
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# echo_rgb "Yep" 10 80 30
|
# echo_rgb "Yep" 10 80 30
|
||||||
# echo_rgb "DESTROY MY EYES" 255 0 255 0 255 0
|
# echo_rgb "DESTROY MY EYES" 255 0 255 0 255 0
|
||||||
#
|
#
|
||||||
# POSIX Compliant:
|
# POSIX Compliant:
|
||||||
# N/A
|
# N/A
|
||||||
#
|
#
|
||||||
|
|
||||||
local red
|
local red
|
||||||
local green
|
local green
|
||||||
local blue
|
local blue
|
||||||
local input
|
local input
|
||||||
|
|
||||||
local bg_red
|
local bg_red
|
||||||
local bg_green
|
local bg_green
|
||||||
local bg_blue
|
local bg_blue
|
||||||
|
|
||||||
|
input="${1}"
|
||||||
|
red="${2}"
|
||||||
|
green="${3}"
|
||||||
|
blue="${4}"
|
||||||
|
bg_red="${5}"
|
||||||
|
bg_green="${6}"
|
||||||
|
bg_blue="${7}"
|
||||||
|
|
||||||
input="${1}"
|
for num in "${@:2}"; do
|
||||||
red="${2}"
|
[[ ! "${num}" =~ [0-9] ]] &&
|
||||||
green="${3}"
|
echo "Given RGB value was not a number, received ${num}" &&
|
||||||
blue="${4}"
|
return 1
|
||||||
bg_red="${5}"
|
[[ "${num}" -gt 255 ]] &&
|
||||||
bg_green="${6}"
|
echo "Given RGB value must be less than 255, received ${num}" &&
|
||||||
bg_blue="${7}"
|
return 1
|
||||||
|
[[ "${num}" -lt 0 ]] &&
|
||||||
|
echo "Given RGB value must be more than 0, received ${num}" &&
|
||||||
|
return 1
|
||||||
|
done
|
||||||
|
|
||||||
for num in "${@:2}"; do
|
if [ -t 1 ]; then
|
||||||
[[ ! "${num}" =~ [0-9] ]] \
|
if [ -n "${5}" ]; then
|
||||||
&& echo "Given RGB value was not a number, received ${num}" \
|
[[ -z "${6}" ]] && echo "A value must be passed for bg_green" && return 1
|
||||||
&& return 1
|
[[ -z "${7}" ]] && echo "A value must be passed for bg_blue" && return 1
|
||||||
[[ "${num}" -gt 255 ]] \
|
printf "\033[38;2;%s;%s;%s;48;2;%s;%s;%sm%s\033[m\n" \
|
||||||
&& echo "Given RGB value must be less than 255, received ${num}" \
|
"${red}" "${green}" "${blue}" "${bg_red}" "${bg_green}" "${bg_blue}" "${input}"
|
||||||
&& return 1
|
else
|
||||||
[[ "${num}" -lt 0 ]] \
|
printf "\033[0;38;2;%s;%s;%sm%s\033[m\n" "${red}" "${green}" "${blue}" "${input}"
|
||||||
&& echo "Given RGB value must be more than 0, received ${num}" \
|
fi
|
||||||
&& return 1
|
else
|
||||||
done
|
printf "%s\n" "${input}"
|
||||||
|
fi
|
||||||
if [ -n "${5}" ]; then
|
return 0
|
||||||
[[ -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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
# Reset colors in the terminal to default
|
# Reset colors in the terminal to default
|
||||||
#
|
#
|
||||||
# Usage:
|
# Usage:
|
||||||
# reset
|
# reset
|
||||||
#
|
#
|
||||||
# POSIX Compliant:
|
# POSIX Compliant:
|
||||||
# N/A
|
# N/A
|
||||||
#
|
#
|
||||||
|
|
||||||
printf "\e[m\n"
|
printf "\e[m\n"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user