Module: SmartColored

Included in:
String
Defined in:
lib/smart_colored.rb

Defined Under Namespace

Classes: String

Constant Summary collapse

COLOR_CODES =
{
  :black   => 30,
  :red     => 31,
  :green   => 32,
  :yellow  => 33,
  :blue    => 34,
  :magenta => 35,
  :cyan    => 36,
  :white   => 37
}
FORMAT_CODES =
{
  :bold      => 1,
  :underline => 4,
  :inverse   => 7
}
CLEAR_SEQUENCE =
"\e[0m"
PART_REGEXP =
"(?:on_)?(?:#{COLOR_CODES.keys.join('|')})|#{FORMAT_CODES.keys.join('|')}"
COMBINED_REGEXP =
/^(?:(?:#{PART_REGEXP})_){2,}$/
COMBINED_REGEXP_PART =
/#{PART_REGEXP}/
@@name_to_attributes =
{}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/smart_colored.rb', line 102

def method_missing(method, *arguments, &block)
  if (method_s = "#{method}_") =~ COMBINED_REGEXP
    attributes = @@name_to_attributes.values_at(*method_s.scan(COMBINED_REGEXP_PART).map(&:to_sym)).inject(&:merge)
    SmartColored.map_name_to_attributes method, attributes
    apply_format(attributes)
  else
    super
  end
end

Class Method Details

.map_name_to_attributes(name, attributes) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/smart_colored.rb', line 82

def self.map_name_to_attributes(name, attributes)
  name = name.to_sym
  @@name_to_attributes[name] = attributes
  define_method name do
    apply_format(attributes)
  end
end

Instance Method Details

#apply_format(format = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/smart_colored.rb', line 27

def apply_format(format = {})
  apply_attributes = {}
  if color = COLOR_CODES[format[:color]]
    apply_attributes[:color] = color
  end
  if background = COLOR_CODES[format[:background]]
    apply_attributes[:background] = background + 10
  end
  FORMAT_CODES.each do |key, value|
    if format[key]
      apply_attributes[value] = true
    end
  end

  previous_attributes = {}
  clear_sequence = false
  str = "#{CLEAR_SEQUENCE}#{self}"
  str.gsub!(/(?:\e\[\d+(?:;\d+)*m)+/) do |m|
    unless $'.empty?
      codes = m.scan(/\d+/).map(&:to_i).uniq
      sequence_attributes = {}
      codes.each do |code|
        case code
        when 0
        when 30..37
          sequence_attributes[:color] = code
        when 40..47
          sequence_attributes[:background] = code
        else
          sequence_attributes[code] = true
        end
      end

      current_attributes = apply_attributes.merge(codes.include?(0) ? sequence_attributes : previous_attributes.merge(sequence_attributes))
      sequence_attributes = if (previous_attributes.keys - current_attributes.keys).empty?
        current_attributes.to_a - previous_attributes.to_a
      else
        current_attributes.merge(0 => true)
      end
      codes = sequence_attributes.map do |key, value|
        key == :color || key == :background ? value : key
      end
      previous_attributes = current_attributes

      unless codes.empty?
        clear_sequence = true
        "\e[#{codes.sort.join(';')}m"
      end
    end
  end
  str << CLEAR_SEQUENCE if clear_sequence
  self.class.new(str)
end

#widthObject



23
24
25
# File 'lib/smart_colored.rb', line 23

def width
  gsub(/\e\[\d+(?:;\d+)*m/, '').length
end