From 9e9e73400b530697159614b443aae3c63bd18c8e Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Mon, 17 Jan 2022 10:25:33 -0600 Subject: [PATCH] [ RGB Out ] - Detect if in tty --- Templates/Functions/Logging/Logging-RGB.bash | 168 ++++++++-------- Templates/Functions/RGB-Terminal-Colors.bash | 196 +++++++++---------- 2 files changed, 184 insertions(+), 180 deletions(-) diff --git a/Templates/Functions/Logging/Logging-RGB.bash b/Templates/Functions/Logging/Logging-RGB.bash index 38a1cd9..8f8520f 100755 --- a/Templates/Functions/Logging/Logging-RGB.bash +++ b/Templates/Functions/Logging/Logging-RGB.bash @@ -1,104 +1,108 @@ #!/bin/bash echo_rgb() { - # Echo a colored string to the terminal based on rgb values - # - # Positional Arguments: - # - # message - # - The message to be printed to stdout - # red - # - The red value from 0 to 255 - # green - # - The green value from 0 to 255 - # blue - # - 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 + # - The message to be printed to stdout + # red + # - The red value from 0 to 255 + # green + # - The green value from 0 to 255 + # blue + # - 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 - # - The log level, defined within a case check in this function - # message - # - The info message - # line_number - # - 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 + # - The log level, defined within a case check in this function + # message + # - The info message + # line_number + # - 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 } diff --git a/Templates/Functions/RGB-Terminal-Colors.bash b/Templates/Functions/RGB-Terminal-Colors.bash index 424a96b..1637abd 100755 --- a/Templates/Functions/RGB-Terminal-Colors.bash +++ b/Templates/Functions/RGB-Terminal-Colors.bash @@ -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 + # - The red value from 0 to 255 + # green + # - The green value from 0 to 255 + # blue + # - 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 - # - The red value from 0 to 255 - # green - # - The green value from 0 to 255 - # blue - # - 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 - # - The message to be printed to stdout - # red - # - The red value from 0 to 255 - # green - # - The green value from 0 to 255 - # blue - # - The blue value from 0 to 255 - # bg_red - # - The background red value from 0 to 255 - # bg_green - # - The background green value from 0 to 255 - # bg_blue - # - 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 + # - The message to be printed to stdout + # red + # - The red value from 0 to 255 + # green + # - The green value from 0 to 255 + # blue + # - The blue value from 0 to 255 + # bg_red + # - The background red value from 0 to 255 + # bg_green + # - The background green value from 0 to 255 + # bg_blue + # - 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" }