diff --git a/src/colorize.cr b/src/colorize.cr index 60ab17a28718..4996d3c52c15 100644 --- a/src/colorize.cr +++ b/src/colorize.cr @@ -129,16 +129,18 @@ module Colorize # "hello".colorize.red.to_s # => "hello" # ``` # - # NOTE: This is by default disabled if the environment variable `NO_COLOR` contains any value. - class_property? enabled : Bool { !ENV.has_key?("NO_COLOR") } + # NOTE: This is by default disabled if the environment variable `NO_COLOR` + # contains any non-empty value. + class_property? enabled : Bool { !ENV["NO_COLOR"]?.try(&.empty?.!) } # Makes `Colorize.enabled` `true` if and only if both of `STDOUT.tty?` # and `STDERR.tty?` are `true` and the tty is not considered a dumb terminal. # This is determined by the environment variable called `TERM`. # If `TERM=dumb`, color won't be enabled. - # If `NO_COLOR` contains any value color won't be enabled conforming to https://no-color.org + # If `NO_COLOR` contains any non-empty value, color won't be enabled + # conforming to https://no-color.org def self.on_tty_only! - self.enabled = STDOUT.tty? && STDERR.tty? && ENV["TERM"]? != "dumb" && !ENV.has_key?("NO_COLOR") + self.enabled = STDOUT.tty? && STDERR.tty? && ENV["TERM"]? != "dumb" && !ENV["NO_COLOR"]?.try(&.empty?.!) end # Resets the color and text decoration of the *io*. diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index a7a911b00feb..fae6a50a21a4 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -59,7 +59,7 @@ class Crystal::Command @compiler : Compiler? def initialize(@options : Array(String)) - @color = ENV["TERM"]? != "dumb" && !ENV.has_key?("NO_COLOR") + @color = ENV["TERM"]? != "dumb" && !ENV["NO_COLOR"]?.try(&.empty?.!) @error_trace = false @progress_tracker = ProgressTracker.new end @@ -762,7 +762,7 @@ class Crystal::Command private def error(msg, exit_code = 1) # This is for the case where the main command is wrong - @color = false if ARGV.includes?("--no-color") || ENV["TERM"]? == "dumb" || ENV.has_key?("NO_COLOR") + @color = false if ARGV.includes?("--no-color") || ENV["TERM"]? == "dumb" || ENV["NO_COLOR"]?.try(&.empty?.!) Crystal.error msg, @color, exit_code: exit_code end