Class: String

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

Instance Method Summary collapse

Instance Method Details

#c(*codes) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/nineteen_ninetynine/ext.rb', line 2

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



14
15
16
17
18
19
20
# File 'lib/nineteen_ninetynine/ext.rb', line 14

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



38
39
40
# File 'lib/nineteen_ninetynine/ext.rb', line 38

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

#trim_indentObject



42
43
44
45
46
# File 'lib/nineteen_ninetynine/ext.rb', line 42

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