## Color functions [@bashly-upgrade colors] ## This file is a part of Bashly standard library ## ## Usage: ## Use any of the functions below to color or format a portion of a string. ## ## echo βbefore $(red this is red) afterβ ## echo βbefore $(green_bold this is green_bold) afterβ ## ## Color output will be disabled if βNO_COLOR` environment variable is set ## in compliance with no-color.org/ ## ## In case you wish to enable auto detection for color output based on the ## terminal being interactive (TTY), call `enable_auto_colors` in your ## `src/initialize.sh` (Run `bashly add hooks` to add this file). ## enable_auto_colors() {
## If NO_COLOR has not been set and stdout is not a TTY, disable colors
if [[ -z ${NO_COLOR+x} && ! -t 1 ]]; then
NO_COLOR=1
fi
}
print_in_color() {
local color="$1"
shift
if [[ "${NO_COLOR:-}" == "" ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}
red() { print_in_color βe[31mβ β$*β; } green() { print_in_color βe[32mβ β$*β; } yellow() { print_in_color βe[33mβ β$*β; } blue() { print_in_color βe[34mβ β$*β; } magenta() { print_in_color βe[35mβ β$*β; } cyan() { print_in_color βe[36mβ β$*β; } black() { print_in_color βe[30mβ β$*β; } white() { print_in_color βe[37mβ β$*β; }
bold() { print_in_color βe[1mβ β$*β; } underlined() { print_in_color βe[4mβ β$*β; } bold_underlined() { print_in_color βe[1;4mβ β$*β; }
red_bold() { print_in_color βe[1;31mβ β$*β; } green_bold() { print_in_color βe[1;32mβ β$*β; } yellow_bold() { print_in_color βe[1;33mβ β$*β; } blue_bold() { print_in_color βe[1;34mβ β$*β; } magenta_bold() { print_in_color βe[1;35mβ β$*β; } cyan_bold() { print_in_color βe[1;36mβ β$*β; } black_bold() { print_in_color βe[1;30mβ β$*β; } white_bold() { print_in_color βe[1;37mβ β$*β; }
red_underlined() { print_in_color βe[4;31mβ β$*β; } green_underlined() { print_in_color βe[4;32mβ β$*β; } yellow_underlined() { print_in_color βe[4;33mβ β$*β; } blue_underlined() { print_in_color βe[4;34mβ β$*β; } magenta_underlined() { print_in_color βe[4;35mβ β$*β; } cyan_underlined() { print_in_color βe[4;36mβ β$*β; } black_underlined() { print_in_color βe[4;30mβ β$*β; } white_underlined() { print_in_color βe[4;37mβ β$*β; }