Sha256: 678df805e22e920535db590cc125d08b8caf3bb5de89f11b2bd472a8eb0cff8b

Contents?: true

Size: 939 Bytes

Versions: 2

Compression:

Stored size: 939 Bytes

Contents

# frozen_string_literal: true

module RubyJard
  module Decorators
    ##
    # Decorator to decorate a file of source code
    # It loads a window of source code that centers the current line position.
    class SourceDecorator
      attr_reader :codes, :window_start, :window_end

      def initialize(file, lineno, window)
        @file = file
        @lineno = lineno
        @window = window
        @codes = []

        decorate
      end

      def decorate
        begin
          file = File.open(@file)
        rescue Errno::ENOENT
          return
        end

        @window_start = @lineno - @window / 2
        @window_start = 1 if @window_start <= 0
        @window_end = @window_start + @window

        until file.eof?
          loc = file.gets
          next if file.lineno < @window_start
          break if @window_end < file.lineno

          @codes << loc
        end

        file.close
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_jard-0.2.2 lib/ruby_jard/decorators/source_decorator.rb
ruby_jard-0.2.1 lib/ruby_jard/decorators/source_decorator.rb