Class: String

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

Instance Method Summary collapse

Instance Method Details

#c(*codes) ⇒ Object



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

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



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

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

#indent(count, char = ' ') ⇒ Object



72
73
74
# File 'lib/earthquake/ext.rb', line 72

def indent(count, char = ' ')
  (char * count) + gsub(/(\n+)/) { |m| m + (char * count) }
end

#trim_indentObject



76
77
78
79
80
# File 'lib/earthquake/ext.rb', line 76

def trim_indent
    lines = self.split("\n")
    unindent = self.split("\n").select { |s| s !~ /^\s$/ }.map { |s| s.index(/[^\s]/) || 0 }.min
    lines.map { |s| s.gsub(/^#{' ' * unindent}/, '') }.join("\n")
end