Class: Grepresent::FormatterRule

Inherits:
Object
  • Object
show all
Defined in:
lib/grepresent/formatter_rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, format) ⇒ FormatterRule

Returns a new instance of FormatterRule.



6
7
8
9
# File 'lib/grepresent/formatter_rule.rb', line 6

def initialize(pattern, format)
  @pattern = Regexp.new(pattern)
  @format = format
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



4
5
6
# File 'lib/grepresent/formatter_rule.rb', line 4

def format
  @format
end

#patternObject (readonly)

Returns the value of attribute pattern.



4
5
6
# File 'lib/grepresent/formatter_rule.rb', line 4

def pattern
  @pattern
end

Instance Method Details

#apply(string) ⇒ Object



11
12
13
14
15
# File 'lib/grepresent/formatter_rule.rb', line 11

def apply(string)
  string.gsub(pattern) do |m|
    "#{ formats }#{ m }#{ Term::ANSIColor.reset }"
  end
end

#formatsObject



17
18
19
# File 'lib/grepresent/formatter_rule.rb', line 17

def formats
  format.split(',').map { |f| Term::ANSIColor.send(f) }.join('')
end