summaryrefslogtreecommitdiff
path: root/tool/lib
diff options
context:
space:
mode:
authorHiroshi SHIBATA <hsbt@ruby-lang.org>2019-07-15 08:54:34 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2019-07-15 09:40:22 +0900
commite8ddbc0239b9dfa32787e93c6942f085e5c42b49 (patch)
tree64bd46935c5d71649908b77768cb0777e4c6041e /tool/lib
parent0a711b0edff6eaf978cfc17cdd6a7cc6c17c6686 (diff)
Put colorize to library directory.
Same as 66299e7ca83d379d13abaa5411f3e0419334cabb
Diffstat (limited to 'tool/lib')
-rw-r--r--tool/lib/colorize.rb50
-rw-r--r--tool/lib/test/unit.rb2
2 files changed, 51 insertions, 1 deletions
diff --git a/tool/lib/colorize.rb b/tool/lib/colorize.rb
new file mode 100644
index 0000000000..855e1331ad
--- /dev/null
+++ b/tool/lib/colorize.rb
@@ -0,0 +1,50 @@
+# frozen-string-literal: true
+
+class Colorize
+ def initialize(color = nil, opts = ((_, color = color, nil)[0] if Hash === color))
+ @colors = @reset = nil
+ if color or (color == nil && STDOUT.tty?)
+ if (/\A\e\[.*m\z/ =~ IO.popen("tput smso", "r", :err => IO::NULL, &:read) rescue nil)
+ @beg = "\e["
+ colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:\n]*)/)] : {}
+ if opts and colors_file = opts[:colors_file]
+ begin
+ File.read(colors_file).scan(/(\w+)=([^:\n]*)/) do |n, c|
+ colors[n] ||= c
+ end
+ rescue Errno::ENOENT
+ end
+ end
+ @colors = colors
+ @reset = "#{@beg}m"
+ end
+ end
+ self
+ end
+
+ DEFAULTS = {
+ "pass"=>"32", "fail"=>"31;1", "skip"=>"33;1",
+ "black"=>"30", "red"=>"31", "green"=>"32", "yellow"=>"33",
+ "blue"=>"34", "magenta"=>"35", "cyan"=>"36", "white"=>"37",
+ }
+
+ def decorate(str, name)
+ if @colors and color = (@colors[name] || DEFAULTS[name])
+ "#{@beg}#{color}m#{str}#{@reset}"
+ else
+ str
+ end
+ end
+
+ DEFAULTS.each_key do |name|
+ define_method(name) {|str|
+ decorate(str, name)
+ }
+ end
+end
+
+if $0 == __FILE__
+ colorize = Colorize.new
+ col = ARGV.shift
+ ARGV.each {|str| puts colorize.decorate(str, col)}
+end
diff --git a/tool/lib/test/unit.rb b/tool/lib/test/unit.rb
index 5a980603a5..d237a9a0e9 100644
--- a/tool/lib/test/unit.rb
+++ b/tool/lib/test/unit.rb
@@ -6,7 +6,7 @@ end
require 'minitest/unit'
require 'test/unit/assertions'
require_relative '../envutil'
-require_relative '../../../tool/colorize'
+require_relative '../colorize'
require 'test/unit/testcase'
require 'optparse'