Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/colorize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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*.
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down