-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Color functions should act as if NO_COLOR
is set when fd 0 is not a tty
#327
Comments
It may be desirable to introduce a if [[ -n "${FORCE_COLOR}" ]]; then
printf "$color%b\e[0m\n" "$*"
elif [[ -z ${NO_COLOR+x} ]] && [[ -t 1 ]] && [[ $- == *i* ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi You could use |
Alright. I am not opposed to it, but isn't the whole purpose of One of the annoying things about no-color-when-no-tty is that when you run a colorful application through Thoughts on that? |
I do not like the overly complex color detection settings. This is why we follow NO_COLOR. They way I see it, adding a non tty detection, means there is no way to force colors, unless adding FORCE_COLOR..... which brings me to my first point... :) |
Unfortunately, I agree that piping through chalk uses
Given that if (( ${FORCE_COLOR:-1} > 0 )); then
printf "$color%b\e[0m\n" "$*"
elif (( ${FORCE_COLOR:-1} = 0 )); then
printf "%b\n" "$*"
elif [[ -z ${NO_COLOR+x} ]] && [[ -t 1 ]] && [[ $- == *i* ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi |
Note that the above fix works correctly when calling I do not have a trivial solution to this specific issue. |
Well, since the added color functions can be modified according to the users needs, I am leaning towards implementing the "full suite", but in a way that will help users disable or enable any of the features. The full suite being (by order of priority)
I will take a look at it tomorrow, unless you plan on proposing a PR? Also - why do we have two tty related conditions? |
BTW the source script is here. |
I can do a PR, but probably not in the next 24–48 hours.
Only the first one is TTY-related. The second is shell-related and the presence of ❯ bash -c 'echo $-'
hBc
❯ bash -i -c 'echo $-'
himBHc That way, if you have a script which supports colour running under a non-interactive shell (such as under |
Nice. I will have time for it tomorrow morning, so I will take first stab at it. |
Well. There is a problem. The color functions are intended to be used inside a string, like so:
when used like this, So - the only way I can think of to support the auto detection, requires setting the color mode beforehand - at the script's initialization, and not the function itself. Like so: #!/usr/bin/env bash
set_color_mode() {
if (( ${FORCE_COLOR:-0} > 0 )); then
wants_color=true
elif (( ${FORCE_COLOR:-1} == 0 )); then
wants_color=false
elif [[ -n ${NO_COLOR+x} ]]; then
wants_color=false
elif [[ -t 1 ]] && [[ $- == *I* ]]; then
wants_color=true
else
wants_color=false
fi
}
print_in_color() {
local color="$1"
shift
if $wants_color; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}
red() { print_in_color "\e[31m" "$*"; }
green() { print_in_color "\e[32m" "$*"; }
set_color_mode
green "Direct test"
echo "And $(red inline test)" This works in all cases that I found. The |
I think that’s a fine compromise, and sort of where I was going to probably implement this when I got around to it. |
That's the thing. I cannot make that compromise. The |
I think you can — and it doesn’t even effect ## Color functions [@bashly-upgrade colors]
## This file is a part of Bashly standard library
##
## 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/
##
declare wants_color
print_in_color() {
local color="$1"
shift
if $wants_color; 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" "$*"; }
if ((${FORCE_COLOR:-0} > 0)); then
wants_color=true
elif ((${FORCE_COLOR:-1} == 0)); then
wants_color=false
elif [[ -n ${NO_COLOR+x} ]]; then
wants_color=false
elif [[ -t 1 ]] && [[ $- == *I* ]]; then
wants_color=true
else
wants_color=false
fi Bashly takes whatever is present in What's in
This may be something that you prefer not to do because it doesn’t fit your intentions of how Bashly should be create scripts, but nothing prevents users of Bashly from doing exactly that with the way that the The only bit of excess documentation that may be added for this (aside from describing |
Unacceptable. Bashly goes to great lengths to ensure the generated script is clean and neat and resembles how programs in "real" languages look like. Writing code outside functions, in the middle of the script is not something I am going to allow. What users do with the lib directory is their own business. If they want to ruin it, they can. There is nothing I can or care to do about it. |
I am going to close this as well. The color library - like any other Thanks for raising it. |
Description
Colour functions should act as if
NO_COLOR
is set when non-interactive or input is not a TTY. In that way, running a function in a sub shell would not output ANSI escape codes, or when piping to another function.Reproduction steps
For demonstration purposes, I modified the included
src/lib/colors.sh
to includecyan_bold hello
at the bottom of the script. I’m also explicitly escaping the ANSI codes for visibility.Actual behavior
Expected behavior
Proposed fix
Change
to
The text was updated successfully, but these errors were encountered: