Sha256: e8900e6a76c72b2c6ae1aae403df0f30e50472e932762d52d8e2db7aeac9c548

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

module Vedeu

  # Change coordinates into an escape sequence to set the cursor position.
  # Also used as the basis of an Offset.
  #
  # @api private
  class Position < Array

    # Initializes a new instance of Position.
    #
    # @param y [Fixnum]
    # @param x [Fixnum]
    # @return [Position]
    def initialize(y = 0, x = 0)
      super([y, x])
    end

    # Returns an escape sequence to position the cursor. When passed a block,
    # will position the cursor, yield and return the original position.
    #
    # @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. If either of these values are 0, then we use 1,
    # as ANSI terminals do not have a 0, 0 coordinate.
    #
    # @return [String]
    def sequence
      ["\e[", (y == 0 ? 1 : y), ';', (x == 0 ? 1 : x), 'H'].join
    end

    # Returns the y coordinate. Can be 0 as this class may also be used as an
    # Offset.
    #
    # @return [Fixnum]
    def y
      (first == nil) ? 0 : first
    end

    # Returns the x coordinate. Can be 0 as this class may also be used as an
    # Offset.
    #
    # @return [Fixnum]
    def x
      (last == nil) ? 0 : last
    end

  end # Position

end # Vedeu

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
vedeu-0.2.12 lib/vedeu/support/position.rb
vedeu-0.2.11 lib/vedeu/support/position.rb
vedeu-0.2.10 lib/vedeu/support/position.rb
vedeu-0.2.9 lib/vedeu/support/position.rb
vedeu-0.2.8 lib/vedeu/support/position.rb
vedeu-0.2.7 lib/vedeu/support/position.rb
vedeu-0.2.6 lib/vedeu/support/position.rb
vedeu-0.2.5 lib/vedeu/support/position.rb
vedeu-0.2.4 lib/vedeu/support/position.rb