diff --git a/examples/colors/src/lib/colors.sh b/examples/colors/src/lib/colors.sh index 7458e2d9..0653bb63 100644 --- a/examples/colors/src/lib/colors.sh +++ b/examples/colors/src/lib/colors.sh @@ -10,10 +10,21 @@ ## Color output will be disabled if `NO_COLOR` environment variable is set ## in compliance with https://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 [[ -z ${NO_COLOR+x} ]]; then + if [[ "${NO_COLOR:-}" == "" ]]; then printf "$color%b\e[0m\n" "$*" else printf "%b\n" "$*" diff --git a/examples/dependencies-alt/src/lib/colors.sh b/examples/dependencies-alt/src/lib/colors.sh index 7458e2d9..0653bb63 100644 --- a/examples/dependencies-alt/src/lib/colors.sh +++ b/examples/dependencies-alt/src/lib/colors.sh @@ -10,10 +10,21 @@ ## Color output will be disabled if `NO_COLOR` environment variable is set ## in compliance with https://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 [[ -z ${NO_COLOR+x} ]]; then + if [[ "${NO_COLOR:-}" == "" ]]; then printf "$color%b\e[0m\n" "$*" else printf "%b\n" "$*" diff --git a/examples/dependencies/src/lib/colors.sh b/examples/dependencies/src/lib/colors.sh index 7458e2d9..0653bb63 100644 --- a/examples/dependencies/src/lib/colors.sh +++ b/examples/dependencies/src/lib/colors.sh @@ -10,10 +10,21 @@ ## Color output will be disabled if `NO_COLOR` environment variable is set ## in compliance with https://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 [[ -z ${NO_COLOR+x} ]]; then + if [[ "${NO_COLOR:-}" == "" ]]; then printf "$color%b\e[0m\n" "$*" else printf "%b\n" "$*" diff --git a/examples/render-mandoc/docs/download.1 b/examples/render-mandoc/docs/download.1 index 502b828c..09bf0f85 100644 --- a/examples/render-mandoc/docs/download.1 +++ b/examples/render-mandoc/docs/download.1 @@ -1,6 +1,6 @@ .\" Automatically generated by Pandoc 3.2 .\" -.TH "download" "1" "December 2024" "Version 0.1.0" "Sample application" +.TH "download" "1" "January 2025" "Version 0.1.0" "Sample application" .SH NAME \f[B]download\f[R] \- Sample application .SH SYNOPSIS diff --git a/examples/render-mandoc/docs/download.md b/examples/render-mandoc/docs/download.md index 8e93e65a..32d3df81 100644 --- a/examples/render-mandoc/docs/download.md +++ b/examples/render-mandoc/docs/download.md @@ -1,6 +1,6 @@ % download(1) Version 0.1.0 | Sample application % Lana Lang -% December 2024 +% January 2025 NAME ================================================== diff --git a/lib/bashly/libraries/colors/colors.sh b/lib/bashly/libraries/colors/colors.sh index b98fbd7d..0653bb63 100644 --- a/lib/bashly/libraries/colors/colors.sh +++ b/lib/bashly/libraries/colors/colors.sh @@ -10,6 +10,17 @@ ## Color output will be disabled if `NO_COLOR` environment variable is set ## in compliance with https://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 diff --git a/lib/bashly/libraries/libraries.yml b/lib/bashly/libraries/libraries.yml index 1cff21fb..cda16f55 100644 --- a/lib/bashly/libraries/libraries.yml +++ b/lib/bashly/libraries/libraries.yml @@ -3,6 +3,14 @@ colors: files: - source: "colors/colors.sh" target: "%{user_lib_dir}/colors.%{user_ext}" + post_install_message: | + The colors library supports the g`NO_COLOR` standard out of the box. + + In case you wish to also enable automatic output of colors based on the + terminal being interactive (TTY), call g`enable_auto_colors` in your + g`src/initialize.sh`. You may run the following command to add this file: + + m`$ bashly add hooks` completions: help: Generate a bash completions function. diff --git a/spec/approvals/cli/add/colors b/spec/approvals/cli/add/colors index affaa155..89b0867b 100644 --- a/spec/approvals/cli/add/colors +++ b/spec/approvals/cli/add/colors @@ -1 +1,10 @@ created spec/tmp/src/lib/colors.sh + +The colors library supports the NO_COLOR standard out of the box. + +In case you wish to also enable automatic output of colors based on the +terminal being interactive (TTY), call enable_auto_colors in your +src/initialize.sh. You may run the following command to add this file: + + $ bashly add hooks + diff --git a/spec/approvals/examples/colors b/spec/approvals/examples/colors index 1240b710..7cc59352 100644 --- a/spec/approvals/examples/colors +++ b/spec/approvals/examples/colors @@ -1,5 +1,14 @@ + bashly add colors --force created src/lib/colors.sh + +The colors library supports the NO_COLOR standard out of the box. + +In case you wish to also enable automatic output of colors based on the +terminal being interactive (TTY), call enable_auto_colors in your +src/initialize.sh. You may run the following command to add this file: + + $ bashly add hooks + + bashly generate creating user files in src skipped src/root_command.sh (exists) diff --git a/spec/approvals/examples/dependencies b/spec/approvals/examples/dependencies index 434b4f25..874ebc31 100644 --- a/spec/approvals/examples/dependencies +++ b/spec/approvals/examples/dependencies @@ -1,5 +1,14 @@ + bashly add colors created src/lib/colors.sh + +The colors library supports the NO_COLOR standard out of the box. + +In case you wish to also enable automatic output of colors based on the +terminal being interactive (TTY), call enable_auto_colors in your +src/initialize.sh. You may run the following command to add this file: + + $ bashly add hooks + + bashly generate creating user files in src created src/download_command.sh diff --git a/spec/approvals/examples/dependencies-alt b/spec/approvals/examples/dependencies-alt index ebf9eb3f..25b7ef40 100644 --- a/spec/approvals/examples/dependencies-alt +++ b/spec/approvals/examples/dependencies-alt @@ -1,5 +1,14 @@ + bashly add colors created src/lib/colors.sh + +The colors library supports the NO_COLOR standard out of the box. + +In case you wish to also enable automatic output of colors based on the +terminal being interactive (TTY), call enable_auto_colors in your +src/initialize.sh. You may run the following command to add this file: + + $ bashly add hooks + + bashly generate creating user files in src created src/download_command.sh diff --git a/spec/approvals/examples/render-mandoc b/spec/approvals/examples/render-mandoc index 49a8c85b..8fb99f35 100644 --- a/spec/approvals/examples/render-mandoc +++ b/spec/approvals/examples/render-mandoc @@ -44,4 +44,4 @@ ISSUE TRACKER AUTHORS Lana Lang. -Version 0.1.0 December 2024 download(1) +Version 0.1.0 January 2025 download(1) diff --git a/spec/approvals/fixtures/lib-custom-path b/spec/approvals/fixtures/lib-custom-path index c4a941c2..02a3e73a 100644 --- a/spec/approvals/fixtures/lib-custom-path +++ b/spec/approvals/fixtures/lib-custom-path @@ -7,6 +7,15 @@ run ./cli --help to test your bash script created src/my-libz/sample_function.sh + bundle exec bashly add colors created src/my-libz/colors.sh + +The colors library supports the NO_COLOR standard out of the box. + +In case you wish to also enable automatic output of colors based on the +terminal being interactive (TTY), call enable_auto_colors in your +src/initialize.sh. You may run the following command to add this file: + + $ bashly add hooks + + bundle exec bashly generate creating user files in src skipped src/root_command.sh (exists) diff --git a/spec/approvals/fixtures/partials-extension b/spec/approvals/fixtures/partials-extension index bf567379..825e2278 100644 --- a/spec/approvals/fixtures/partials-extension +++ b/spec/approvals/fixtures/partials-extension @@ -11,6 +11,15 @@ created ./cli run ./cli --help to test your bash script + bundle exec bashly add colors created src/lib/colors.bash + +The colors library supports the NO_COLOR standard out of the box. + +In case you wish to also enable automatic output of colors based on the +terminal being interactive (TTY), call enable_auto_colors in your +src/initialize.sh. You may run the following command to add this file: + + $ bashly add hooks + + bundle exec bashly add completions created src/lib/send_completions.bash diff --git a/spec/bashly/library_spec.rb b/spec/bashly/library_spec.rb index a3aa0fc1..269daf02 100644 --- a/spec/bashly/library_spec.rb +++ b/spec/bashly/library_spec.rb @@ -32,6 +32,8 @@ describe '#post_install_message' do context 'when the library has no configured message' do + let(:name) { :config } + it 'returns nil' do expect(subject.post_install_message).to be_nil end