Class: FileLinePresenter

Inherits:
Object show all
Defined in:
lib/file_line_presenter.rb

Constant Summary collapse

TAB =
"  "

Class Method Summary collapse

Class Method Details

.file_length(file_line) ⇒ Object



39
40
41
# File 'lib/file_line_presenter.rb', line 39

def self.file_length(file_line)
  `wc -l #{file_line.path}`.strip.split(" ")[0].to_i
end

.numbered_lines_at(indices, path) ⇒ Object



23
24
25
26
27
# File 'lib/file_line_presenter.rb', line 23

def self.numbered_lines_at(indices, path)
  indices.map do |line_number|
    with_line_number(FileLine.new(line_number, path))
  end
end

.present_contents(file_line, pattern, context = 0) ⇒ Object



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

def self.present_contents(file_line, pattern, context = 0)
  current_highlighted_line = with_line_number(file_line, with_highlighting(file_line, pattern))
  line_number = file_line.line_number
  line_path = file_line.path
  lower_limit = [line_number - context, 1].max
  upper_limit = [line_number + context, file_length(file_line)].min
  above_line_indices = (lower_limit...line_number)
  below_line_indices = (line_number + 1..upper_limit)

  above_lines = numbered_lines_at(above_line_indices, line_path)
  below_lines = numbered_lines_at(below_line_indices, line_path)
  (above_lines + [current_highlighted_line] + below_lines).join("\n")
end

.with_highlighting(file_line, pattern) ⇒ Object



33
34
35
36
37
# File 'lib/file_line_presenter.rb', line 33

def self.with_highlighting(file_line, pattern)
  file_line.raw_contents.gsub!(/#{pattern}/) do |match|
    match.red
  end
end

.with_line_number(file_line, contents = file_line.raw_contents) ⇒ Object



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

def self.with_line_number(file_line, contents = file_line.raw_contents)
  "#{file_line.line_number}:#{TAB}#{contents}"
end