Skip to content

Commit 5daeea5

Browse files
authored
Merge pull request #137 from DannyBen/add/no-colors
Add support for NO_COLORS
2 parents 96d7c00 + 1ae60a2 commit 5daeea5

File tree

4 files changed

+80
-42
lines changed

4 files changed

+80
-42
lines changed

examples/colors/src/lib/colors.sh

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,42 @@
44
#
55
# Usage:
66
# Use any of the functions below to color or format a portion of a string.
7-
#
7+
#
88
# echo "before $(red this is red) after"
99
# echo "before $(green_bold this is green_bold) after"
1010
#
11+
# Color output will be disabled if `NO_COLOR` environment variable is set
12+
# in compliance with https://no-color.org/
13+
#
1114
# ---
1215

13-
red() { printf "\e[31m%b\e[0m\n" "$*"; }
14-
green() { printf "\e[32m%b\e[0m\n" "$*"; }
15-
yellow() { printf "\e[33m%b\e[0m\n" "$*"; }
16-
blue() { printf "\e[34m%b\e[0m\n" "$*"; }
17-
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
18-
cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
19-
bold() { printf "\e[1m%b\e[0m\n" "$*"; }
20-
underlined() { printf "\e[4m%b\e[0m\n" "$*"; }
21-
red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; }
22-
green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; }
23-
yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; }
24-
blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; }
25-
magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; }
26-
cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; }
27-
red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; }
28-
green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; }
29-
yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; }
30-
blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; }
31-
magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; }
32-
cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; }
16+
print_in_color() {
17+
local color="$1"
18+
shift
19+
if [[ -z ${NO_COLOR+x} ]]; then
20+
printf "$color%b\e[0m\n" "$*";
21+
else
22+
printf "%b\n" "$*";
23+
fi
24+
}
25+
26+
red() { print_in_color "\e[31m" "$*"; }
27+
green() { print_in_color "\e[32m" "$*"; }
28+
yellow() { print_in_color "\e[33m" "$*"; }
29+
blue() { print_in_color "\e[34m" "$*"; }
30+
magenta() { print_in_color "\e[35m" "$*"; }
31+
cyan() { print_in_color "\e[36m" "$*"; }
32+
bold() { print_in_color "\e[1m" "$*"; }
33+
underlined() { print_in_color "\e[4m" "$*"; }
34+
red_bold() { print_in_color "\e[1;31m" "$*"; }
35+
green_bold() { print_in_color "\e[1;32m" "$*"; }
36+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
37+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
38+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
39+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
40+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
41+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
42+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
43+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
44+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
45+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

examples/colors/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
set -x
44

5+
bashly add colors --force
56
bashly generate
67

78
### Try Me ###
89

910
./colorly
11+
NO_COLOR=1 ./colorly

lib/bashly/templates/lib/colors.sh

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,42 @@
44
#
55
# Usage:
66
# Use any of the functions below to color or format a portion of a string.
7-
#
7+
#
88
# echo "before $(red this is red) after"
99
# echo "before $(green_bold this is green_bold) after"
1010
#
11+
# Color output will be disabled if `NO_COLOR` environment variable is set
12+
# in compliance with https://no-color.org/
13+
#
1114
# ---
1215

13-
red() { printf "\e[31m%b\e[0m\n" "$*"; }
14-
green() { printf "\e[32m%b\e[0m\n" "$*"; }
15-
yellow() { printf "\e[33m%b\e[0m\n" "$*"; }
16-
blue() { printf "\e[34m%b\e[0m\n" "$*"; }
17-
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
18-
cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
19-
bold() { printf "\e[1m%b\e[0m\n" "$*"; }
20-
underlined() { printf "\e[4m%b\e[0m\n" "$*"; }
21-
red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; }
22-
green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; }
23-
yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; }
24-
blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; }
25-
magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; }
26-
cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; }
27-
red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; }
28-
green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; }
29-
yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; }
30-
blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; }
31-
magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; }
32-
cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; }
16+
print_in_color() {
17+
local color="$1"
18+
shift
19+
if [[ -z ${NO_COLOR+x} ]]; then
20+
printf "$color%b\e[0m\n" "$*";
21+
else
22+
printf "%b\n" "$*";
23+
fi
24+
}
25+
26+
red() { print_in_color "\e[31m" "$*"; }
27+
green() { print_in_color "\e[32m" "$*"; }
28+
yellow() { print_in_color "\e[33m" "$*"; }
29+
blue() { print_in_color "\e[34m" "$*"; }
30+
magenta() { print_in_color "\e[35m" "$*"; }
31+
cyan() { print_in_color "\e[36m" "$*"; }
32+
bold() { print_in_color "\e[1m" "$*"; }
33+
underlined() { print_in_color "\e[4m" "$*"; }
34+
red_bold() { print_in_color "\e[1;31m" "$*"; }
35+
green_bold() { print_in_color "\e[1;32m" "$*"; }
36+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
37+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
38+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
39+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
40+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
41+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
42+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
43+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
44+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
45+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

spec/approvals/examples/colors

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
+ bashly add colors --force
2+
created src/lib/colors.sh
13
+ bashly generate
24
creating user files in src
35
skipped src/initialize.sh (exists)
@@ -11,3 +13,11 @@ Message Recevied:
1113
==> hello colors
1214
===> hello colors
1315

16+
+ NO_COLOR=1
17+
+ ./colorly
18+
Message Recevied:
19+
20+
=> hello colors
21+
==> hello colors
22+
===> hello colors
23+

0 commit comments

Comments
 (0)