Class: Vedeu::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/geometry/position.rb

Overview

Change coordinates into an escape sequence to set the cursor position.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(y = 1, x = 1) ⇒ Position

Initializes a new instance of Vedeu::Position.

Parameters:

  • y (Fixnum) (defaults to: 1)

    The row/line position.

  • x (Fixnum) (defaults to: 1)

    The column/character position.



44
45
46
47
# File 'lib/vedeu/geometry/position.rb', line 44

def initialize(y = 1, x = 1)
  @y = (y.nil? || y < 1) ? 1 : y
  @x = (x.nil? || x < 1) ? 1 : x
end

Instance Attribute Details

#xFixnum (readonly) Also known as: last

Returns:

  • (Fixnum)


12
13
14
# File 'lib/vedeu/geometry/position.rb', line 12

def x
  @x
end

#yFixnum (readonly) Also known as: first

Returns:

  • (Fixnum)


8
9
10
# File 'lib/vedeu/geometry/position.rb', line 8

def y
  @y
end

Class Method Details

.[](y, x) ⇒ Object

Convenience constructor for Vedeu::Position.

Parameters:

  • y (Fixnum)

    The row/line position.

  • x (Fixnum)

    The column/character position.



20
21
22
# File 'lib/vedeu/geometry/position.rb', line 20

def self.[](y, x)
  new(y, x)
end

.coerce(value) ⇒ void

This method returns an undefined value.

Parameters:



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vedeu/geometry/position.rb', line 26

def self.coerce(value)
  if value.is_a?(self)
    value

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

  elsif value.is_a?(Hash)
    new(value.fetch(:y, 1), value.fetch(:x, 1))

  end
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

An object is equal when its values are the same.

Parameters:

Returns:

  • (Boolean)


53
54
55
# File 'lib/vedeu/geometry/position.rb', line 53

def eql?(other)
  self.class == other.class && (x == other.x && y == other.y)
end

#sequenceString (private)

Returns the escape sequence to reposition the cursors at the coordinates specified by x and y.

Returns:

  • (String)


88
89
90
# File 'lib/vedeu/geometry/position.rb', line 88

def sequence
  ["\e[", y, ';', x, 'H'].join
end

#to_aArray<Fixnum>

Return a tuple containing the y and x coordinates.

Returns:

  • (Array<Fixnum>)


61
62
63
# File 'lib/vedeu/geometry/position.rb', line 61

def to_a
  [y, x]
end

#to_sString

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.

Yield Returns:

  • (void)

    Returns the block wrapped in position escape sequences.

Returns:

  • (String)


72
73
74
75
76
77
78
79
80
# File 'lib/vedeu/geometry/position.rb', line 72

def to_s
  if block_given?
    [sequence, yield, sequence].join

  else
    sequence

  end
end