Sha256: 9da00da8d6fa48dffe74bc8e146919a04f166682538bde905e2daff2c48e4600

Contents?: true

Size: 979 Bytes

Versions: 4

Compression:

Stored size: 979 Bytes

Contents

module Vedeu

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

    # Initializes a new instance of Position.
    #
    # @param y [Fixnum]
    # @param x [Fixnum]
    # @return [Position]
    def initialize(y = 1, x = 1)
      @y, @x = 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

    # @api private
    # @return [String]
    def sequence
      ["\e[", y, ';', x, 'H'].join
    end

    # @api private
    # @return [Fixnum]
    def y
      (@y == 0 || @y == nil) ? 1 : @y
    end

    # @api private
    # @return [Fixnum]
    def x
      (@x == 0 || @x == nil) ? 1 : @x
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vedeu-0.2.2 lib/vedeu/support/position.rb
vedeu-0.2.1 lib/vedeu/support/position.rb
vedeu-0.2.0 lib/vedeu/support/position.rb
vedeu-0.1.19 lib/vedeu/support/position.rb