Replies: 6 comments 14 replies
-
Example of expected result: |
Beta Was this translation helpful? Give feedback.
-
I think I might agree with this, with a small added detail: The default should be exactly what you propose, BUT it should still remain easy for the user to override the default. Or in other words, when the user fails to specify how they want color to be output, try to be "smart" about it using the |
Beta Was this translation helpful? Give feedback.
-
Perhaps something like this: Current implementation: print_in_color() {
local color="$1"
shift
if [[ -z ${NO_COLOR+x} ]]; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
} New implementation: print_color_always() {
local color="$1"
shift
printf "$color%b\e[0m\n" "$*"
}
print_newline() {
printf "%b\n" "$*"
}
if [ "${NO_COLOR}" == "1" ]; then
print_in_color() {
shift
print_newline "$@"
}
elif [ "${NO_COLOR}" == "0" ]; then
print_in_color() {
print_color_always "$@"
}
elif [ -t 1 ]; then
print_in_color() {
print_color_always "$@"
}
else
print_in_color() {
shift
print_newline "$@"
}
fi |
Beta Was this translation helpful? Give feedback.
-
I am not convinced at all. Here is why:
That said - if there is a way to slightly (read: "while keeping it simple") modify the bundled colors library in order to make it easier to enable this behavior - I am for it. |
Beta Was this translation helpful? Give feedback.
-
after looking at it in-depth, i have come to the conclusion that script developers should implement their own that said, this would be another good "recipe" one could add to the wiki. |
Beta Was this translation helpful? Give feedback.
-
Here is my proposal:
print_in_color() {
local color="$1"
shift
if use_color; then
printf "$color%b\e[0m\n" "$*"
else
printf "%b\n" "$*"
fi
}
use_color() {
if [[ -n $COLOR ]]; then
case $COLOR in
always) return 0 ;;
never) return 1 ;;
*) [[ -t 1 ]] && return 0 || return 1 ;; # anything else is "auto"
esac
elif [[ "${NO_COLOR:-}" == "" ]]; then
return 0
else
return 1
fi
}
red() { print_in_color "\e[31m" "$*"; }
# ... more color functions, as in the original library This allows the developer to choose what to use, and even to just leave it for the user to decide. Thoughts? |
Beta Was this translation helpful? Give feedback.
-
Colorized output is pretty neat in interactive sessions. But when we want to use pipes or redirection, those ANSI escape codes are pollution.
There's a POSIX-compliant way to check if a script is being executed in an interactive session
test -t
.Here's the description from the official specification
I usually use the bash version:
It would be cool if the
colors.sh
from the bashly standard library was smart enough to check if the output is going to a terminal or not (pipe/redirection).I tried adding the
[[ -t 1 ]]
logic in theprint_in_color
function, but soon realized that when it's called as a consequence ofecho "before $(red this is red) after"
, the stdout is never a terminal (in other words:[[ -t 1 ]]
is always false).The solution I've found was adding this to my
src/initialize.sh
:and it works pretty well.
It also works if we put that line in the top-level scope of
colors.sh
, which would make the solution available for everyone with no need to tweak theinitialize.sh
.What do you think?
Beta Was this translation helpful? Give feedback.
All reactions