Skip to content

Make Colorize.on_tty_only! the default behavior #15881

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

HertzDevil
Copy link
Contributor

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.

src/colorize.cr Outdated

# 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
#
# This can be used to revert `Colorize.enabled?` to its default value after
# colorization is explicitly enabled or disabled.
def self.on_tty_only!
self.enabled = STDOUT.tty? && STDERR.tty? && ENV["TERM"]? != "dumb" && !ENV.has_key?("NO_COLOR")
Copy link
Member

@straight-shoota straight-shoota Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: We could reset @@enabled to avoid duplication. This means the default block of the above class_property definition would be invoked on the next getter call.

Suggested change
self.enabled = STDOUT.tty? && STDERR.tty? && ENV["TERM"]? != "dumb" && !ENV.has_key?("NO_COLOR")
@@enabled = nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line in Shards implicitly assumes that on_tty_only! returns the new value of enabled?, so this is unfortunately a breaking change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe return enabled then? This still avoids duplicating the exact same condition.

Suggested change
self.enabled = STDOUT.tty? && STDERR.tty? && ENV["TERM"]? != "dumb" && !ENV.has_key?("NO_COLOR")
@enabled = nil
enabled

HertzDevil and others added 3 commits June 5, 2025 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants