Skip to content

Commit 43e0244

Browse files
authored
Merge pull request #611 from DannyBen/update/colors-tty
Update colors library to support auto-colors based on TTY
2 parents 29c5e60 + 8064685 commit 43e0244

File tree

15 files changed

+114
-6
lines changed

15 files changed

+114
-6
lines changed

examples/colors/src/lib/colors.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@
1010
## Color output will be disabled if `NO_COLOR` environment variable is set
1111
## in compliance with https://no-color.org/
1212
##
13+
## In case you wish to enable auto detection for color output based on the
14+
## terminal being interactive (TTY), call `enable_auto_colors` in your
15+
## `src/initialize.sh` (Run `bashly add hooks` to add this file).
16+
##
17+
enable_auto_colors() {
18+
## If NO_COLOR has not been set and stdout is not a TTY, disable colors
19+
if [[ -z ${NO_COLOR+x} && ! -t 1 ]]; then
20+
NO_COLOR=1
21+
fi
22+
}
23+
1324
print_in_color() {
1425
local color="$1"
1526
shift
16-
if [[ -z ${NO_COLOR+x} ]]; then
27+
if [[ "${NO_COLOR:-}" == "" ]]; then
1728
printf "$color%b\e[0m\n" "$*"
1829
else
1930
printf "%b\n" "$*"

examples/dependencies-alt/src/lib/colors.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@
1010
## Color output will be disabled if `NO_COLOR` environment variable is set
1111
## in compliance with https://no-color.org/
1212
##
13+
## In case you wish to enable auto detection for color output based on the
14+
## terminal being interactive (TTY), call `enable_auto_colors` in your
15+
## `src/initialize.sh` (Run `bashly add hooks` to add this file).
16+
##
17+
enable_auto_colors() {
18+
## If NO_COLOR has not been set and stdout is not a TTY, disable colors
19+
if [[ -z ${NO_COLOR+x} && ! -t 1 ]]; then
20+
NO_COLOR=1
21+
fi
22+
}
23+
1324
print_in_color() {
1425
local color="$1"
1526
shift
16-
if [[ -z ${NO_COLOR+x} ]]; then
27+
if [[ "${NO_COLOR:-}" == "" ]]; then
1728
printf "$color%b\e[0m\n" "$*"
1829
else
1930
printf "%b\n" "$*"

examples/dependencies/src/lib/colors.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@
1010
## Color output will be disabled if `NO_COLOR` environment variable is set
1111
## in compliance with https://no-color.org/
1212
##
13+
## In case you wish to enable auto detection for color output based on the
14+
## terminal being interactive (TTY), call `enable_auto_colors` in your
15+
## `src/initialize.sh` (Run `bashly add hooks` to add this file).
16+
##
17+
enable_auto_colors() {
18+
## If NO_COLOR has not been set and stdout is not a TTY, disable colors
19+
if [[ -z ${NO_COLOR+x} && ! -t 1 ]]; then
20+
NO_COLOR=1
21+
fi
22+
}
23+
1324
print_in_color() {
1425
local color="$1"
1526
shift
16-
if [[ -z ${NO_COLOR+x} ]]; then
27+
if [[ "${NO_COLOR:-}" == "" ]]; then
1728
printf "$color%b\e[0m\n" "$*"
1829
else
1930
printf "%b\n" "$*"

examples/render-mandoc/docs/download.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" Automatically generated by Pandoc 3.2
22
.\"
3-
.TH "download" "1" "December 2024" "Version 0.1.0" "Sample application"
3+
.TH "download" "1" "January 2025" "Version 0.1.0" "Sample application"
44
.SH NAME
55
\f[B]download\f[R] \- Sample application
66
.SH SYNOPSIS

examples/render-mandoc/docs/download.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% download(1) Version 0.1.0 | Sample application
22
% Lana Lang
3-
% December 2024
3+
% January 2025
44

55
NAME
66
==================================================

lib/bashly/libraries/colors/colors.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
## Color output will be disabled if `NO_COLOR` environment variable is set
1111
## in compliance with https://no-color.org/
1212
##
13+
## In case you wish to enable auto detection for color output based on the
14+
## terminal being interactive (TTY), call `enable_auto_colors` in your
15+
## `src/initialize.sh` (Run `bashly add hooks` to add this file).
16+
##
17+
enable_auto_colors() {
18+
## If NO_COLOR has not been set and stdout is not a TTY, disable colors
19+
if [[ -z ${NO_COLOR+x} && ! -t 1 ]]; then
20+
NO_COLOR=1
21+
fi
22+
}
23+
1324
print_in_color() {
1425
local color="$1"
1526
shift

lib/bashly/libraries/libraries.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ colors:
33
files:
44
- source: "colors/colors.sh"
55
target: "%{user_lib_dir}/colors.%{user_ext}"
6+
post_install_message: |
7+
The colors library supports the g`NO_COLOR` standard out of the box.
8+
9+
In case you wish to also enable automatic output of colors based on the
10+
terminal being interactive (TTY), call g`enable_auto_colors` in your
11+
g`src/initialize.sh`. You may run the following command to add this file:
12+
13+
m`$ bashly add hooks`
614
715
completions:
816
help: Generate a bash completions function.

spec/approvals/cli/add/colors

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
created spec/tmp/src/lib/colors.sh
2+
3+
The colors library supports the NO_COLOR standard out of the box.
4+
5+
In case you wish to also enable automatic output of colors based on the
6+
terminal being interactive (TTY), call enable_auto_colors in your
7+
src/initialize.sh. You may run the following command to add this file:
8+
9+
$ bashly add hooks
10+

spec/approvals/examples/colors

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
+ bashly add colors --force
22
created src/lib/colors.sh
3+
4+
The colors library supports the NO_COLOR standard out of the box.
5+
6+
In case you wish to also enable automatic output of colors based on the
7+
terminal being interactive (TTY), call enable_auto_colors in your
8+
src/initialize.sh. You may run the following command to add this file:
9+
10+
$ bashly add hooks
11+
312
+ bashly generate
413
creating user files in src
514
skipped src/root_command.sh (exists)

spec/approvals/examples/dependencies

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
+ bashly add colors
22
created src/lib/colors.sh
3+
4+
The colors library supports the NO_COLOR standard out of the box.
5+
6+
In case you wish to also enable automatic output of colors based on the
7+
terminal being interactive (TTY), call enable_auto_colors in your
8+
src/initialize.sh. You may run the following command to add this file:
9+
10+
$ bashly add hooks
11+
312
+ bashly generate
413
creating user files in src
514
created src/download_command.sh

0 commit comments

Comments
 (0)