Class: NodeGrep::Presenter
- Inherits:
-
Object
- Object
- NodeGrep::Presenter
- Defined in:
- lib/nodegrep/presenter.rb
Defined Under Namespace
Classes: OutOfBounds
Instance Attribute Summary collapse
-
#color_output ⇒ Object
Returns the value of attribute color_output.
-
#line_numbers ⇒ Object
Returns the value of attribute line_numbers.
Instance Method Summary collapse
- #get_lines(x, y) ⇒ Object
-
#initialize(source) ⇒ Presenter
constructor
A new instance of Presenter.
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_output ⇒ Object
Returns the value of attribute color_output.
9 10 11 |
# File 'lib/nodegrep/presenter.rb', line 9 def color_output @color_output end |
#line_numbers ⇒ Object
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
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 |