Skip to content

Commit 4581c8e

Browse files
Make Colorize.on_tty_only! the default behavior (#15881)
Originally part of #7690, this seems to be the more sensible behavior compared to ignoring non-terminal streams and `$TERM`. This does not deprecate `.on_tty_only!` because it is still useful after the default is overridden. Effectively reverts #14258. Co-authored-by: Johannes Müller <[email protected]>
1 parent 5ccf2ff commit 4581c8e

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/colorize.cr

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,25 @@ module Colorize
129129
# "hello".colorize.red.to_s # => "hello"
130130
# ```
131131
#
132-
# NOTE: This is by default disabled if the environment variable `NO_COLOR`
133-
# contains any non-empty value.
134-
class_property? enabled : Bool { !ENV["NO_COLOR"]?.try(&.empty?.!) }
132+
# NOTE: This is by default enabled according to `.on_tty_only!`.
133+
class_property? enabled : Bool do
134+
STDOUT.tty? && STDERR.tty? && ENV["TERM"]? != "dumb" && !ENV["NO_COLOR"]?.try(&.empty?.!)
135+
end
135136

136137
# Makes `Colorize.enabled` `true` if and only if both of `STDOUT.tty?`
137138
# and `STDERR.tty?` are `true` and the tty is not considered a dumb terminal.
138139
# This is determined by the environment variable called `TERM`.
139140
# If `TERM=dumb`, color won't be enabled.
140141
# If `NO_COLOR` contains any non-empty value, color won't be enabled
141142
# conforming to https://no-color.org
142-
def self.on_tty_only!
143-
self.enabled = STDOUT.tty? && STDERR.tty? && ENV["TERM"]? != "dumb" && !ENV["NO_COLOR"]?.try(&.empty?.!)
143+
#
144+
# Returns the new value of `Colorize.enabled?`.
145+
#
146+
# This can be used to revert `Colorize.enabled?` to its default value after
147+
# colorization is explicitly enabled or disabled.
148+
def self.on_tty_only! : Bool
149+
@@enabled = nil
150+
enabled?
144151
end
145152

146153
# Resets the color and text decoration of the *io*.

0 commit comments

Comments
 (0)