module Colored
require ‘Win32/Console/ANSI’ if RUBY_PLATFORM =~ /win32/
Constants
- BBS_COLOR_TABLE
BBS-style numeric color codes.
- COLORS
- EXTRAS
- VALID_COLORS
Public Instance Methods
Returns the terminal code for a specified color.
# File lib/epitools/colored.rb, line 223 def color(color_name) background = color_name.to_s =~ /on_/ color_name = color_name.to_s.sub('on_', '') return unless color_name && COLORS[color_name] "\e[#{COLORS[color_name] + (background ? 10 : 0)}m" end
Colorize a string (this method is called by red, blue, red_on_green, etc.)
Accepts options:
:foreground The name of the foreground color as a string. :background The name of the background color as a string. :extra Extra styling, like 'bold', 'light', 'underline', 'reversed', or 'clear'.
With no options, it uses tagged colors:
puts "<light_green><magenta>*</magenta> Hey mom! I am <light_blue>SO</light_blue> colored right now.</light_green>".colorize
Or numeric ANSI tagged colors (from the BBS days):
puts "<10><5>*</5> Hey mom! I am <9>SO</9> colored right now.</10>".colorize
# File lib/epitools/colored.rb, line 175 def colorize(string=nil, options = {}) if string == nil return self.tagged_colors end if @@is_tty colored = [color(options[:foreground]), color("on_#{options[:background]}"), extra(options[:extra])].compact * '' colored << string colored << extra(:clear) else string end end
An array of all possible colors.
# File lib/epitools/colored.rb, line 208 def colors @@colors ||= COLORS.keys.sort end
Color commands will do nothing.
# File lib/epitools/colored.rb, line 263 def disable! @@is_tty = false end
Color commands will always produce colored strings, regardless of whether the script is being run in a terminal.
# File lib/epitools/colored.rb, line 243 def enable! @@is_tty = true end
Enable Colored
just for this block.
# File lib/epitools/colored.rb, line 252 def enable_temporarily(&block) last_state = @@is_tty @@is_tty = true block.call @@is_tty = last_state end
Will color commands actually modify the strings?
# File lib/epitools/colored.rb, line 233 def enabled? @@is_tty end
Returns the terminal code for one of the extra styling options.
# File lib/epitools/colored.rb, line 215 def extra(extra_name) extra_name = extra_name.to_s "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name] end
Find all occurrences of “pattern” in the string and highlight them with the specified color. (defaults to light_yellow)
The pattern can be a string or a regular expression.
# File lib/epitools/colored.rb, line 195 def highlight(pattern, color=:light_yellow, &block) pattern = Regexp.new(Regexp.escape(pattern)) if pattern.is_a? String if block_given? gsub(pattern, &block) else gsub(pattern) { |match| match.send(color) } end end
Colorize a string that has “color tags”.
# File lib/epitools/colored.rb, line 278 def tagged_colors stack = [] # split the string into tags and literal strings tokens = self.split(/(<\/?[\w\d_]+>)/) tokens.delete_if { |token| token.size == 0 } result = "" tokens.each do |token| # token is an opening tag! if /<([\w\d_]+)>/ =~ token and valid_tag?($1) stack.push $1 # token is a closing tag! elsif /<\/([\w\d_]+)>/ =~ token and valid_tag?($1) # if this color is on the stack somwehere... if pos = stack.rindex($1) # close the tag by removing it from the stack stack.delete_at pos else raise "Error: tried to close an unopened color tag -- #{token}" end # token is a literal string! else color = (stack.last || "white") color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/ result << token.send(color) end end result end
Is this string legal?
# File lib/epitools/colored.rb, line 270 def valid_tag?(tag) VALID_COLORS.include?(tag) or (tag =~ /^\d+$/ and BBS_COLOR_TABLE.include?(tag.to_i) ) end