Class: RubySearch::Formatter

Inherits:
Struct
  • Object
show all
Defined in:
lib/ruby_search/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file

Returns:

  • (Object)

    the current value of file



4
5
6
# File 'lib/ruby_search/formatter.rb', line 4

def file
  @file
end

#matchesObject

Returns the value of attribute matches

Returns:

  • (Object)

    the current value of matches



4
5
6
# File 'lib/ruby_search/formatter.rb', line 4

def matches
  @matches
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



4
5
6
# File 'lib/ruby_search/formatter.rb', line 4

def options
  @options
end

Instance Method Details



21
22
23
24
25
26
27
28
29
# File 'lib/ruby_search/formatter.rb', line 21

def print
  if !matches.empty? || options[:verbose]
    if block_given?
      yield to_str
    else
      puts to_str
    end
  end
end

#to_strObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_search/formatter.rb', line 5

def to_str
  string = ''
  string << "====#{file}\n".colorize(:cyan)
  matches.each do |match, i|
    string << i.to_s
    string << "\t"
    string << [
      match.pre_match,
      match.to_s.colorize(:red),
      match.post_match
    ].join.chomp
    string << "\n"
  end
  string
end