Class: Lines

Inherits:
Common show all
Defined in:
lib/glark/output/lines.rb

Direct Known Subclasses

Glark::Lines, Grep::Lines

Instance Attribute Summary

Attributes inherited from Formatted

#formatted

Attributes inherited from Results

#count

Instance Method Summary collapse

Methods inherited from Common

#add_match

Methods inherited from Formatted

#get_line_to_print

Methods inherited from Results

#add_match, #matched?

Constructor Details

#initialize(file, spec) ⇒ Lines

Returns a new instance of Lines.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/glark/output/lines.rb', line 12

def initialize file, spec
  super

  @print_context = false
  
  context = spec.context
  @after = context && context.after
  @before = context && context.before

  @stati = Glark::LineStatus.new
end

Instance Method Details

#at_match_limit?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/glark/output/lines.rb', line 28

def at_match_limit?
  @match_limit && @count >= @match_limit
end

#display_matches?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/glark/output/lines.rb', line 24

def display_matches?
  true
end

#displayed_nameObject



32
33
34
# File 'lib/glark/output/lines.rb', line 32

def displayed_name
  @label || @file.fname
end

prints the line, and adjusts for the fact that in our world, lines are 0-indexed, whereas they are displayed as if 1-indexed.



44
45
46
# File 'lib/glark/output/lines.rb', line 44

def print_line lnum, ch = nil 
  raise "error: print_line must be implemented by an output subclass"
end

Prints the line, which is assumed to be 0-indexed, and is thus adjusted by one.



38
39
40
# File 'lib/glark/output/lines.rb', line 38

def print_line_number lnum 
  @out.printf "%5d ", lnum + 1
end

#process_end(lnum) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/glark/output/lines.rb', line 78

def process_end lnum
  if @invert_match
    write_nonmatching 0, lnum
  elsif matched?
    write_matching 0, lnum
  end
end

#process_match(startline, endline, fromline, toline) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/glark/output/lines.rb', line 86

def process_match startline, endline, fromline, toline
  add_match startline, endline
  
  if display_matches?
    write_matches fromline, toline
  end
  return at_match_limit?
end

#set_status(startline, endline) ⇒ Object



95
96
97
98
99
# File 'lib/glark/output/lines.rb', line 95

def set_status startline, endline
  fromline = [0, startline - @before].max
  toline = endline + @after
  @stati.set_match fromline, startline, endline, toline
end

#write_matches(from, to) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/glark/output/lines.rb', line 70

def write_matches from, to 
  if @invert_match
    write_nonmatching from, to
  else
    write_matching from, to
  end
end

#write_matching(from, to) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/glark/output/lines.rb', line 48

def write_matching from, to
  (from .. to).each do |ln|
    next unless @stati.char(ln) && !@stati.is_written?(ln)

    # this used to be conditional on show_break, but no more
    if from > 0 && !@stati.char(ln - 1) && @print_context
      @out.puts "  ---"
    end
    
    print_line ln, @stati.char(ln)
    @stati.set_as_written ln
  end
end

#write_nonmatching(from, to) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/glark/output/lines.rb', line 62

def write_nonmatching from, to
  (from .. to).each do |ln|
    next if @stati.is_written?(ln) || @stati.char(ln) == ':'
    print_line ln 
    @stati.set_as_written ln
  end
end