Method: Regexp::Template#describe

Defined in:
lib/core_ext/regexp.rb

#describe(atom) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/core_ext/regexp.rb', line 151

def describe(atom)
  matchers = {}
  match_count = "exactly 1 instance"
  if(atom.to_s[atom.to_s.length-1] == "*")
    match_count = "any number of"
  elsif(atom.to_s[atom.to_s.length-1] == "?")
    match_count = "zero or one"
  elsif(atom.to_s[atom.to_s.length-1] == "+")
    match_count = "1 or more"
  elsif(atom.to_s.match(/\{(?<count>.*)\}\z/))
    match_count_data = atom.to_s.match(/\{(?<count>.*)\}\z/)[:count]
    if(match_count_data.split(',').length == 2)
      match_count = "between #{match_count_data.split(',').first} and #{match_count_data.split(',').last}"
    elsif(match_count_data > 1)
      match_count = "exactly #{match_count_data}"
    end
  end

  if(atom.to_s.include?("literal_"))
    desc = "#{atom.to_s.gsub("_"," ")}"
    color= :yellow
  elsif(atom.to_s[0] == "(" && atom.to_s[atom.length-1] == ")")
    desc = "#{self.preview(atom.to_s[1,atom.length-1],:capture)}"
  elsif(atom.to_s[0] == "\\" && self.class::MATCHERS.keys.include?(atom.to_s[1]))
    desc = "#{self.class::MATCHERS[atom.to_s[1]]} characters"
    color= :yellow
  elsif(@type_matchers.values.include?(atom.to_s))
    matcher_name = (@type_matchers.select{|k| @type_matchers[k] == atom.to_s}.first).first.to_s
    desc  = "#{matcher_name}"
    type_matcher = true
    color = {r:4,b:0,g:4}
  elsif(atom.to_s[0] == "\\" && atom.length <= 3 && !self.class::MATCHERS.keys.include?(atom.to_s[1]))
    desc = "literal #{atom}"
    color= :yellow
  else
    desc = @terminal.rbg(r:0,b:5,g:0, text:"\"#{atom}\"")
  end
  if(!type_matcher)
    desc = "#{match_count} of #{@terminal.add_formatting(desc, color)}"
    return desc.gsub(" of of "," of ")
  else
    desc[0] = desc[0].upcase
    if("aeiou".include?(desc[0,1].downcase))
      article = "an"
    else
      article = "a"
    end
    if(!!color && color.is_a?(Hash))
      color[:text] = desc
      desc = "#{article} #{@terminal.rbg(**color)}"
    elsif(!!color)
      desc = "#{article} #{@terminal.add_formatting(desc, color)}"
    else
      desc = "#{article} #{desc}."
    end
  end
end