class String

Public Instance Methods

c(*codes) click to toggle source
# File lib/earthquake/ext.rb, line 21
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) click to toggle source
# File lib/earthquake/ext.rb, line 33
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 = ' ') click to toggle source
# File lib/earthquake/ext.rb, line 57
def indent(count, char = ' ')
  (char * count) + gsub(/(\n+)/) { |m| m + (char * count) }
end
trim_indent() click to toggle source
# File lib/earthquake/ext.rb, line 61
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