class Rubygrep::Outputter

Constants

COLORS

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/rubygrep/outputter.rb, line 8
def initialize(options = {})
  @options = options
end

Public Instance Methods

error(message, line_data) click to toggle source
# File lib/rubygrep/outputter.rb, line 16
def error(message, line_data)
  $stdout << "#{colorize(line_data[:path])}: #{message}\n"
end
out(match_data, line_data) click to toggle source
# File lib/rubygrep/outputter.rb, line 12
def out(match_data, line_data)
  $stdout << format_with_options(match_data, line_data)
end

Private Instance Methods

colorize(string, color = :red) click to toggle source
# File lib/rubygrep/outputter.rb, line 33
def colorize(string, color = :red)
  "\033[#{COLORS[color]}m#{string}\033[0m"
end
colorize_match(match_data, string) click to toggle source
# File lib/rubygrep/outputter.rb, line 37
def colorize_match(match_data, string)
  string.gsub(match_data.regexp) do |match|
    colorize(match)
  end
end
format_with_options(match_data, line_data) click to toggle source
# File lib/rubygrep/outputter.rb, line 22
def format_with_options(match_data, line_data)
  result = colorize_match(match_data, "#{line_data[:str]}")
  if options[:line_number]
    result = "#{colorize(line_data[:str_num], :magenta)}: #{result}"
  end
  if options[:with_filename]
    result = "#{colorize(line_data[:path], :magenta)}: #{result}"
  end
  result.include?("\n") ? result : "#{result}\n"
end