From 1ae60a21c453698003b9ec904b97ff0a9e6bbe4a Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sat, 23 Oct 2021 12:56:43 +0000 Subject: [PATCH] - Add support for NO_COLORS --- examples/colors/src/lib/colors.sh | 55 ++++++++++++++++++------------ examples/colors/test.sh | 2 ++ lib/bashly/templates/lib/colors.sh | 55 ++++++++++++++++++------------ spec/approvals/examples/colors | 10 ++++++ 4 files changed, 80 insertions(+), 42 deletions(-) diff --git a/examples/colors/src/lib/colors.sh b/examples/colors/src/lib/colors.sh index 485d45e6..5a1a41d9 100644 --- a/examples/colors/src/lib/colors.sh +++ b/examples/colors/src/lib/colors.sh @@ -4,29 +4,42 @@ # # 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 https://no-color.org/ +# # --- -red() { printf "\e[31m%b\e[0m\n" "$*"; } -green() { printf "\e[32m%b\e[0m\n" "$*"; } -yellow() { printf "\e[33m%b\e[0m\n" "$*"; } -blue() { printf "\e[34m%b\e[0m\n" "$*"; } -magenta() { printf "\e[35m%b\e[0m\n" "$*"; } -cyan() { printf "\e[36m%b\e[0m\n" "$*"; } -bold() { printf "\e[1m%b\e[0m\n" "$*"; } -underlined() { printf "\e[4m%b\e[0m\n" "$*"; } -red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; } -green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; } -yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; } -blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; } -magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; } -cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; } -red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; } -green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; } -yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; } -blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; } -magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; } -cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; } +print_in_color() { + local color="$1" + shift + if [[ -z ${NO_COLOR+x} ]]; 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" "$*"; } +bold() { print_in_color "\e[1m" "$*"; } +underlined() { print_in_color "\e[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" "$*"; } +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" "$*"; } diff --git a/examples/colors/test.sh b/examples/colors/test.sh index 57703480..3aa60938 100644 --- a/examples/colors/test.sh +++ b/examples/colors/test.sh @@ -2,8 +2,10 @@ set -x +bashly add colors --force bashly generate ### Try Me ### ./colorly +NO_COLOR=1 ./colorly diff --git a/lib/bashly/templates/lib/colors.sh b/lib/bashly/templates/lib/colors.sh index 485d45e6..5a1a41d9 100644 --- a/lib/bashly/templates/lib/colors.sh +++ b/lib/bashly/templates/lib/colors.sh @@ -4,29 +4,42 @@ # # 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 https://no-color.org/ +# # --- -red() { printf "\e[31m%b\e[0m\n" "$*"; } -green() { printf "\e[32m%b\e[0m\n" "$*"; } -yellow() { printf "\e[33m%b\e[0m\n" "$*"; } -blue() { printf "\e[34m%b\e[0m\n" "$*"; } -magenta() { printf "\e[35m%b\e[0m\n" "$*"; } -cyan() { printf "\e[36m%b\e[0m\n" "$*"; } -bold() { printf "\e[1m%b\e[0m\n" "$*"; } -underlined() { printf "\e[4m%b\e[0m\n" "$*"; } -red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; } -green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; } -yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; } -blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; } -magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; } -cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; } -red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; } -green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; } -yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; } -blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; } -magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; } -cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; } +print_in_color() { + local color="$1" + shift + if [[ -z ${NO_COLOR+x} ]]; 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" "$*"; } +bold() { print_in_color "\e[1m" "$*"; } +underlined() { print_in_color "\e[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" "$*"; } +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" "$*"; } diff --git a/spec/approvals/examples/colors b/spec/approvals/examples/colors index 59dc42d2..c7f93990 100644 --- a/spec/approvals/examples/colors +++ b/spec/approvals/examples/colors @@ -1,3 +1,5 @@ ++ bashly add colors --force +created src/lib/colors.sh + bashly generate creating user files in src skipped src/initialize.sh (exists) @@ -11,3 +13,11 @@ Message Recevied: ==> hello colors ===> hello colors ++ NO_COLOR=1 ++ ./colorly +Message Recevied: + + => hello colors + ==> hello colors + ===> hello colors +