aboutsummaryrefslogtreecommitdiffstats
path: root/tool
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-04-21 03:01:12 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-04-21 03:01:12 +0000
commitb811b97458bd92de58d97b9ba400ae076c7c5967 (patch)
treef35a75b2fd1550b09cc9aaaff59af047bde85528 /tool
parent501a865af44db26d80495146325bdd564df52e60 (diff)
downloadruby-b811b97458bd92de58d97b9ba400ae076c7c5967.tar.gz
ext/extmk.rb: colorize notes [Feature #13302]
* common.mk (build-ext): pass variables to colorize. * ext/extmk.rb: colorize notes with tool/colorize.rb. * tool/colorize.rb: extract from tool/generic_erb.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58426 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'tool')
-rw-r--r--tool/colorize.rb41
-rw-r--r--tool/generic_erb.rb16
2 files changed, 46 insertions, 11 deletions
diff --git a/tool/colorize.rb b/tool/colorize.rb
new file mode 100644
index 0000000000..6b9f568020
--- /dev/null
+++ b/tool/colorize.rb
@@ -0,0 +1,41 @@
+class Colorize
+ def initialize(color = nil)
+ @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]*)/)] : {}
+ @reset = "#{@beg}m"
+ end
+ end
+ self
+ end
+
+ DEFAULTS = {"pass"=>"32;1", "fail"=>"31;1", "skip"=>"33;1"}
+
+ def decorate(str, name)
+ if @colors and color = (@colors[name] || DEFAULTS[name])
+ "#{@beg}#{color}m#{str}#{@reset}"
+ else
+ str
+ end
+ end
+
+ def pass(str)
+ decorate(str, "pass")
+ end
+
+ def fail(str)
+ decorate(str, "fail")
+ end
+
+ def skip(str)
+ decorate(str, "skip")
+ end
+end
+
+if $0 == __FILE__
+ colorize = Colorize.new
+ col = ARGV.shift
+ ARGV.each {|str| puts colorize.decorate(str, col)}
+end
diff --git a/tool/generic_erb.rb b/tool/generic_erb.rb
index d08efe06c3..08077f2dbc 100644
--- a/tool/generic_erb.rb
+++ b/tool/generic_erb.rb
@@ -8,6 +8,7 @@ require 'optparse'
require 'fileutils'
$:.unshift(File.dirname(__FILE__))
require 'vpath'
+require 'colorize'
vpath = VPath.new
timestamp = nil
@@ -25,17 +26,10 @@ opt = OptionParser.new do |o|
vpath.def_options(o)
o.order!(ARGV)
end
-unchanged = "unchanged"
-updated = "updated"
-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]*)/)] : {}
- reset = "#{beg}m"
- unchanged = "#{beg}#{colors["pass"] || "32;1"}m#{unchanged}#{reset}"
- updated = "#{beg}#{colors["fail"] || "31;1"}m#{updated}#{reset}"
- end
-end
+color = Colorize.new(color)
+unchanged = color.pass("unchanged")
+updated = color.fail("updated")
+
template = ARGV.shift or abort opt.to_s
erb = ERB.new(File.read(template), nil, '%-')
erb.filename = template