|
4 | 4 | # |
5 | 5 | # Usage: |
6 | 6 | # Use any of the functions below to color or format a portion of a string. |
7 | | -# |
| 7 | +# |
8 | 8 | # echo "before $(red this is red) after" |
9 | 9 | # echo "before $(green_bold this is green_bold) after" |
10 | 10 | # |
| 11 | +# Color output will be disabled if `NO_COLOR` environment variable is set |
| 12 | +# in compliance with https://no-color.org/ |
| 13 | +# |
11 | 14 | # --- |
12 | 15 |
|
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" "$*"; } |
0 commit comments