class ConsoleUtil::Color
Public Class Methods
const_missing(const)
click to toggle source
Used to create magic constants for every supported color combination.
Calls superclass method
# File lib/console_util/color.rb, line 31 def const_missing(const) # Attempt to parse the method name as a color string. color_escape_code = convert_color_string(const.to_s) if color_escape_code # Define a constant for the ANSI escape code that corresponds to the # specified color and return it. self.class_eval <<-COLOR_CONST, __FILE__, __LINE__ + 1 #{const} = "#{color_escape_code}" COLOR_CONST else super end end
valid_escape_code?(color_escape_code)
click to toggle source
# File lib/console_util/color.rb, line 46 def valid_escape_code?(color_escape_code) !color_escape_code.match(/^\e\[[0-9;]*m$/).nil? end
Private Class Methods
convert_color_string(color_string)
click to toggle source
# File lib/console_util/color.rb, line 51 def convert_color_string(color_string) bright = nil fg = nil bg = nil # Parse the color string; supported formats are: # - FG # - FG_ON_BG # - ON_BG # - BRIGHT_FG # - BRIGHT_FG_ON_BG case color_string when /^ON_([A-Z]+)$/ bg = @@colors.index($1.downcase.to_sym) return nil if bg.nil? when /^(BRIGHT_)?([A-Z]+)(_ON_([A-Z]+))?$/ bright = !$1.nil? fg = @@colors.index($2.downcase.to_sym) return nil if fg.nil? if $4 bg = @@colors.index($4.downcase.to_sym) return nil if bg.nil? end else return nil end # Convert the color to an ANSI escape code sequence that sets the current # console color. color_codes = [] color_codes << (bright ? @@color_codes[:bright] : @@color_codes[:normal]) unless bright.nil? color_codes << (fg + @@fg_color_base).to_s if fg color_codes << (bg + @@bg_color_base).to_s if bg "\e[#{color_codes.join(';')}m" end