Sha256: dabcb319322982e263a302864b905b18f76ee11c724b549aadfff19a1874647d

Contents?: true

Size: 1001 Bytes

Versions: 4

Compression:

Stored size: 1001 Bytes

Contents

module PutsDebuggerer
  class SourceFile
    def initialize(file_path)
      @file = File.new(file_path) if file_path
    end
    
    def source(source_line_count, source_line_number)
      @source = ''
      return @source if RUBY_ENGINE == 'opal'
      # For Opal Ruby compatibility, skip source lines if file does not respond to readline (as in Opal)
      lines = source_lines(source_line_count, source_line_number)
      @source = lines.join(' '*5) if @file.respond_to?(:readline)
      @source
    end
    
    def source_lines(source_line_count, source_line_number)
      lines = []
      begin
        while @file.lineno < source_line_number + source_line_count
          file_line_number = @file.lineno + 1
          file_line = @file.readline
          if file_line_number >= source_line_number && file_line_number < source_line_number + source_line_count
            lines << file_line
          end
        end
      rescue EOFError
        # Done
      end
      lines
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
puts_debuggerer-0.13.1 lib/puts_debuggerer/source_file.rb
puts_debuggerer-0.13.0 lib/puts_debuggerer/source_file.rb
puts_debuggerer-0.12.0 lib/puts_debuggerer/source_file.rb
puts_debuggerer-0.11.0 lib/puts_debuggerer/source_file.rb