Class: Vedeu::Geometry::Position Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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) ⇒ Vedeu::Geometry::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a new instance of Vedeu::Geometry::Position.



49
50
51
52
53
54
# File 'lib/vedeu/geometry/position.rb', line 49

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

  freeze
end

Instance Attribute Details

#xFixnum (readonly) Also known as: last

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def x
  @x
end

#yFixnum (readonly) Also known as: first

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
# File 'lib/vedeu/geometry/position.rb', line 14

def y
  @y
end

Class Method Details

.[](y, x) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convenience constructor for Vedeu::Geometry::Position.



25
26
27
# File 'lib/vedeu/geometry/position.rb', line 25

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

.coerce(value) ⇒ Vedeu::Geometry::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vedeu/geometry/position.rb', line 31

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

#<=>(other) ⇒ Fixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
72
73
74
75
76
77
# File 'lib/vedeu/geometry/position.rb', line 69

def <=>(other)
  if y == other.y
    x <=> other.x

  else
    y <=> other.y

  end
end

#as_indicesArray<Fixnum>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a position into an index for the terminal. An index is the position minus 1.



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

def as_indices
  xi = ((x - 1) <= 1) ? 0 : (x - 1)
  yi = ((y - 1) <= 1) ? 0 : (y - 1)

  [yi, xi]
end

#downVedeu::Geometry::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Increase y coordinate; moves down.



118
119
120
# File 'lib/vedeu/geometry/position.rb', line 118

def down
  Vedeu::Geometry::Position.new(y + 1, x)
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An object is equal when its values are the same.



83
84
85
# File 'lib/vedeu/geometry/position.rb', line 83

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

#leftVedeu::Geometry::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decrease x coordinate; moves left.



125
126
127
# File 'lib/vedeu/geometry/position.rb', line 125

def left
  Vedeu::Geometry::Position.new(y, x - 1)
end

#rightVedeu::Geometry::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Increase x coordinate; moves right.



132
133
134
# File 'lib/vedeu/geometry/position.rb', line 132

def right
  Vedeu::Geometry::Position.new(y, x + 1)
end

#sequenceString (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



149
150
151
# File 'lib/vedeu/geometry/position.rb', line 149

def sequence
  "\e[#{y};#{x}H".freeze
end

#to_aArray<Fixnum>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a tuple containing the y and x coordinates.



91
92
93
# File 'lib/vedeu/geometry/position.rb', line 91

def to_a
  [y, x]
end

#to_positionVedeu::Geometry::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



96
97
98
# File 'lib/vedeu/geometry/position.rb', line 96

def to_position
  self
end

#to_sString Also known as: to_str

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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:

  • (String)

    Returns the block wrapped in position escape sequences.



108
109
110
111
112
# File 'lib/vedeu/geometry/position.rb', line 108

def to_s
  return "#{sequence}#{yield}".freeze if block_given?

  sequence
end

#upVedeu::Geometry::Position

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decrease y coordinate; moves up.



139
140
141
# File 'lib/vedeu/geometry/position.rb', line 139

def up
  Vedeu::Geometry::Position.new(y - 1, x)
end