Class: CureGrepManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cureutils/cure_grep_manager.rb

Overview

Singleton class for cure grep Managing the method for matching and printing.

Defined Under Namespace

Modules: ColorMode

Instance Method Summary collapse

Constructor Details

#initializeCureGrepManager



12
13
14
15
16
17
# File 'lib/cureutils/cure_grep_manager.rb', line 12

def initialize
  @in = $stdin
  @out = $stdout
  @err = $stderr
  @match_method = 'match'
end

Instance Method Details

#option_colorize(colorize) ⇒ Object



24
25
26
# File 'lib/cureutils/cure_grep_manager.rb', line 24

def option_colorize(colorize)
  @str_color = (colorize ? ColorMode::RED : ColorMode::NONE)
end

#option_only(only_flag) ⇒ Object



19
20
21
22
# File 'lib/cureutils/cure_grep_manager.rb', line 19

def option_only(only_flag)
  @only_matched = only_flag
  @match_method = (only_flag ? 'scan' : 'match')
end

#pattern(pat, is_exregex = false) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/cureutils/cure_grep_manager.rb', line 43

def pattern(pat, is_exregex = false)
  if is_exregex
    @pattern = /#{pat}/
    return
  end
  @pattern = /#{Common.pregex2regex(pat)}/
end


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cureutils/cure_grep_manager.rb', line 51

def print_results
  exit_status = 1
  @in.each do |line|
    matched_strs = line.send(@match_method, @pattern)
    next unless matched_strs
    exit_status = 0
    if @only_matched
      matched_strs.each { |str| @out.puts str.send(@str_color) }
    else
      @out.puts line.gsub(@pattern, '\0'.send(@str_color))
    end
  end
  exit_status
end

#source_input(source = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/cureutils/cure_grep_manager.rb', line 32

def source_input(source = nil)
  if source.nil? || source.empty?
    @in = $stdin
  elsif source =~ /^-$/
    # If the file name is "-", use STDIN.
    @in = $stdin
  else
    file(source)
  end
end

#source_output(source = $stdout) ⇒ Object



28
29
30
# File 'lib/cureutils/cure_grep_manager.rb', line 28

def source_output(source = $stdout)
  @out = source
end