module TermColor
Constants
- VERSION
Public Class Methods
colorize(text, color)
click to toggle source
# File lib/termcolor.rb, line 60 def colorize(text, color) parse("<#{color}>#{escape(text)}</#{color}>") end
escape(text)
click to toggle source
# File lib/termcolor.rb, line 19 def escape(text) escape_or_unescape(:escape, text) end
parse(text)
click to toggle source
# File lib/termcolor.rb, line 13 def parse(text) listener = MyListener.new REXML::Parsers::StreamParser.new(prepare_parse(text), listener).parse listener.result end
prepare_parse(text)
click to toggle source
# File lib/termcolor.rb, line 34 def prepare_parse(text) tag_separate text.gsub(/<(\/?)(\d+)>/, '<\1_\2>') end
tag_separate(text)
click to toggle source
# File lib/termcolor.rb, line 38 def tag_separate(text) re = /<(\/*)([^\W_]+)(?:_(on_[^\W_]+))*(?:_with_([^\W_]+))*(?:_and_([^\W_]+))*>/ text.gsub(re) do matchs = $~.captures if matchs.shift.empty? tag = ->t{ "<#{t}>" } else matchs.reverse! tag = ->t{ "</#{t}>" } end matchs.compact.map { |word| tag[word] }.join end end
test(*args)
click to toggle source
# File lib/termcolor.rb, line 52 def test(*args) args = (0..109).to_a if args.empty? args.each_with_index do |color, index| print parse("<#{color}> #{color} </#{color}>") + "\t" puts if (index + 1) % 10 == 0 end end
unescape(text)
click to toggle source
# File lib/termcolor.rb, line 23 def unescape(text) escape_or_unescape(:unescape, text) end
Private Class Methods
escape_or_unescape(dir=:escape, text)
click to toggle source
# File lib/termcolor.rb, line 27 def escape_or_unescape(dir=:escape, text) h = Hash[*%w(& & < < > > ' ' " ")] h = h.invert if dir == :unescape text.gsub(/(#{h.keys.join('|')})/){ h[$1] } end