Class: NodeGrep::Presenter

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

Defined Under Namespace

Classes: OutOfBounds

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Presenter

Returns a new instance of Presenter.



14
15
16
# File 'lib/nodegrep/presenter.rb', line 14

def initialize(source)
  @source_lines = source.split("\n").map { |l| l.gsub("\r", '') }
end

Instance Attribute Details

#color_outputObject

Returns the value of attribute color_output.



9
10
11
# File 'lib/nodegrep/presenter.rb', line 9

def color_output
  @color_output
end

#line_numbersObject

Returns the value of attribute line_numbers.



9
10
11
# File 'lib/nodegrep/presenter.rb', line 9

def line_numbers
  @line_numbers
end

Instance Method Details

#get_lines(x, y) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nodegrep/presenter.rb', line 18

def get_lines(x, y)
  num_lines = @source_lines.length
  raise OutOfBounds if x < 1 || y < 1 || x > y
  raise OutOfBounds if x > num_lines || y > num_lines

  x -= 1
  y -= 1
  formatted_lines = (x..y).each.collect do |i|
    output = String.new
    output << "#{i + 1}: " if @line_numbers
    output << @source_lines[i]
    output
  end
  formatted_lines.join("\n")
end