github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/tests/includes/colors.sh (about)

     1  supports_colors() {
     2  	if [[ -z ${TERM} ]] || [[ ${TERM} == "" ]] || [[ ${TERM} == "dumb" ]]; then
     3  		echo "NO"
     4  		return
     5  	fi
     6  	if which tput >/dev/null 2>&1; then
     7  		# shellcheck disable=SC2046
     8  		if [[ $(tput colors) -gt 1 ]]; then
     9  			echo "YES"
    10  			return
    11  		fi
    12  	fi
    13  	echo "NO"
    14  }
    15  
    16  red() {
    17  	if [[ "$(supports_colors)" == "YES" ]]; then
    18  		tput sgr0
    19  		echo "$(tput setaf 1)${1}$(tput sgr0)"
    20  		return
    21  	fi
    22  	echo "${1}"
    23  }
    24  
    25  green() {
    26  	if [[ "$(supports_colors)" == "YES" ]]; then
    27  		tput sgr0
    28  		echo "$(tput setaf 2)${1}$(tput sgr0)"
    29  		return
    30  	fi
    31  	echo "${1}"
    32  }