Sha256: 0853727ff33ac1f727808f01fd2705da20181a1204ea817c2c745e56cebce3b1

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

module Vedeu

  # Stores and manipulates the position of the current cursor.
  #
  # @api private
  class Cursor

    # @return [Fixnum]
    attr_reader :top

    # @return [Fixnum]
    attr_reader :bottom

    # @return [Fixnum]
    attr_reader :left

    # @return [Fixnum]
    attr_reader :right

    # @return [Fixnum]
    attr_reader :cursor_x

    # @return [Fixnum]
    attr_reader :cursor_y

    # Provides a new instance of Cursor.
    #
    # @param attributes [Hash]
    # @return [Cursor]
    def initialize(attributes = {})
      @attributes = attributes

      @top        = attributes.fetch(:top)
      @bottom     = attributes.fetch(:bottom)
      @left       = attributes.fetch(:left)
      @right      = attributes.fetch(:right)
      @cursor_y   = attributes.fetch(:cursor_y, @top)
      @cursor_x   = attributes.fetch(:cursor_x, @left)
    end

    # Reports the position of the cursor.
    #
    # @return [Array]
    def position
      [ @cursor_y, @cursor_x ]
    end

    # Move the cursor up one row.
    #
    # @return [Cursor]
    def move_up
      unless @cursor_y == top || @cursor_y - 1 < top
        @cursor_y -= 1
      end

      self
    end

    # Move the cursor down one row.
    #
    # @return [Cursor]
    def move_down
      unless @cursor_y == bottom || @cursor_y + 1 > bottom
        @cursor_y += 1
      end

      self
    end

    # Move the cursor left one column.
    #
    # @return [Cursor]
    def move_left
      unless @cursor_x == left || @cursor_x - 1 < left
        @cursor_x -= 1
      end

      self
    end

    # Move the cursor right one column.
    #
    # @return [Cursor]
    def move_right
      unless @cursor_x == right || @cursor_x + 1 > right
        @cursor_x += 1
      end

      self
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.2.0 lib/vedeu/support/cursor.rb