Sha256: 8379fe06a6d54e312039fc79f696b57fc5f4cc4fada99448131a225b3cc6955a

Contents?: true

Size: 1.57 KB

Versions: 8

Compression:

Stored size: 1.57 KB

Contents

module Vedeu

  module Editor

    # Crop the lines to the visible area of the document, as defined by the
    # geometry provided.
    #
    class Cropper

      # Returns a new instance of Vedeu::Editor::Cropper.
      #
      # @param lines [Vedeu::Editor::Lines]
      # @param height [Fixnum]
      # @param width [Fixnum]
      # @param ox [Fixnum]
      # @param oy [Fixnum]
      # @return [Vedeu::Editor::Cropper]
      def initialize(lines:, height:, width:, ox:, oy:)
        @lines  = lines
        @height = height
        @width  = width
        @ox     = ox
        @oy     = oy
      end

      # Returns the lines cropped.
      #
      # @note If there are no lines of content, we return an empty array. If
      # there are any empty lines, then they are discarded.
      #
      # @return [Array<void>]
      def cropped
        lines.map { |line| columns(line) }
      end

      protected

      # @!attribute [r] height
      # @return [Fixnum]
      attr_reader :height

      # @!attribute [r] width
      # @return [Fixnum]
      attr_reader :width

      # @!attribute [r] ox
      # @return [Fixnum]
      attr_reader :ox

      # @!attribute [r] oy
      # @return [Fixnum]
      attr_reader :oy

      private

      # Return a range of visible lines.
      #
      # @return [Vedeu::Editor::Lines]
      def lines
        @lines[oy...(oy + height)] || []
      end

      # Return a range of visible characters from each line.
      #
      # @return [String]
      def columns(line)
        line[ox...(ox + width)] || ''
      end

    end # Editor

  end # Cropper

end # Vedeu

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vedeu-0.6.8 lib/vedeu/editor/cropper.rb
vedeu-0.6.7 lib/vedeu/input/editor/cropper.rb
vedeu-0.6.6 lib/vedeu/input/editor/cropper.rb
vedeu-0.6.5 lib/vedeu/input/editor/cropper.rb
vedeu-0.6.4 lib/vedeu/input/editor/cropper.rb
vedeu-0.6.3 lib/vedeu/input/editor/cropper.rb
vedeu-0.6.2 lib/vedeu/input/editor/cropper.rb
vedeu-0.6.1 lib/vedeu/input/editor/cropper.rb