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



12
13
14
# File 'lib/nodegrep/presenter.rb', line 12

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.



7
8
9
# File 'lib/nodegrep/presenter.rb', line 7

def color_output
  @color_output
end

#line_numbersObject

Returns the value of attribute line_numbers.



7
8
9
# File 'lib/nodegrep/presenter.rb', line 7

def line_numbers
  @line_numbers
end

Instance Method Details

#get_lines(x, y) ⇒ Object

Raises:



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

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 = ""
    output << "#{i + 1}: " if @line_numbers
    output << @source_lines[i]
    output
  end
  formatted_lines.join("\n")
end