Sha256: 496828f261fd08697b2fa01387791dc57dd53694e3affb723aaae4a40e7a94f9

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

module Vedeu

  # Change coordinates into an escape sequence to set the cursor position.
  #
  # @api private
  #
  class Position

    attr_reader  :y,
      :x

    alias_method :first, :y
    alias_method :last, :x

    # @param value [Array<Fixnum>|Vedeu::Position]
    # @return [void]
    def self.coerce(value)
      if value.is_a?(self)
        value

      elsif value.is_a?(Array)
        new(*value)

      else
        # not sure how to proceed

      end
    end

    # Initializes a new instance of Position.
    #
    # @param y [Fixnum] The row/line position.
    # @param x [Fixnum] The column/character position.
    # @return [Position]
    def initialize(y = 1, x = 1)
      @y = (y.nil? || y < 1) ? 1 : y
      @x = (x.nil? || x < 1) ? 1 : x
    end

    # @return [String]
    def inspect
      "<#{self.class.name} (y:#{@y} x:#{@x})>"
    end

    # @param other [Vedeu::Position]
    # @return [Boolean]
    def ==(other)
      eql?(other)
    end

    # @param other [Vedeu::Position]
    # @return [Boolean]
    def eql?(other)
      self.class == other.class && (x == other.x && y == other.y)
    end

    # Return the escape sequence required to position the cursor at a particular
    # point on the screen. When passed a block, will do the aforementioned,
    # call the block and then reposition to this location.
    #
    # @param block [Proc]
    # @return [String]
    def to_s(&block)
      if block_given?
        [ sequence, yield, sequence ].join

      else
        sequence

      end
    end

    private

    # Returns the escape sequence to reposition the cursors at the coordinates
    # specified by x and y.
    #
    # @return [String]
    def sequence
      ["\e[", y, ';', x, 'H'].join
    end

  end # Position

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.4.1 lib/vedeu/support/position.rb
vedeu-0.4.0 lib/vedeu/support/position.rb
vedeu-0.3.5 lib/vedeu/support/position.rb