Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/earthquake/ext.rb

Instance Method Summary collapse

Instance Method Details

#c(*codes) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/earthquake/ext.rb', line 34

def c(*codes)
  codes = codes.flatten.map { |code|
    case code
    when String, Symbol
      Earthquake.config[:color][code.to_sym] rescue nil
    else
      code
    end
  }.compact.unshift(0)
  "\e[#{codes.join(';')}m#{self}\e[0m"
end

#coloring(pattern, color = nil, &block) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/earthquake/ext.rb', line 46

def coloring(pattern, color = nil, &block)
  self.gsub(pattern) do |i|
    applied_colors = self[0...$~.begin(0)].scan(/\e\[[\d;]+m/)
    c = color || block.call(i)
    "#{i.c(c)}#{applied_colors.join}"
  end
end

#eObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/earthquake/ext.rb', line 66

def e
  gsub(/[&<>'"]/) do |s|
    case s
      when '&' then '&amp;'
      when '<' then '&lt;'
      when '>' then '&gt;'
      when "'" then '&apos;'
      when '"' then '&quot;'
    end
  end
end

#uObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/earthquake/ext.rb', line 54

def u
  gsub(/&(lt|gt|amp|quot|apos);/) do |s|
    case s
      when '&amp;' then '&'
      when '&lt;' then '<'
      when '&gt;' then '>'
      when '&apos;' then "'"
      when '&quot;' then '"'
    end
  end
end