colorize.rb: support for NO_COLOR

This commit is contained in:
Nobuyoshi Nakada 2022-11-01 10:56:28 +09:00
parent f0c8c1e878
commit 99a79dc40b
No known key found for this signature in database
GPG Key ID: 7CD2805BFA3770C6

View File

@ -7,7 +7,7 @@ class Colorize
def initialize(color = nil, opts = ((_, color = color, nil)[0] if Hash === color)) def initialize(color = nil, opts = ((_, color = color, nil)[0] if Hash === color))
@colors = @reset = nil @colors = @reset = nil
@color = (opts[:color] if opts) @color = (opts[:color] if opts)
if color or (color == nil && STDOUT.tty?) if color or (color == nil && STDOUT.tty? && (ENV["NO_COLOR"] || "").empty?)
if (%w[smso so].any? {|attr| /\A\e\[.*m\z/ =~ IO.popen("tput #{attr}", "r", :err => IO::NULL, &:read)} rescue nil) if (%w[smso so].any? {|attr| /\A\e\[.*m\z/ =~ IO.popen("tput #{attr}", "r", :err => IO::NULL, &:read)} rescue nil)
@beg = "\e[" @beg = "\e["
colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:\n]*)/)] : {} colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:\n]*)/)] : {}