Class: Grepper::Formatter

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

Overview

Description

Grepper::Formatter provides output similar to GNU grep (and probably other greps.

Usage

f = Grepper::Formatter.initialize(grepper)
f.format
puts f.output

Output will show filenames if there are multiple files, and will show before- and after-context. The Formatter doesn’t really do anything except format the information that the Grepper and its result set already have.

For version and license information, see grepper.rb.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grepper) ⇒ Formatter

Returns a new instance of Formatter.



28
29
30
31
# File 'lib/formatter.rb', line 28

def initialize(grepper)
  @output = ""
  @grepper = grepper
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



26
27
28
# File 'lib/formatter.rb', line 26

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



25
26
27
# File 'lib/formatter.rb', line 25

def output
  @output
end

Instance Method Details

#boundary_string_for(m1, m2, conditions_hash) ⇒ Object



62
63
64
65
66
67
# File 'lib/formatter.rb', line 62

def boundary_string_for(m1, m2, conditions_hash)
  conditions = conditions_hash[:conditions]
  return "" unless conditions
  return "" if matches_are_contiguous?(m1, m2)
  return "--\n"
end

#context_lines(lines, file) ⇒ Object



58
59
60
# File 'lib/formatter.rb', line 58

def context_lines(lines,file)
  lines.map {|line| one_or_many(line,"#{file}-") }.join
end

#file_boundary_string_for(f1, f2) ⇒ Object



69
70
71
# File 'lib/formatter.rb', line 69

def file_boundary_string_for(f1, f2)
  f1 &&! (f1 == f2) ? "--\n" : ""
end

#formatObject



41
42
43
# File 'lib/formatter.rb', line 41

def format
  @output = process_grepper(@grepper)
end

#format_line(n, b, l, a) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/formatter.rb', line 45

def format_line(n,b,l,a)
  case
  when options.include?('n')
    "#{n}:#{l}"
  else
    l.dup
  end
end

#matches_are_contiguous?(first, second) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/formatter.rb', line 33

def matches_are_contiguous?(first,second)
  first.absolute_end == second.absolute_start - 1
end

#multi?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/formatter.rb', line 37

def multi?
  @grepper.results.size > 1 &&! @options.include?('h')
end

#one_or_many(string, many_string) ⇒ Object



54
55
56
# File 'lib/formatter.rb', line 54

def one_or_many(string, many_string)
  multi? ? many_string + string : string
end

#process_grepper(grepper) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/formatter.rb', line 73

def process_grepper(grepper)
  str = ""
  last_file = nil
  grepper.results.each do |file,matches|
    if options.include?('l')
      str << "#{file}\n"
    elsif options.include?('c')
      str << one_or_many("#{matches.size}\n", "#{file}:")
    else
      matches.each_with_index do |(n,before,line,after),m|
        if (after || before)
          str << file_boundary_string_for(last_file, file)
        end

        last_file = file

        if before
          prev = matches[m-1]
          str << boundary_string_for(prev, matches[m], :conditions => prev &&! m.zero?)
          str << context_lines(before,file)
        end

        str << one_or_many(format_line(n,before,line,after), "#{file}:")

        if after
          str << context_lines(after,file)
          succ = matches[m+1]
          str << boundary_string_for(matches[m], succ, :conditions => succ)
        end  
      end
    end
  end
  return str
end