diff options
author | Nobuyoshi Nakada <[email protected]> | 2023-12-09 23:58:21 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2023-12-10 20:43:44 +0900 |
commit | 0096d6a809085820a0532f755b4fedd631c7e3f1 (patch) | |
tree | 4702768fa02827105396b5911b54ebf5630b8387 /tool/lib | |
parent | c4c39082af3520cd96aefc2219a7037865f3f710 (diff) |
Extract configuration and lookup methods [ci skip]
Diffstat (limited to 'tool/lib')
-rw-r--r-- | tool/lib/colorize.rb | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/tool/lib/colorize.rb b/tool/lib/colorize.rb index 8fb90e1833..688a51525f 100644 --- a/tool/lib/colorize.rb +++ b/tool/lib/colorize.rb @@ -7,7 +7,7 @@ class Colorize def initialize(color = nil, opts = ((_, color = color, nil)[0] if Hash === color)) @colors = @reset = nil @color = opts && opts[:color] || color - if color or (color == nil && STDOUT.tty? && (ENV["NO_COLOR"] || "").empty?) + if color or (color == nil && coloring?) if (%w[smso so].any? {|attr| /\A\e\[.*m\z/ =~ IO.popen("tput #{attr}", "r", :err => IO::NULL, &:read)} rescue nil) @beg = "\e[" colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:\n]*)/)] : {} @@ -33,17 +33,24 @@ class Colorize "bold"=>"1", "underline"=>"4", "reverse"=>"7", } - NO_COLOR = (nc = ENV['NO_COLOR']) && !nc.empty? + def coloring? + STDOUT.tty? && (!(nc = ENV['NO_COLOR']) || nc.empty?) + end # colorize.decorate(str, name = color_name) def decorate(str, name = @color) - if !NO_COLOR and @colors and color = (@colors[name] || DEFAULTS[name]) + if coloring? and color = resolve_color(name) "#{@beg}#{color}m#{str}#{@reset}" else str end end + def resolve_color(name = @color, seen = {}) + return unless @colors + @colors[name] || DEFAULTS[name] + end + DEFAULTS.each_key do |name| define_method(name) {|str| decorate(str, name) |