Class: String

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

Instance Method Summary collapse

Instance Method Details

#c(*codes) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/earthquake/ext.rb', line 23

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



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

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



59
60
61
# File 'lib/earthquake/ext.rb', line 59

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

#trim_indentObject



63
64
65
66
67
# File 'lib/earthquake/ext.rb', line 63

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